segfault on strtok

F

Fatih Gey

Hi,

.. following causes a segfault. .. didn't
know why ?!

int main() {
char name[15];
strcpy (name, "ab8bc8cd8ed");

char cur[800];
strcpy (cur, strtok(name, "8"));

while (cur) {
printf ("Output: %s\n", cur);
printf ("Stringlength %i\n", strlen(cur));
strcpy(cur,strtok(0, "8"));
printf ("next\n");
}

}

output: -----------------------

Output: ab
Stringlength 2
next
Output: bc
Stringlength 2
next
Output: cd
Stringlength 2
next
Output: ed
Stringlength 2
Segmentationfault
 
A

Andreas Kahari

[cut]

#include <stdio.h>
#include said:
int main() {
char name[15];
strcpy (name, "ab8bc8cd8ed");

char cur[800];
strcpy (cur, strtok(name, "8"));

while (cur) {

cur is never going to be zero.
printf ("Output: %s\n", cur);
printf ("Stringlength %i\n", strlen(cur));
strcpy(cur,strtok(0, "8"));

(strictly speaking, the zero on that line should be a null
pointer)
printf ("next\n");
}

}


Check the return value of strtok(). It returns NULL when no
more tokens remains. NULL is defined in <stdlib.h>.

Cheers,
Andreas
 
K

Kevin Goodsell

Andreas said:
(strictly speaking, the zero on that line should be a null
pointer)

0 converted to a pointer *is* a null pointer. There's nothing wrong with
using 0 here.

-Kevin
 
P

pete

Andreas said:
[cut]

#include <stdio.h>
#include <string.h>
NULL is defined in <stdlib.h>.

NULL is also defined in stdio.h and string.h,
as well as in other header files.

stddef.h has the NULL macro and the _t typedefs.
NULL and all of the other macros and typedefs,
are also defined in each header file that has a prototype
for any function, which is defined as either
being able to accept them [NULL and friends], as arguments,
or return them as values.
 
A

Andreas Kahari

Andreas Kahari wrote: [cut]
(strictly speaking, the zero on that line should be a null
pointer)

0 converted to a pointer *is* a null pointer. There's nothing wrong with
using 0 here.

Yes, the integer zero is a null pointer, but only if it's cast
to void * and converted into a pointer type. Besides, it looks
better if one says NULL where one means NULL.
 
S

Sheldon Simms

Yes, the integer zero is a null pointer, but only if it's cast
to void * and converted into a pointer type.

The first argument to strtok() above is not "the integer zero". It
is a null pointer constant and there is no need to cast it to
void *

In fact, NULL is often defined like this:

#define NULL 0
Besides, it looks better if one says NULL where one means NULL.

That is a matter of opinion.
 
F

Fatih Gey

#include <stdio.h>
#include said:
int main() {
char name[15];
strcpy (name, "ab8bc8cd8ed");

char cur[800];
strcpy (cur, strtok(name, "8"));

while (cur) {


cur is never going to be zero.

printf ("Output: %s\n", cur);
printf ("Stringlength %i\n", strlen(cur));
strcpy(cur,strtok(0, "8"));


(strictly speaking, the zero on that line should be a null
pointer)

printf ("next\n");
}

}



