微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C:分段错误核心转储,同时查找函数名称

我想编写一个C代码来查找C文件中的所有函数并打印相应的函数名称

我的代码是:

#include <stdio.h> #include <string.h> #define SIZE 1024 void ffname(char *line) { int i=1,j=0; char *dt; char *name; strtok(line,"("); dt = strchr(line,' '); if(dt[i] == '*') i++; while(dt[i] != '') { name[j]=dt[i]; i++; j++; } name[j] =''; printf("Function name is: %sn",name); } int main(int argc,char **argv) { if(argc < 2) { printf("Give the filename n"); printf("Usage: %s filenamen",argv[0]); return -1; } int i,lines =0,funlines =0,count =0,fn =0,flag =0; char c[100],b[100]; FILE *fd; fd = fopen(argv[1],"r"); while(fgets(c,SIZE,fd)) { lines++; i=0; for(i=0;i<strlen(c);i++) { while( c[i] =='t' || c[i] == ' ') { i++; } if( c[i] == '{') { count++; if(flag) { funlines++; } if(count == 1) { fn++; printf("Function %d is Started..............n",fn); flag = 1; ffname(b); } break; } else if( c[i] == '}') { count--; if(!count) { flag = 0; printf("No of lines in the function %d is: %dn",fn,funlines); printf("Function %d is finished..........n",fn); funlines = 0; } else { funlines++; } break; } else if(flag) { funlines++; break; } } strcpy(b,c); } printf("Total no of fucnion%dn",fn); printf("Total no of lines%dn",lines); return 0; }

当我把下面的C文件作为input时,

#include<stdio.h> void add() { int a=5,b=7; printf("Addition is:%dn",a+b); } void sub() { int a=20,b=8; printf("Subtraction is:%dn",ab); } void main() { char *name="dhahira dhasneem"; char *line; line=strchr(name,' '); printf("Line:%sn",line); printf("Name:%sn",name); add(); sub(); }

我得到以下输出

ALSA Api:如何同时播放两个wave文件

uip_listen()函数不等待监听连接 – uIP库

如何在.net或其他技术中呈现应用程序

vfork()与gcc -O2似乎有错误的结果。

将GCC选项添加到C源文件的顶部

Function 1 is Started.............. Segmentation fault (core dumped)

我使用valgrind ,但我不知道如何识别错误。 请指导我 谢谢。

更新:

当我使用build议的答案时,我得到了输出。 之后,我想扩展我以前的代码,将函数的详细信息(函数名称函数深度)存储到结构中。 当我用来存储简单程序的function细节时,我得到了输出。 但是我得到了我的程序的以下输出,当我在gdb运行这个。

(gdb) b 87 Breakpoint 1 at 0x804885e: file fun_printstruct.c,line 87. (gdb) r dat.c Starting program: /home/dhahira/dhas/Project/a.out dat.c Function 1 is Started.............. Program received signal SIGSEGV,Segmentation fault. 0x080485d4 in ffname (line=0xbfffe71c "/*struct *dhahira",name=0x0) at fun_printstruct.c:21 21 name[j]=dt[i]; (gdb) s Program terminated with signal SIGSEGV,Segmentation fault. The program no longer exists.

我的代码是:(扩展用于存储function的细节结构)

#include <stdio.h> #include <string.h> #define SIZE 1024 void ffname(char *line) { int i=1,j=0; char *dt; char name[SIZE]; strtok(line,flag =0,fg=0,size=0,emptyflag=0,commandflag=0; char c[SIZE],b[SIZE],st[SIZE],d[SIZE]; int command[]={}; FILE *fd; fd = fopen(argv[1],fd)) { emptyflag=0; lines++; for(i=0;i<(sizeof(command)/4);i++) { if(lines == command[i]) { commandflag=1; break; } } strcpy(st,c); strtok(st," "); size = strlen(c); if(size == 1 && (strcmp(c,"n"))== 0) emptyflag=1; if( !strcmp(st,"struct")) fg=1; for(i=0;i<size;i++) { if(commandflag) { break; } while( c[i] =='t' || c[i] == ' ') { i++; } if( c[i] == '{') { count++; if(flag) { if(!emptyflag) funlines++; else emptyflag=0; } if(count ==1 && fg ==1) { if(b[strlen(b)-2] == ')') { fn++; printf("Function %d is Started..............n",fn); flag = 1; ffname(b); } else { count--; } } else if(count == 1) { fn++; printf("Function %d is Started..............n",fn); flag = 1; ffname(b); } break; } else if( c[i] == '}') { count--; if(count ==0 && fg ==1) { flag = 0; printf("No of lines in the function %d is: %dn",fn); funlines = 0; fg=0; } else if(count ==0) { flag = 0; printf("No of lines in the function %d is: %dn",fn); funlines = 0; } else if(count == -1) { count=0; fg=0; } else { if(!emptyflag) funlines++; else emptyflag=0; } break; } else if(flag ==1 && fg==1) { if(!emptyflag) funlines++; else emptyflag=0; break; } else if(flag) { if(!emptyflag) funlines++; else emptyflag=0; break; } break; } if(commandflag == 1) commandflag = 0; else strcpy(b,lines); return 0; }

请指导我克服这个问题。

这个问题是由于扩展代码而引起的吗?(我可以分别得到正确的输出。)

如何让C#中的进程(不是线程)同步文件系统访问

检测string字符集

如何分配单独的内存来存储机密数据?

在linux中调度任务

频繁isatty()调用性能影响

您将SIZE声明为1024,然后读入长度为100的数组:

#define SIZE 1024 // ... snip .... char c[100],fd))

这意味着你正在写好你的数组的边界,到你的堆栈的其余部分,并导致腐败。 在这种情况下,这可能不会实际发生,因为您的输入文件中的行全部小于100个字符,但是如果有人以长行传入文件,则可能会发生这种情况。

这应该是

char c[SIZE],b[SIZE];

或者在fgets()调用中使用sizof() :

fgets(c,sizeof(c),fd)

正如其他人所指出的那样,最好是打开所有可用的警告; 这将帮助您更快地发现错误。 在GCC或Clang中,我会推荐-Wall -Wextra -Werror ; 这将使所有的常见警告,并导致警告被视为错误,所以你不能忽视它们。 如果我在代码上运行它,我也会得到以下警告:

sf.c:16:9: error: variable 'name' is uninitialized when used here [-Werror,-Wuninitialized] name[j]=dt[i]; ^~~~ sf.c:9:15: note: initialize the variable 'name' to silence this warning char *name; ^ = NULL sf.c:40:18: error: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] for(i=0;i<strlen(c);i++) ~^~~~~~~~~~

第二个不是一个严重的问题,只是施放或者声明i没有签名; 首先你应该修复。 你需要为堆栈( char name[SIZE];或者类似的)或者动态地( char *name = malloc(strlen(line));或者类似的东西char *name = malloc(strlen(line));分配一个名字缓冲区。 事实上, name是一个未初始化的指针; 它可以指向内存中的任何地方,一旦你尝试通过存储name[j]解引用它,你正在写入一个无效的内存区域,并得到一个错误

最后,一旦你解决了这些问题,如果你还有其他问题,我建议在调试器下运行它,看看问题出在哪里。 如果您使用的是IDE,则可能内置了一个调试器接口; 如果不是,用-g编译,然后运行gdb executable arguments 。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