473,796 Members | 2,618 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
40 3526
On Sun, 26 Oct 2003 23:06:08 +0000, Mark McIntyre wrote:
On Sat, 25 Oct 2003 19:22:11 -0400, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
It seems like you don't really "get" the fact...
I "get" it perfectly. Its just that zero is an integer, and letting
the compiler implicitly convert it to a void* so that you can compare
to a pointer, while syntactically fine, doesn't sit well with me.


The compiler does the same thing whether or not you use the NULL
macro: It recognizes a null pointer constant and converts it to a
null pointer if used in a pointer context. If not used in a pointer
context, the 0 is an integer constant. Using NULL instead of 0
doesn't affect this at all.
Anyway, YMMV
True.
HAND, OAO.


Roger.

Nov 13 '05 #31
On Sun, 26 Oct 2003 20:13:06 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Sun, 26 Oct 2003 23:06:08 +0000, Mark McIntyre wrote:
On Sat, 25 Oct 2003 19:22:11 -0400, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
It seems like you don't really "get" the fact...


I "get" it perfectly. Its just that zero is an integer, and letting
the compiler implicitly convert it to a void* so that you can compare
to a pointer, while syntactically fine, doesn't sit well with me.


The compiler does the same thing whether or not you use the NULL
macro:


You miss the point. This is nothing to do with the compiler. The
compiler probably also recognises
int* p = ((long)100000 - (long)pow(10,5) );
as meaning "set p to a null pointer"
that doesn't mean to say its a good idea to use this definition of
NULL, purely from a point of view of clarity of code.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #32
Mark McIntyre wrote:
You miss the point. This is nothing to do with the compiler. The
compiler probably also recognises
int* p = ((long)100000 - (long)pow(10,5) );
as meaning "set p to a null pointer"


I doubt it. That doesn't look much an integer constant expression.

Jeremy.
Nov 13 '05 #33
On Mon, 27 Oct 2003 22:47:55 +0000, Mark McIntyre wrote:
On Sun, 26 Oct 2003 20:13:06 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Sun, 26 Oct 2003 23:06:08 +0000, Mark McIntyre wrote:
On Sat, 25 Oct 2003 19:22:11 -0400, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:

It seems like you don't really "get" the fact...

I "get" it perfectly. Its just that zero is an integer, and letting
the compiler implicitly convert it to a void* so that you can compare
to a pointer, while syntactically fine, doesn't sit well with me.


The compiler does the same thing whether or not you use the NULL
macro:


You miss the point. This is nothing to do with the compiler.


Then why did you mention it?

Nov 13 '05 #34
On 27 Oct 2003 23:16:22 GMT, in comp.lang.c , Jeremy Yallop
<je****@jdyallo p.freeserve.co. uk> wrote:
Mark McIntyre wrote:
You miss the point. This is nothing to do with the compiler. The
compiler probably also recognises
int* p = ((long)100000 - (long)pow(10,5) );
as meaning "set p to a null pointer"


I doubt it. That doesn't look much an integer constant expression.


depends how pow is implemented :-)

but you're right. replace pow with 10*10*10*10*10

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #35
On Mon, 27 Oct 2003 19:48:41 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Mon, 27 Oct 2003 22:47:55 +0000, Mark McIntyre wrote:
On Sun, 26 Oct 2003 20:13:06 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Sun, 26 Oct 2003 23:06:08 +0000, Mark McIntyre wrote:
The compiler does the same thing whether or not you use the NULL
macro:


You miss the point. This is nothing to do with the compiler.


Then why did you mention it?


I didn't mention the compiler, you brought that up.

Shall we continue to psephologise?
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #36

Restoring Context...

On Tue, 28 Oct 2003 18:51:00 +0000, Mark McIntyre wrote:
On Mon, 27 Oct 2003 19:48:41 -0500, Sheldon Simms wrote:
On Mon, 27 Oct 2003 22:47:55 +0000, Mark McIntyre wrote:
On Sun, 26 Oct 2003 20:13:06 -0500, Sheldon Simms wrote:

On Sun, 26 Oct 2003 23:06:08 +0000, Mark McIntyre wrote:

