Calloc 导致分段错误

发布于 2024-11-26 07:38:18 字数 2403 浏览 2 评论 0原文

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(){
int n=10;
char *s= calloc(2,sizeof(char));
sprintf(s,"%d",n);
printf(s);
return 0;
}

目的是将 2 位数字分配给 (char *)。 当我运行代码时,出现分段错误。 valgrind 的输出是-

==18540== Command: ./test
==18540== 
==18540== Conditional jump or move depends on uninitialised value(s)
==18540==    at 0x366C06F397: _IO_str_init_static_internal (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C8A: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540== 
==18540== Conditional jump or move depends on uninitialised value(s)
==18540==    at 0x366C06E37B: _IO_default_xsputn (in /lib64/libc-2.5.so)
==18540==    by 0x366C043D38: vfprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C98: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540== 
==18540== Conditional jump or move depends on uninitialised value(s)
==18540==    at 0x366C06F20A: _IO_str_overflow (in /lib64/libc-2.5.so)
==18540==    by 0x366C06E3E3: _IO_default_xsputn (in /lib64/libc-2.5.so)
==18540==    by 0x366C043D38: vfprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C98: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540== 
==18540== Use of uninitialised value of size 8
==18540==    at 0x366C06F241: _IO_str_overflow (in /lib64/libc-2.5.so)
==18540==    by 0x366C06E3E3: _IO_default_xsputn (in /lib64/libc-2.5.so)
==18540==    by 0x366C043D38: vfprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C98: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540== 
==18540== Invalid write of size 1
==18540==    at 0x366C06F241: _IO_str_overflow (in /lib64/libc-2.5.so)
==18540==    by 0x366C06E3E3: _IO_default_xsputn (in /lib64/libc-2.5.so)
==18540==    by 0x366C043D38: vfprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C98: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==18540== 

Here is my code:

#include <stdio.h>
#include <stdlib.h>

int main(){
int n=10;
char *s= calloc(2,sizeof(char));
sprintf(s,"%d",n);
printf(s);
return 0;
}

The intent is to assing 2 digit number to a (char *).
when I run the code, I get segmentation fault. Outout from valgrind is-

==18540== Command: ./test
==18540== 
==18540== Conditional jump or move depends on uninitialised value(s)
==18540==    at 0x366C06F397: _IO_str_init_static_internal (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C8A: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540== 
==18540== Conditional jump or move depends on uninitialised value(s)
==18540==    at 0x366C06E37B: _IO_default_xsputn (in /lib64/libc-2.5.so)
==18540==    by 0x366C043D38: vfprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C98: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540== 
==18540== Conditional jump or move depends on uninitialised value(s)
==18540==    at 0x366C06F20A: _IO_str_overflow (in /lib64/libc-2.5.so)
==18540==    by 0x366C06E3E3: _IO_default_xsputn (in /lib64/libc-2.5.so)
==18540==    by 0x366C043D38: vfprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C98: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540== 
==18540== Use of uninitialised value of size 8
==18540==    at 0x366C06F241: _IO_str_overflow (in /lib64/libc-2.5.so)
==18540==    by 0x366C06E3E3: _IO_default_xsputn (in /lib64/libc-2.5.so)
==18540==    by 0x366C043D38: vfprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C98: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540== 
==18540== Invalid write of size 1
==18540==    at 0x366C06F241: _IO_str_overflow (in /lib64/libc-2.5.so)
==18540==    by 0x366C06E3E3: _IO_default_xsputn (in /lib64/libc-2.5.so)
==18540==    by 0x366C043D38: vfprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C063C98: vsprintf (in /lib64/libc-2.5.so)
==18540==    by 0x366C04D677: sprintf (in /lib64/libc-2.5.so)
==18540==    by 0x4004FC: main (test.c:8)
==18540==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==18540== 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北音执念 2024-12-03 07:38:18

您只为 2 个字符分配空间,然后放置字符串“10\0”,该字符串需要另一个字符来保存 null/0 终止字符。因此,您需要为这个特定示例分配 3 个字符才能工作。

阅读C 字符串了解完整详细信息。

You allocate space for just 2 chars and then put the string '10\0' which needs another char to hold the null/0 terminating character. So you need to allocate 3 chars for this particular example to work.

Read up C Strings for full details.

海之角 2024-12-03 07:38:18

您分配了 2 个字节,但字符串大小为 3:

'1'、'0'、'\0'(空终止符),在这种情况下结果未定义,因为您损坏了堆

此外,当您分配内存时,不要最后忘记拨打免费电话。

You allocate 2 bytes, but the string size is 3:

'1', '0', '\0' (null terminator) the result is undefined in this case since you corrupt the heap

Also, when you allocate memory, don't forget to call free at the end.

美煞众生 2024-12-03 07:38:18

您需要为空终止字符串的空字符分配空间。总共 3 个字符。

另外 printf(s); 应该是:printf("%s", s);

You need to allocate room for the null character of a null terminated string. That's 3 characters total.

Also printf(s); should be: printf("%s", s);

空名 2024-12-03 07:38:18

对于这段特定的代码,字符串“10”需要 3 个字节,即 '1'、'0' 和 '\0' 。所以需要分配3个字节的内存。

For this particular piece of code the string "10" requires 3 bytes, which are '1', '0', and '\0' . So you need 3 bytes of memory to be allocated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文