Check the return value of strtok(). It returns NULL when no
more tokens remains. NULL is defined in <stdlib.h>.
ok .. i added the both include-lines, changed
while (cur) {
in
while (cur != NULL) {
and replaced 0 with NULL in subsequenced strtok-func, but it keeps
producing segfaults :-(

I think, the problem causes when while run last time and strtok returns
NULL. Is there anything wrong with strcpy-ing this NULL pointer to var
"cur" ? (see also the output-wrap in first msg of thread)


Fatih Gey
 
A

Andreas Kahari

Fatih Gey wrote: said:
ok .. i added the both include-lines, changed
while (cur) {
in
while (cur != NULL) {
and replaced 0 with NULL in subsequenced strtok-func, but it keeps
producing segfaults :-(

I think, the problem causes when while run last time and strtok returns
NULL. Is there anything wrong with strcpy-ing this NULL pointer to var
"cur" ? (see also the output-wrap in first msg of thread)

Yes, that's a problem since strcpy() will probably try to
dereference the opinters given to it, and you can't dereference
a NULL pointer. Assign the retun value from strtok() to a char
pointer and check its value before using it with strcpy().
 
P

pete

Andreas Kahari wrote:
Yes, the integer zero is a null pointer, but only if it's cast
to void * and converted into a pointer type.

A constant expression with a value of zero,
is called a "null pointer constant"
(you can find that phrase in the standard),
whther or not it's cast to (void*).
 
A

Al Bowers

Fatih said:
Hi,

.. following causes a segfault. .. didn't
know why ?!

int main() {
char name[15];
strcpy (name, "ab8bc8cd8ed");

char cur[800];
strcpy (cur, strtok(name, "8"));

while (cur) {
printf ("Output: %s\n", cur);
printf ("Stringlength %i\n", strlen(cur));
strcpy(cur,strtok(0, "8"));
printf ("next\n");
}

}

Once all the delimiters, '8' in this case, are exhausted, function
strtok will return NULL. At the point of the seg fault your
statement:
strcpy(cur,strtok(0,"8"));
becomes
strcpy(cur,NULL);
The second argument NULL is not defined for function strcpy.
The argument must be a string. Therein lies your seg fault.

Actually, if your intent is to only print the tokens, you should
just use a char * instead of using the character array cur.

#include <string.h>
#include <stdio.h>

int main(void)
{
char name[15], *cur;

strcpy (name, "ab8bc8cd8ed");
cur = strtok(name, "8");
while(cur)
{
printf ("Output: %s\n", cur);
printf ("Stringlength %u\n", strlen(cur));
printf ("next\n");
cur = strtok(NULL,"8");
}
}
 
A

Andreas Kahari

A constant expression with a value of zero,
is called a "null pointer constant"
(you can find that phrase in the standard),
whther or not it's cast to (void*).

I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.
 
P

pete

Andreas said:
I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.

I read it differently from the way that you do.

N869
6.3.2.3 Pointers
[#3]
An integer constant expression with the value 0,
or such an expression cast to type void *,
is called a null pointer constant.
 
A

Andreas Kahari

Andreas Kahari wrote: [cut]
I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.
I read it differently from the way that you do.

N869
6.3.2.3 Pointers
[#3]
An integer constant expression with the value 0,
or such an expression cast to type void *,
is called a null pointer constant.


That's the draft standard. I have the book version of the
current standard, but it's at home at the moment. I'll have
to get back to this issue later tonight if noone else has the
current standard at hand and can verify this.


Cheers,
Andreas
 
A

Al Bowers

Andreas said:
I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.

I believe what you are referring to in the standard is the
section 6.3.2.3 Pointers.
Paragraph:
3. An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer constant.

Notice the word "or". It does not say "only if cast".
Among other posibilities, NULL may be defined as:
0, 0L, 0LL, or (void *)0.
 
P

Paul Hsieh

Fatih Gey said:
...
char cur[800];
strcpy (cur, strtok(name, "8"));

while (cur) {

This is a constant loop -- i.e., the loop will never end.
printf ("Output: %s\n", cur);
printf ("Stringlength %i\n", strlen(cur));
strcpy(cur,strtok(0, "8"));

strtok eventually will return NULL. strcpy cannot accept a parameter which is NULL.
 
S

Sheldon Simms

Andreas Kahari wrote: [cut]
I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.
I read it differently from the way that you do.

N869
6.3.2.3 Pointers
[#3]
An integer constant expression with the value 0,
or such an expression cast to type void *,
is called a null pointer constant.


That's the draft standard. I have the book version of the
current standard, but it's at home at the moment. I'll have
to get back to this issue later tonight if noone else has the
current standard at hand and can verify this.

ISO/IEC 9899:1999
6.3.2.3 Pointers
3 An integer constant expression with the value 0, or such an
expressioncast to type void *, is called a null pointer
constant.
 
A

Andreas Kahari

ISO/IEC 9899:1999
6.3.2.3 Pointers
3 An integer constant expression with the value 0, or such an
expressioncast to type void *, is called a null pointer
constant.


Yeah yeah, alright.

I wonder what they mean by "or such an expression" though...
 
D

Dan Pop

In said:
Andreas Kahari wrote: [cut]
I was looking at the standard when I wrote it. It says that an
integer which is zero is a null pointer constant if cast to void *.
That null pointer constant is a null pointer if converted to
a pointer type.
I read it differently from the way that you do.

N869
6.3.2.3 Pointers
[#3]
An integer constant expression with the value 0,
or such an expression cast to type void *,
is called a null pointer constant.

That's the draft standard.

It doesn't matter: the text hasn't changed at least since the last
public C89 draft, released 15 years ago.

It makes sense to object to N869 quotations *only* when you know that
either C89 or C99 say something different.

Dan
 
A

Andreas Kahari

In <[email protected]> Andreas


It doesn't matter: the text hasn't changed at least since the last
public C89 draft, released 15 years ago.

It makes sense to object to N869 quotations *only* when you know that
either C89 or C99 say something different.


I agree, but I thought I remembered the text differently.
Others proved me wrong though.
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top