Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segfault on strtok

Reply
Thread Tools

segfault on strtok

 
 
Fatih Gey
Guest
Posts: n/a
 
      10-23-2003
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
------------------------------

Thanks for any help
Regards
Fatih Gey


 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      10-23-2003
In article <bn9kks$jov$03$(E-Mail Removed)-online.com>, Fatih Gey wrote:
[cut]

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

> 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


--
Andreas Kähäri
 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-23-2003
Andreas Kahari wrote:
> In article <bn9kks$jov$03$(E-Mail Removed)-online.com>, Fatih Gey wrote:
>
>> 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)


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

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-23-2003
Andreas Kahari wrote:
>
> In article <bn9kks$jov$03$(E-Mail Removed)-online.com>, Fatih Gey wrote:
> [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.

--
pete
 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      10-23-2003
In article <(E-Mail Removed)>, pete wrote:
> Andreas Kahari wrote:

[cut]
>> NULL is defined in <stdlib.h>.

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



Ah, yes.


--
Andreas Kähäri
 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      10-23-2003
In article
<OlZlb.3553$(E-Mail Removed) et>, Kevin
Goodsell wrote:
> 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.

--
Andreas Kähäri
 
Reply With Quote
 
Sheldon Simms
Guest
Posts: n/a
 
      10-24-2003
On Thu, 23 Oct 2003 23:35:08 +0000, Andreas Kahari wrote:

> In article
> <OlZlb.3553$(E-Mail Removed) et>, Kevin
> Goodsell wrote:
>> Andreas Kahari wrote:
>>>> strcpy(cur,strtok(0, "8"));
>>> (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.


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.

 
Reply With Quote
 
Fatih Gey
Guest
Posts: n/a
 
      10-24-2003
>
> #include <stdio.h>
> #include <string.h>
>
>>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

 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      10-24-2003
In article <bnak4r$7qi$06$(E-Mail Removed)-online.com>, Fatih Gey wrote:
[cut]
> 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().


--
Andreas Kähäri
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-24-2003
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*).

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invoking a segfault within a segfault handler - is this undefinedbehavior? Andrey Vul C Programming 8 07-30-2010 02:14 PM
strtok() and std::string Alex Vinokur C++ 6 04-14-2005 01:40 PM
Problems with strtok() returning one too many tokens... Adam Balgach C++ 2 11-28-2004 01:12 AM
strtok trouble Robert C Programming 17 09-06-2003 10:30 PM
strtok problem jorntk@yahoo.com C Programming 4 08-29-2003 11:26 AM



Advertisments