gpt4 book ai didi

c - printf() 打印写入指针的正确 int 值,但不打印写入另一个指针的正确 double 值。这是为什么?

转载 作者:行者123 更新时间:2023-12-01 13:46:25 25 4
gpt4 key购买 nike

我尝试声明两个变量,一个是 int * 类型,另一个是 double * 类型,并为每个变量分配了地址,但是通过取消引用和打印分配显示正确int 的值,但打印 0.0 为 double。这是为什么?

#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;
}

最佳答案

我得到 4.0。

您所做的是将分配给存储地址(x 和 y)的内存分别重新解释为 int 和 double。

你这样做了两次:当你将数据值分配给重新解释的内存时,当你打印它的副本时。这两种情况是截然不同的。

  1. 通过不兼容类型的指针写入内存是未定义的行为,众所周知,像 gcc 这样的编译器在这种情况下会做一些有趣的事情(陷阱或忽略代码)。对此有很多曲折的讨论,包括 Linus Torvalds 的著名咆哮。它可能有效也可能无效。如果它有效,它可能会做预期的事情。 (要获得正确的代码,您必须使用 union 或执行 memcpy。)

    它起作用的一个条件是您的数据类型不需要比指针更多的空间。在 32 位架构上(可能是用于 64 位 Intel CPU 的 32 位编译器),double 将比 4 字节地址长(IEEE 754 double 有 8 个字节)。 *y = 4.0; 写入超出y 的内存,覆盖堆栈上的其他数据。 (注意 y 指向它自己,因此分配给 *y 会覆盖 y 自己的内存。)

  2. 将指针值作为参数传递给 printf,转换规范为 %d resp。 %lf 也未定义。 (实际上,如果转换规范是 %p 并且指针值未强制转换为 void *,则它已经未定义;但这在常见体系结构上经常被忽略且无关紧要。)printf 将只需将堆栈上的内存(这是参数的副本)解释为 int resp。作为 double 。

为了理解发生了什么,让我们看一下 main 堆栈上的内存布局。我已经写了一个程序来详细说明它;来源如下。在我的 64 位 Windows 上,double 值 4.0 可以正常打印;指针变量 y 足够大以容纳 double 的字节,并且所有 8 个字节都被复制到 printf 的堆栈。但是如果指针大小只有 4 个字节,那么只有这 4 个字节会被复制到 printf 的堆栈中,它们都是 0,而超出该堆栈的字节将包含来自早期操作的内存,或任意值,例如 0 ;-) which printf 将读取以尝试解码 double。

这是在各个步骤中对 64 位架构上的堆栈的检查。我用两个标记变量 declStartdeclEnd 将指针声明括起来,这样我就可以看到内存在哪里。我假设该程序也可以在 32 位架构上运行,只需稍作改动。尝试一下,然后告诉我们您看到了什么!

更新:它在 ideone 上运行,它似乎有 4 字节地址。 double 版本不打印 0.0 而是一些任意值,可能是因为 4 个地址字节后面的堆栈垃圾。比照。 https://ideone.com/TJAXli .

enter image description here

上面输出的程序在这里:

#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;
}

关于c - printf() 打印写入指针的正确 int 值,但不打印写入另一个指针的正确 double 值。这是为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35722278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com