printf() 打印写入指针的正确 int 值,但不打印写入另一个指针的正确 double 值。这是为什么?
printf() prints the correct int value written to a pointer, but not the correct double value written to another. Why is that?
我尝试声明两个变量,一个是 int *
类型,另一个是 double *
类型,并为每个变量分配了地址,但是通过取消引用和打印进行的分配显示了 [=13= 的正确值] 但打印 0.0
for double.why is that?
#include <stdio.h>
int main(void)
{
int *x;
x = (int *)&x;
*x = 3;
//print val of pointer
printf("%d\n", x);
double *y;
y = (double *)&y;
*y = 4.0;
printf("%lf\n", y);
return 0;
}
天哪,这是我最近看到的最奇怪的一段代码...
无论如何,如果您真的想弄清楚整个代码中发生了什么,最好的方法是使用调试器逐步执行它。这是它在我的机器上的工作方式:
(gdb) break main
Breakpoint 1 at 0x400535: file test.c, line 6.
(gdb) run
...
Breakpoint 1, main () at test.c:6
warning: Source file is more recent than executable.
6 x = (int *)&x;
(gdb) n
7 *x = 3;
(gdb) p x
= (int *) 0x7fffffffdab0
(gdb) n
9 printf("%d\n", x);
(gdb) p x
= (int *) 0x7fff00000003
(gdb) n
3
12 y = (double *)&y;
(gdb) n
13 *y = 4.0;
(gdb) p y
= (double *) 0x7fffffffdab8
(gdb) n
14 printf("%lf\n", y);
(gdb) p y
= (double *) 0x4010000000000000
(gdb) n
0.000000
15 return 0;
(gdb)
基本上你正在做的是通过在过程中使用它们自己来弄乱指针值。在执行 *x = 3;
时,您可以看到您通过写入 0x00000003 消除了 x
的最低有效 32 位。之后,当您执行 *y = 4.0;
时,您会用 4.0
的内部双精度表示覆盖整个指针值。直觉上,第二个 printf
应该打印 4.0
,所以我猜问题出在 printf
本身。如果你这样做:
double test;
memcpy(&test, &y, sizeof(double));
printf("%lf\n", test);
这将输出 4.000000
.
我得到 4.0。
你所做的就是 re-interpret 分配的内存用于存储地址(x 和 y),分别为 int 和 double。
你这样做了两次:当你将数据值分配给 re-interpreted 内存时,当你打印它的副本时。这两种情况是不同的。
通过不兼容类型的指针写入内存是未定义的行为,众所周知,像 gcc 这样的编译器在这种情况下会做一些有趣的事情(陷阱或忽略代码)。对此有很多曲折的讨论,包括 Linus Torvalds 的著名咆哮。它可能有效也可能无效。如果它有效,它可能会做预期的事情。 (要获得正确的代码,您必须使用联合或执行 memcpy。)
它起作用的一个条件是您的数据类型不需要比指针更多的space。在 32 位体系结构上(对于 64 位 Intel CPU 可能是 32 位编译器),double 将比 4 字节地址长(IEEE 754 double 有 8 个字节)。 *y = 4.0;
写入超出 y
的内存,覆盖堆栈上的其他数据。 (注意 y
指向它自己,因此分配给 *y
会覆盖 y
自己的内存。)
将指针值作为参数传递给 printf
,转换规范为 %d
resp。 %lf
也未定义。 (实际上,如果转换规范为 %p
且指针值未转换为 void *
,则它已经未定义;但这在常见体系结构中经常被忽略且无关紧要。)printf 将只解释堆栈上的内存(这是参数的副本)作为 int resp。作为双人。
为了了解发生了什么,让我们看一下 main 堆栈上的内存布局。我已经写了一个程序来详细说明它;来源如下。在我的 64 位 Windows 上,4.0 的双精度值打印正常;指针变量 y
足够大以容纳双精度数的字节,并且所有 8 个字节都被复制到 printf 的堆栈。但是如果指针大小只有 4 个字节,那么只有这 4 个字节会被复制到 printf 的堆栈中,它们都是 0,而超出该堆栈的字节将包含来自早期操作的内存,或任意值,例如 0 ;-) which printf 将读取以尝试解码 double。
这是在各个步骤中对 64 位架构上的堆栈的检查。我用两个哨兵变量 declStart
和 declEnd
将指针声明括起来,这样我就可以看到内存在哪里。我假设该程序也会 运行 在 32 位架构上进行微小的更改。试一试,告诉我们你看到了什么!
更新:它 运行 在 ideone 上,似乎有 4 字节地址。 double 版本不打印 0.0 而是一些任意值,可能是因为 4 个地址字节后面的堆栈垃圾。比照。 https://ideone.com/TJAXli.
以上输出的程序在这里:
#include <stdio.h>
void dumpMem(void *start, int numBytes)
{
printf("memory at %p:", start);
char *p = start;
while((unsigned long)p%8){ p--; numBytes++;} // align to 8 byte boundary
for(int i=0; i<numBytes; i++)
{
if( i%8 == 0 ) printf("\nAddr %p:", p+i);
printf(" %02x", (unsigned int) (p[i] & 0xff));
}
putchar('\n');
}
int len; // static allocation, protect them from stack overwrites
char *from, *to;
int main(void)
{
unsigned int declStart = 0xaaaaaaaa; // marker
int *x = (int *) 0xbbbbbbbbbbbbbbbb;
double *y = (double *)0xcccccccccccccccc;
unsigned int declEnd = 0xdddddddd; // marker
printf("Addr. of x: %p,\n of y: %p\n", &x, &y);
// This is all UB because the pointers are not
// belonging to the same object. But it should
// work on standard architectures.
// All calls to dumpMem() therefore are UB, too.
// Thinking of it, I'd be hard-pressed to find
// any defined behavior in this program.
if( &declStart < &declEnd )
{
from = (char *)&declStart;
to = (char *)&declEnd + sizeof(declEnd);
}
else
{
from = (char *)&declEnd;
to = (char *)&declStart + sizeof(declStart);
}
len = to - from;
printf("len is %d\n", len);
printf("Memory after initializations:\n");
dumpMem(from, len);
x = (int *)&x;
printf("\nMemory after assigning own address %p to x/*x: \n", &x);
dumpMem(from, len);
*x = 3;
printf("\nMemory after assigning 3 to x/*x: \n");
dumpMem(from, len);
//print val of pointer
printf("x as long: %d\n", (unsigned long)x);
y = (double *)&y;
*y = 4.0;
printf("\nMemory after assigning 4.0 to y/*y: \n");
dumpMem(from, len);
printf("y as float: %f\n", y);
printf("y as double: %lf\n", y);
printf("y as unsigned int: 0x%x\n", y);
printf("y as unsigned long: 0x%lx\n", y);
return 0;
}
我尝试声明两个变量,一个是 int *
类型,另一个是 double *
类型,并为每个变量分配了地址,但是通过取消引用和打印进行的分配显示了 [=13= 的正确值] 但打印 0.0
for double.why is that?
#include <stdio.h>
int main(void)
{
int *x;
x = (int *)&x;
*x = 3;
//print val of pointer
printf("%d\n", x);
double *y;
y = (double *)&y;
*y = 4.0;
printf("%lf\n", y);
return 0;
}
天哪,这是我最近看到的最奇怪的一段代码...
无论如何,如果您真的想弄清楚整个代码中发生了什么,最好的方法是使用调试器逐步执行它。这是它在我的机器上的工作方式:
(gdb) break main
Breakpoint 1 at 0x400535: file test.c, line 6.
(gdb) run
...
Breakpoint 1, main () at test.c:6
warning: Source file is more recent than executable.
6 x = (int *)&x;
(gdb) n
7 *x = 3;
(gdb) p x
= (int *) 0x7fffffffdab0
(gdb) n
9 printf("%d\n", x);
(gdb) p x
= (int *) 0x7fff00000003
(gdb) n
3
12 y = (double *)&y;
(gdb) n
13 *y = 4.0;
(gdb) p y
= (double *) 0x7fffffffdab8
(gdb) n
14 printf("%lf\n", y);
(gdb) p y
= (double *) 0x4010000000000000
(gdb) n
0.000000
15 return 0;
(gdb)
基本上你正在做的是通过在过程中使用它们自己来弄乱指针值。在执行 *x = 3;
时,您可以看到您通过写入 0x00000003 消除了 x
的最低有效 32 位。之后,当您执行 *y = 4.0;
时,您会用 4.0
的内部双精度表示覆盖整个指针值。直觉上,第二个 printf
应该打印 4.0
,所以我猜问题出在 printf
本身。如果你这样做:
double test;
memcpy(&test, &y, sizeof(double));
printf("%lf\n", test);
这将输出 4.000000
.
我得到 4.0。
你所做的就是 re-interpret 分配的内存用于存储地址(x 和 y),分别为 int 和 double。
你这样做了两次:当你将数据值分配给 re-interpreted 内存时,当你打印它的副本时。这两种情况是不同的。
通过不兼容类型的指针写入内存是未定义的行为,众所周知,像 gcc 这样的编译器在这种情况下会做一些有趣的事情(陷阱或忽略代码)。对此有很多曲折的讨论,包括 Linus Torvalds 的著名咆哮。它可能有效也可能无效。如果它有效,它可能会做预期的事情。 (要获得正确的代码,您必须使用联合或执行 memcpy。)
它起作用的一个条件是您的数据类型不需要比指针更多的space。在 32 位体系结构上(对于 64 位 Intel CPU 可能是 32 位编译器),double 将比 4 字节地址长(IEEE 754 double 有 8 个字节)。
*y = 4.0;
写入超出y
的内存,覆盖堆栈上的其他数据。 (注意y
指向它自己,因此分配给*y
会覆盖y
自己的内存。)将指针值作为参数传递给
printf
,转换规范为%d
resp。%lf
也未定义。 (实际上,如果转换规范为%p
且指针值未转换为void *
,则它已经未定义;但这在常见体系结构中经常被忽略且无关紧要。)printf 将只解释堆栈上的内存(这是参数的副本)作为 int resp。作为双人。
为了了解发生了什么,让我们看一下 main 堆栈上的内存布局。我已经写了一个程序来详细说明它;来源如下。在我的 64 位 Windows 上,4.0 的双精度值打印正常;指针变量 y
足够大以容纳双精度数的字节,并且所有 8 个字节都被复制到 printf 的堆栈。但是如果指针大小只有 4 个字节,那么只有这 4 个字节会被复制到 printf 的堆栈中,它们都是 0,而超出该堆栈的字节将包含来自早期操作的内存,或任意值,例如 0 ;-) which printf 将读取以尝试解码 double。
这是在各个步骤中对 64 位架构上的堆栈的检查。我用两个哨兵变量 declStart
和 declEnd
将指针声明括起来,这样我就可以看到内存在哪里。我假设该程序也会 运行 在 32 位架构上进行微小的更改。试一试,告诉我们你看到了什么!
更新:它 运行 在 ideone 上,似乎有 4 字节地址。 double 版本不打印 0.0 而是一些任意值,可能是因为 4 个地址字节后面的堆栈垃圾。比照。 https://ideone.com/TJAXli.
以上输出的程序在这里:
#include <stdio.h>
void dumpMem(void *start, int numBytes)
{
printf("memory at %p:", start);
char *p = start;
while((unsigned long)p%8){ p--; numBytes++;} // align to 8 byte boundary
for(int i=0; i<numBytes; i++)
{
if( i%8 == 0 ) printf("\nAddr %p:", p+i);
printf(" %02x", (unsigned int) (p[i] & 0xff));
}
putchar('\n');
}
int len; // static allocation, protect them from stack overwrites
char *from, *to;
int main(void)
{
unsigned int declStart = 0xaaaaaaaa; // marker
int *x = (int *) 0xbbbbbbbbbbbbbbbb;
double *y = (double *)0xcccccccccccccccc;
unsigned int declEnd = 0xdddddddd; // marker
printf("Addr. of x: %p,\n of y: %p\n", &x, &y);
// This is all UB because the pointers are not
// belonging to the same object. But it should
// work on standard architectures.
// All calls to dumpMem() therefore are UB, too.
// Thinking of it, I'd be hard-pressed to find
// any defined behavior in this program.
if( &declStart < &declEnd )
{
from = (char *)&declStart;
to = (char *)&declEnd + sizeof(declEnd);
}
else
{
from = (char *)&declEnd;
to = (char *)&declStart + sizeof(declStart);
}
len = to - from;
printf("len is %d\n", len);
printf("Memory after initializations:\n");
dumpMem(from, len);
x = (int *)&x;
printf("\nMemory after assigning own address %p to x/*x: \n", &x);
dumpMem(from, len);
*x = 3;
printf("\nMemory after assigning 3 to x/*x: \n");
dumpMem(from, len);
//print val of pointer
printf("x as long: %d\n", (unsigned long)x);
y = (double *)&y;
*y = 4.0;
printf("\nMemory after assigning 4.0 to y/*y: \n");
dumpMem(from, len);
printf("y as float: %f\n", y);
printf("y as double: %lf\n", y);
printf("y as unsigned int: 0x%x\n", y);
printf("y as unsigned long: 0x%lx\n", y);
return 0;
}