By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,299 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,299 IT Pros & Developers. It's quick & easy.

Segmentation Fault Error reported by GDB

P: n/a
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[i]=pch;
14 i++;
15 printf ("%s\n",pch);
16 pch = strtok (NULL, "/");
17 }
18 printf("i=%d\n",i);
19 return 0;
20 }
****************************************

Mar 12 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 12 Mar 2007 11:17:52 -0700, in comp.lang.c , "william"
<wi**********@gmail.comwrote:
>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[i]=pch;
....but you didn't allocate any memory for tokens. Therefore tokens[i]
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
Mar 12 '07 #2

P: n/a

william wrote:
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[i]=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.

Mar 12 '07 #3

P: n/a
On Mar 12, 2:31 pm, Mark McIntyre <markmcint...@spamcop.netwrote:
On 12 Mar 2007 11:17:52 -0700, in comp.lang.c , "william"

<william.m...@gmail.comwrote:
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[i]=pch;

...but you didn't allocate any memory for tokens. Therefore tokens[i]
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

Mar 12 '07 #4

P: n/a
william wrote:
>
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[i]=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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.