C 指针混乱
我想在内存中存储一个字符串并稍后读取它:
$$->desc.constant->base.id = (char*)malloc(200);
sprintf($$->desc.constant->base.id, "%f", $1);
printf("->%s\n", $$->desc.constant->base.id); //LINE A
printf("->%i\n", $$->desc.constant); //LINE B
//SOME OTHER CODE
//Then, later on in a function call:
printf("%i", expr->desc.constant); // LINE D
printf("%s", expr->desc.constant->base.id); // LINE C
尽管 B 行和 D 行显示相同的地址,但 C 行中的 printf 因分段错误而失败。 我缺少什么?
任何帮助将不胜感激!
I want to store a string in memory and read it later:
$->desc.constant->base.id = (char*)malloc(200);
sprintf($->desc.constant->base.id, "%f", $1);
printf("->%s\n", $->desc.constant->base.id); //LINE A
printf("->%i\n", $->desc.constant); //LINE B
//SOME OTHER CODE
//Then, later on in a function call:
printf("%i", expr->desc.constant); // LINE D
printf("%s", expr->desc.constant->base.id); // LINE C
Although Line B and Line D show the same address, the printf in Line C fails with a Segmentation fault. What am I missing?
Any help would really be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那是无效的。 当您在其前面显示
constant
实际上是一个指针时,您不能将其视为int
类型。 它们不一定具有相同的大小和对齐方式。 使用用于void*
的格式。 它将正确输出内存地址:That is invalid. As you show the line prior to it that
constant
is actually a pointer, you cannot treat it as if it were of typeint
. They don't necassarily have the same sizeof and alignment. Use the format used forvoid*
. It will output memory addresses properly:malloc
的返回值。sprintf
->snprintf
"%f"
->"%.*g"
这是一个示例:
输出:
malloc
's return value.sprintf
->snprintf
"%f"
->"%.*g"
Here's an example:
Output:
也许在两段代码之间,您已经
释放
字符串了?Maybe between the time of the two pieces of code you have since
free
d the string?鉴于程序正在产生分段错误,我认为问题很可能是
expr->desc.constant
指定(指向)的结构自分配空间以来已被重用,或者可能这个空间根本就没有被真正分配过。该代码表现出各种小罪,例如使用
sprintf()
而不是snprintf()
,以及无缘无故地为浮点数的字符串表示分配 200 个字节。 (您不太可能需要那么多空间;如果您这样做,您很可能应该比您拥有的数字多至少 100 位,因为浮点数的指数范围通常为 +/-308,并且您这样做的唯一原因是需要 200 个字符才能允许令人难以置信的大或令人难以置信的小数字。)您已经表明
$$->desc.constant
指向同一个地方,但您没有表明该空间如何被分配。 然后,您在 $$->desc.constant->base.id 中分配字符串空间,而无需明确为base
分配空间。Given that the program is producing a segmentation fault, I think the problem is most likely that the structure designated (pointed to) by
expr->desc.constant
has been reused since the space was allocated, or possibly the space was never really allocated at all.The code exhibits various venial sins, such as using
sprintf()
instead ofsnprintf()
, and gratuitously allocating 200 bytes for the string representation of a floating point number. (You are very unlikely to need that much space; if you do, you should most probably allow for at least 100 more digits than you have, since the exponent range for floating point numbers is usually +/-308, and the only reason you'd need 200 characters is to allow for incredibly large or incredibly tiny numbers.)You have shown that
$$->desc.constant
points to the same place, but you have not shown how that space is allocated. You then allocate the string space in$$->desc.constant->base.id
, without clearly allocating the space forbase
.