Segmentation Fault Error reported by GDB

W

william

I received a segfault error in a very short program for test purpose.
I found no luck even if I read some tricks about tracking down the
segfault. Can any one give me a hint which line of the following code
caused the problem?
******************************************
1 #include <stdio.h>
2 #include <string.h>
3
4 int main (int argc, char** argv)
5 {
6 char** tokens;
7 char * pch;
8 int i=0;
9 pch = strtok (argv[1],"/");
10 while (pch != NULL)
11 {
12 if(pch!=NULL)
13 tokens=pch;
14 i++;
15 printf ("%s\n",pch);
16 pch = strtok (NULL, "/");
17 }
18 printf("i=%d\n",i);
19 return 0;
20 }
****************************************
 
M

Mark McIntyre

I received a segfault error in a very short program for test purpose.
I found no luck even if I read some tricks about tracking down the
segfault. Can any one give me a hint which line of the following code
caused the problem?
6 char** tokens;

tokens is a pointer to a pointer.
13 tokens=pch;


....but you didn't allocate any memory for tokens. Therefore tokens
is memory that doesn't belong to you -> your programme will error.

Decide how many tokens you expect to get and then do

tokens = malloc(SOMESIZE * sizeof *tokens);

before using it...


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
S

santosh

william said:
I received a segfault error in a very short program for test purpose.
I found no luck even if I read some tricks about tracking down the
segfault. Can any one give me a hint which line of the following code
caused the problem?
******************************************
1 #include <stdio.h>
2 #include <string.h>
3
4 int main (int argc, char** argv)
5 {
6 char** tokens;
7 char * pch;
8 int i=0;
9 pch = strtok (argv[1],"/");
10 while (pch != NULL)
11 {
12 if(pch!=NULL)
13 tokens=pch;


tokens is a pointer to pointer of type char. But you've not yet
initialised it to point to one or more char pointers. Yet here you're
attempting to use it as if it were pointing to the start of a valid
array of char pointers. You're accessing memory you don't own and thus
the error. Either initialise tokens or set it to point to one or more
char pointers later like this:

tokens = malloc(NO_OF_ELEMENTS * sizeof *tokens);
14 i++;
15 printf ("%s\n",pch);
16 pch = strtok (NULL, "/");
17 }
18 printf("i=%d\n",i);
19 return 0;
20 }
****************************************

PS. On a different note, don't include line numbers in the code you
post. If others want to feed your code to a compiler to see the
results, it'll be a real nuisance to remove them.
 
W

william

I received a segfault error in a very short program for test purpose.
I found no luck even if I read some tricks about tracking down the
segfault. Can any one give me a hint which line of the following code
caused the problem?
6 char** tokens;

tokens is a pointer to a pointer.
13 tokens=pch;


...but you didn't allocate any memory for tokens. Therefore tokens
is memory that doesn't belong to you -> your programme will error.

Decide how many tokens you expect to get and then do

tokens = malloc(SOMESIZE * sizeof *tokens);

before using it...

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

Thank you for the answer. Debugging is much harder than I wrote the
code, that's totally true.

Ji
 
C

CBFalconer

william said:
I received a segfault error in a very short program for test purpose.
I found no luck even if I read some tricks about tracking down the
segfault. Can any one give me a hint which line of the following code
caused the problem?
******************************************
1 #include <stdio.h>
2 #include <string.h>
3
4 int main (int argc, char** argv)
5 {
6 char** tokens;
7 char * pch;
8 int i=0;
9 pch = strtok (argv[1],"/");
10 while (pch != NULL)
11 {
12 if(pch!=NULL)
13 tokens=pch;
14 i++;
15 printf ("%s\n",pch);
16 pch = strtok (NULL, "/");
17 }
18 printf("i=%d\n",i);
19 return 0;
20 }
****************************************


Change line 6 to "char *tokens[MAXTKNS];" and after line 2 add
"#define MAXTKNS 123" (picking an appropriate value). Replace line
12 with "if (MAXTKNS == i) break;".

Think about why I suggest the above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top