>On Sat, 25 Oct 2003 19:22:11 -0400, Sheldon Simms wrote:
>
>>On Sat, 25 Oct 2003 23:02:12 +0100, Mark McIntyre wrote:
>>
>>> *shrug*. For me, I find it counterintuitiv e to say "if pointer is
>>> zero" when I mean "if pointer is a null pointer", since pointers
>>> aren't numbers, they're pointers.
>>
>>It seems like you don't really "get" the fact that
>
> I "get" it perfectly. Its just that zero is an integer, and
> letting the compiler implicitly convert it to a void* so that ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^> you can compare to a pointer, while syntactically fine, doesn't ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^> sit well with me. ^^^^^^^^^^^^^^^ ^
The compiler does the same thing whether or not you use the NULL
macro:

You miss the point. This is nothing to do with the compiler.
Then why did you mention it?


I didn't mention the compiler, you brought that up.


No I didn't. You said that letting the compiler do a conversion
"doesn't sit well with you". I just pointed out that the compiler
does the same thing whether you use the null pointer constant 0,
or you use the macro NULL, because the macro NULL contains a
null pointer constant with the textual representation "0".
Shall we continue to psephologise?


Well since we aren't psephologizing (if there is such a word),
we can't continue. Why would we want to study elections in
comp.lang.c anyway?
Nov 13 '05 #37
On Tue, 28 Oct 2003 20:47:41 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:

Restoring Context...

On Tue, 28 Oct 2003 18:51:00 +0000, Mark McIntyre wrote:
On Mon, 27 Oct 2003 19:48:41 -0500, Sheldon Simms wrote:
On Mon, 27 Oct 2003 22:47:55 +0000, Mark McIntyre wrote:

On Sun, 26 Oct 2003 20:13:06 -0500, Sheldon Simms wrote:

>On Sun, 26 Oct 2003 23:06:08 +0000, Mark McIntyre wrote:
>
>>On Sat, 25 Oct 2003 19:22:11 -0400, Sheldon Simms wrote:
>>
>>>On Sat, 25 Oct 2003 23:02:12 +0100, Mark McIntyre wrote:
>>>
>>>> *shrug*. For me, I find it counterintuitiv e to say "if pointer is
>>>> zero" when I mean "if pointer is a null pointer", since pointers
>>>> aren't numbers, they're pointers.
>>>
>>>It seems like you don't really "get" the fact that
>>
>> I "get" it perfectly. Its just that zero is an integer, and
>> letting the compiler implicitly convert it to a void* so that ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^>> you can compare to a pointer, while syntactically fine, doesn't ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^>> sit well with me. ^^^^^^^^^^^^^^^ ^>
>The compiler does the same thing whether or not you use the NULL
>macro:

You miss the point. This is nothing to do with the compiler.

Then why did you mention it?
I didn't mention the compiler, you brought that up.


No I didn't. You said that letting the compiler do a conversion
"doesn't sit well with you".


So? I didn't say that my point had anything to do with the compiler.

I just pointed out
yada yada. Nobody's listening to us.

We disagree, I happen to think that its needlessly obfuscating to use
zero to mean a null pointer when there's a perfectly good macro
provided for it. You disagree for no obvious reason I can discern.
Enough.
Shall we continue to psephologise?

Well since we aren't psephologizing (if there is such a word),
we can't continue. Why would we want to study elections in
comp.lang.c anyway?


Perhaps filibustering is closer to the mark. We're both trying to talk
the other into the ground, without actually forcing any conclusion,
because there /is/ no conclusion.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #38
Mark McIntyre <ma**********@s pamcop.net> wrote:
Sheldon Simms <sh**********@y ahoo.com> wrote:
<snip>yada yada. Nobody's listening to us.
Not true, it's kind of amusing. ;-)

<snip>
Shall we continue to psephologise?

<snip>
Perhaps filibustering is closer to the mark.

<snip>

And one can learn interesting words, too. ;-)

--
Irrwahn,
prefers NULL over 0 in pointer contexts.
Nov 13 '05 #39
On Thu, 30 Oct 2003 00:29:03 +0100, Irrwahn Grausewitz wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote:
Sheldon Simms <sh**********@y ahoo.com> wrote:


<snip>
yada yada. Nobody's listening to us.


Not true, it's kind of amusing. ;-)


And besides, the thread reaches only halfway to the
the right side of the window. It would be a shame to
quit now...

Nov 13 '05 #40

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

Similar topics

7
5966
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
4927
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
1932
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
2736
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
2588
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
17179
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
2361
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
9531
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
10459
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...
1
10187
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
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
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
7553
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
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4120
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
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.