Note that there are some explanatory texts on larger screens.

plurals
  1. POC: Segmentation Fault(Core dumped) while finding function names
    primarykey
    data
    text
    <p>I'd like to write a C code to find all the functions in a C file and print the corresponding function names. </p> <p>My code is: </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #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] != '\0') { name[j]=dt[i]; i++; j++; } name[j] ='\0'; printf("Function name is: %s\n", name); } int main(int argc, char **argv) { if(argc &lt; 2) { printf("Give the filename \n"); printf("Usage: %s filename\n", 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&lt;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: %d\n", 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%d\n",fn); printf("Total no of lines%d\n",lines); return 0; } </code></pre> <p>When I give the following C file as input,</p> <pre><code>#include&lt;stdio.h&gt; void add() { int a=5,b=7; printf("Addition is:%d\n", a+b); } void sub() { int a=20,b=8; printf("Subtraction is:%d\n", a-b); } void main() { char *name="dhahira dhasneem"; char *line; line=strchr(name,' '); printf("Line:%s\n",line); printf("Name:%s\n",name); add(); sub(); } </code></pre> <p>I get the following output.</p> <pre><code>Function 1 is Started.............. Segmentation fault (core dumped) </code></pre> <p>I use <code>valgrind</code>, but I don't know how to identify error with that. Please guide me. Thank you.</p> <p><strong>Update:</strong></p> <p>I got the output, when I used suggested answer. After that, I'd like to extend my previous code for storing the details of function(function name and function depth) into a Structure. I was getting output, when I used to store function details for simple program. But I'm getting the following output for my program, when I run this in<code>gdb</code>.</p> <pre><code>(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. </code></pre> <p>My code is:(extended for storing function details into structure)</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #define SIZE 1024 void ffname(char *line) { int i=1,j=0; char *dt; char name[SIZE]; strtok(line,"("); dt = strchr(line,' '); if(dt[i] == '*') i++; while(dt[i] != '\0') { name[j]=dt[i]; i++; j++; } name[j] ='\0'; printf("Function name is: %s\n", name); } int main(int argc, char **argv) { if(argc &lt; 2) { printf("Give the filename \n"); printf("Usage: %s filename\n", argv[0]); return -1; } int i, lines =0, funlines =0,count =0, fn =0, 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],"r"); while(fgets(c,SIZE,fd)) { emptyflag=0; lines++; for(i=0;i&lt;(sizeof(command)/4);i++) { if(lines == command[i]) { commandflag=1; break; } } strcpy(st,c); strtok(st," "); size = strlen(c); if(size == 1 &amp;&amp; (strcmp(c,"\n"))== 0) emptyflag=1; if( !strcmp(st,"struct")) fg=1; for(i=0;i&lt;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 &amp;&amp; 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 &amp;&amp; fg ==1) { flag = 0; printf("No of lines in the function %d is: %d\n", fn, funlines); printf("Function %d is finished..........\n", fn); funlines = 0; fg=0; } else if(count ==0) { flag = 0; printf("No of lines in the function %d is: %d\n", fn, funlines); printf("Function %d is finished..........\n", fn); funlines = 0; } else if(count == -1) { count=0; fg=0; } else { if(!emptyflag) funlines++; else emptyflag=0; } break; } else if(flag ==1 &amp;&amp; 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,c); } printf("Total no of fucnion%d\n",fn); printf("Total no of lines%d\n",lines); return 0; } </code></pre> <p>Please guide me to overcome from this problem. </p> <p>Is this problem coming because of extending the code?(I can get the correct output separately.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload