== 18077 == by 0x80483CD:main(test.c:30) == 18077 ==地址0x4不是堆栈,malloc''d或(最近)free''d == 18077 == == 18077 ==流程以信号11的默认动作终止 (SIGSEGV) == 18077 ==不在地址0x4的映射区域内访问 == 18077 ==在0x8048494:test(test.c:72) == 18077 == by 0x80483CD:main(test.c:30) == 18077 == == 18077 ==错误摘要:2错误来自2个上下文(被压制:11个来自 1) == 18077 == malloc / free:在退出时使用:2,651个块中的214,600个字节。 == 18077 == malloc / free:2,651个allocs,0个frees,分配214,600个字节。 == 18077 ==对于检测到的错误计数,请重新运行:-v == 18077 ==搜索指向2,651个未释放块的指针。 == 18077 ==已检查60,224字节。 / **********************************结束VALGRIND输出 ***** ************************** /I am pretty new to C and doing my first project in C. I actually readalmost the entire FAQ, but can''t seem to figure out this problem.I have a structure. I have a list of these structures. Inside eachstructure, I have two members: a list of strings, and a string.I have made a sample program below that exhibits the error I amhaving. I also read about Valgrind, and used it to tell me where Iwas getting the segmentation fault, which is really helpful. TheValgrind output is below my sample code.At line 72 (it is labeled below), I use an unitialized value and then,I try to write to it. I am not sure what I am doing wrong, because Ithink I am initializing the value inside my "initialize" routine. Cananyone help me figure out where I am going wrong./********************************** START SAMPLE CODE**************************************/#include <stdlib.h>#include <alloca.h>#include <stdio.h>#include <string.h>#include <unistd.h>#define MAXSTRINGS 50#define MAXSTRING 80#define MAXSAMPLES 50/* this is my sample structure. its members are a character poitner,and a pointer to a character pointer */struct sample {char *string; /*substitute will fill in full path_name */char **stringlist; /*list of arguments to send to execve */};int main (void) {/* function declarations */struct sample **initialize(struct sample **samplist);void test (struct sample **samplist);/* neener is a pointer to a pointer of type sample* */struct sample **neener;neener = initialize(neener);test (neener);}struct sample **initialize (struct sample **samplist) {struct sample *sp;int i, j;/* allocate enough space for 50 pointers to sample structures */samplist = (struct sample **) malloc (MAXSAMPLES * sizeof(structsample *));/* set sp to the the first pointer allocated to samplist */sp = *samplist;/* for each of samplist''s pointers to sample structures, */for (i = 0; i < MAXSAMPLES; ++i) {/* allocate enough space for one pointer to the sample structure*/sp = (struct sample *) malloc (sizeof (struct sample));/* allocate enough space of the for a string */sp->string = (char *) malloc (MAXSTRING * sizeof (char));/* allocate enough pointers for the stringlist member''s pointer*/sp->stringlist = (char **) malloc (MAXSTRINGS * sizeof (char *));/* and for each of stringlists pointers, allocate enough space fora string*/for (j = 0; j < MAXSTRINGS; ++j)*(sp->stringlist + j) = (char *) malloc (MAXSTRING *sizeof(char));/* increment sp so it points to samplist''s next allocated pointer*/++sp;}return samplist;}void test (struct sample **samplist) {struct sample *sp;char *string;sp = *samplist;string = "Testing 1 2 3";*(sp->stringlist + 0) = strcpy(*(sp->stringlist + 0), string); /*line 72 *//* segmentation fault */}/********************************** END SAMPLE CODE**************************************//********************************** START VALGRIND OUTPUT*******************************/==18077== Use of uninitialised value of size 4==18077== at 0x8048494: test (test.c:72)==18077== by 0x80483CD: main (test.c:30)==18077====18077== Invalid read of size 4==18077== at 0x8048494: test (test.c:72)==18077== by 0x80483CD: main (test.c:30)==18077== Address 0x4 is not stack''d, malloc''d or (recently) free''d==18077====18077== Process terminating with default action of signal 11(SIGSEGV)==18077== Access not within mapped region at address 0x4==18077== at 0x8048494: test (test.c:72)==18077== by 0x80483CD: main (test.c:30)==18077====18077== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 11 from1)==18077== malloc/free: in use at exit: 214,600 bytes in 2,651 blocks.==18077== malloc/free: 2,651 allocs, 0 frees, 214,600 bytes allocated.==18077== For counts of detected errors, rerun with: -v==18077== searching for pointers to 2,651 not-freed blocks.==18077== checked 60,224 bytes./********************************** END VALGRIND OUTPUT*******************************/推荐答案 9月15,14:44 pm,jbholman< jbhol ... @ gmail.comwrote:On Sep 15, 11:44 pm, jbholman <[email protected]: 我是C的新手并在C中完成我的第一个项目。实际上阅读了几乎整个FAQ的,但似乎无法弄清楚这个问题。I am pretty new to C and doing my first project in C. I actually readalmost the entire FAQ, but can''t seem to figure out this problem. < snip><snip> #include< stdlib.h> #include< alloca.h>#include <stdlib.h>#include <alloca.h> 您的代码在这里失败。 < alloca。这不是由 语言定义的标准标题。 发布到适当的新闻组,如果有的话,或者只是阅读你的 实施文档,如果有的话。Your code fails here. <alloca.his not a standard header defined bythe language.Post to an appropriate newsgroup, if any, or simply read yourimplementations documentation, if any. 9月15日,3:47 * pm,vipps ... @ gmail.com写道: 对此抱歉。我删除了#include< alloca.hand 重新编译。我使用gcc 4.2.3。我仍然得到与Valgrind相同的结果 相同的错误。对此再次抱歉。On Sep 15, 3:47*pm, [email protected] wrote:Sorry about this. I removed the the #include <alloca.handrecompiled. I used gcc 4.2.3. I still get the same error withidentical results from Valgrind. Sorry again about that. 9月15日晚上11点44分,jbholman< jbhol ... @ gmail.comwrote:On Sep 15, 11:44 pm, jbholman <[email protected]: 我对C很新,并在C中做我的第一个项目。*我实际上读了几乎整个FAQ的,但似乎无法弄清楚这个问题。 I am pretty new to C and doing my first project in C. *I actually read almost the entire FAQ, but can''t seem to figure out this problem. < snip><snip> #include< stdlib.h> #include< alloca.h> #include <stdlib.h> #include <alloca.h> 你的代码在这里失败了。 < alloca。这不是由 语言定义的标准标题。 发布到适当的新闻组,如果有的话,或者只是阅读你的 实施文档,如果有的话。Your code fails here. <alloca.his not a standard header defined bythe language.Post to an appropriate newsgroup, if any, or simply read yourimplementations documentation, if any. 9月15日晚上11:55,jbholman< jbhol ... @ gmail.comwrote:On Sep 15, 11:55 pm, jbholman <[email protected]: 对此感到抱歉。我删除了#include< alloca.hand 重新编译。我使用gcc 4.2.3。我仍然得到与Valgrind相同的结果 相同的错误。再次抱歉。Sorry about this. I removed the the #include <alloca.handrecompiled. I used gcc 4.2.3. I still get the same error withidentical results from Valgrind. Sorry again about that. < snip top post> 请不要发帖。 参见 < http://www.catb.org/jargon/html/T/top-post.html> < http://www.caliburn .nl / topposting.html> 现在,下一个错误是你包含< unistd.h,它也不是一个 标准的标题。 删除所有那些不是标准C的东西,你最终会得到一个没有做你想要的程序的。在适当的新闻组中发帖,而不是做b / b 。 (我不知道究竟在哪里指导你,但也许 < news:comp.unix.programmer将是合适的)<snip top post>Please don''t top post.See<http://www.catb.org/jargon/html/T/top-post.html><http://www.caliburn.nl/topposting.html>Now, the next error is when you include <unistd.hwhich is also not astandard header.Removing all those things that are not standard C, and you''ll end upwith a program that doesn''t quite do what you want. Instead of doingthat, post in an appropriate newsgroup.(I don''t know where exactly to direct you, but perhaps<news:comp.unix.programmerwould be appropriate) 这篇关于malloc用于结构成员和分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 19:29