473,770 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segfault on strtok

Hi,

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

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

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

while (cur) {
printf ("Output: %s\n", cur);
printf ("Stringleng th %i\n", strlen(cur));
strcpy(cur,strt ok(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
Segmentationfau lt
------------------------------

Thanks for any help
Regards
Fatih Gey
Nov 13 '05 #1
40 3524
In article <bn************ *@news.t-online.com>, Fatih Gey wrote:
[cut]

#include <stdio.h>
#include <string.h>
int main() {
char name[15];
strcpy (name, "ab8bc8cd8e d");

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

while (cur) {
cur is never going to be zero.
printf ("Output: %s\n", cur);
printf ("Stringleng th %i\n", strlen(cur));
strcpy(cur,strt ok(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
Nov 13 '05 #2
Andreas Kahari wrote:
In article <bn************ *@news.t-online.com>, Fatih Gey wrote:
printf ("Output: %s\n", cur);
printf ("Stringleng th %i\n", strlen(cur));
strcpy(cur,strt ok(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.

Nov 13 '05 #3
Andreas Kahari wrote:

In article <bn************ *@news.t-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
Nov 13 '05 #4
In article <3F***********@ mindspring.com> , 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
Nov 13 '05 #5
In article
<Ol************ ****@newsread4. news.pas.earthl ink.net>, 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
Nov 13 '05 #6
On Thu, 23 Oct 2003 23:35:08 +0000, Andreas Kahari wrote:
In article
<Ol************ ****@newsread4. news.pas.earthl ink.net>, Kevin
Goodsell wrote:
Andreas Kahari wrote:
strcpy(cur,strt ok(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.

Nov 13 '05 #7
>
#include <stdio.h>
#include <string.h>
int main() {
char name[15];
strcpy (name, "ab8bc8cd8e d");

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

while (cur) {

cur is never going to be zero.

printf ("Output: %s\n", cur);
printf ("Stringleng th %i\n", strlen(cur));
strcpy(cur,strt ok(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

Nov 13 '05 #8
In article <bn************ *@news.t-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
Nov 13 '05 #9
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
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5965
by: James Leddy | last post by:
For some reason, I get a segmentation fault when I exit this program I made. Most of the time when I have seen this it has occured in the middle of the program and the program terminates. However, in this instance, the program gets a segmentation fault on exit. I already posted this question, but people were asking for some code, so here it is: #define DWORD unsigned long
13
4926
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
8
1930
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello dreams." #include <stdafx.h> #include <string.h> #include <stdio.h> int main()
4
2735
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is somehow confused or upset. I have the following code, which I am using to tokenise some input which is in th form x:y:1.2: int tokenize_input(Sale *sale, char *string){
3
3811
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include <string> #include <stdlib.h> using namespace std;
29
2587
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
17177
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
2360
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC) 3.4.5 (mingw special) I would like there to be a default, to be returned when two delimiters
2
1295
by: aviraldg | last post by:
I'm having a SEGFAULT on the following program. Weird thing is that it happens randomly with different "str"s (ie. user input; check main() for more info) and my debugger shows me that the getToken function is working and returning the correct answer. #include <stdio.h> #include <string.h> #include <ctype.h> char* getToken(char *src, unsigned int pos=0) { char *token=new char; unsigned int i=0;
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.