C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strtok segfaults in CLI but not in GDB

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Pietro Cerutti
Guest





PostPosted: Wed May 16, 2007 9:11 am    Post subject: strtok segfaults in CLI but not in GDB Reply with quote



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>

int main()
{
char *input1 = "Hello, World!";

char *tok;

tok = strtok(input1, " ");
if(tok) printf("%s\n", tok);

tok = strtok(NULL, " ");
if(tok) printf("%s\n", tok);

return(0);

}

### END STRTOK ###


Now, when I run it from the command line, I get a bus error:

### BEGIN COMMAND LINE OUTPUT ###

Quote:
gcc -ggdb -Wall -o strtok strtok.c
./strtok
Bus error (core dumped)

Exit 138

### END COMMAND LINE OUTPUT ###

When I run it step by step in GDB, the program terminates normally:

### BEGIN DEBUGGER OUTPUT ###

Quote:
gdb ./strtok
GNU gdb 6.1.1 [FreeBSD]

[snip]GDB copyright and bla bla[/snip]
(gdb) break main
Breakpoint 1 at 0x8048570: file strtok.c, line 6.
(gdb) run
Starting program: /home/piter/strtok

Breakpoint 1, main () at strtok.c:6
6 char *input1 = "Hello, World!";
(gdb) next
10 tok = strtok(input1, " ");
(gdb)
11 if(tok) printf("%s\n", tok);
(gdb)
Hello,
13 tok = strtok(NULL, " ");
(gdb)
14 if(tok) printf("%s\n", tok);
(gdb)
World!
16 return(0);
(gdb)
18 }
(gdb)
0x08048485 in _start ()
(gdb)
Single stepping until exit from function _start,
which has no line number information.

Program exited normally.
(gdb)

### END DEBUGGER OUTPUT ###

Is there something I'm missing wrt C and/or strtok, or it's rather a
problem related to my environment (in which case I'll be happy to post
in the right newsgroup) ?

Thanx in advance

--
Pietro Cerutti

PGP Public Key ID:
http://gahr.ch/pgp
Back to top
Ian Collins
Guest





PostPosted: Wed May 16, 2007 9:11 am    Post subject: Re: strtok segfaults in CLI but not in GDB Reply with quote



Pietro Cerutti wrote:
Quote:
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

int main()
{
char *input1 = "Hello, World!";

char *tok;

tok = strtok(input1, " ");

strtok alters its input. You are passing it a string literal, modifying
a string literal invokes the demons of undefined behavior. Don't.

--
Ian Collins.
Back to top
Richard Heathfield
Guest





PostPosted: Wed May 16, 2007 9:12 am    Post subject: Re: strtok segfaults in CLI but not in GDB Reply with quote



Pietro Cerutti said:

Quote:
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

int main()
{
char *input1 = "Hello, World!";

char *tok;

tok = strtok(input1, " ");

strtok modifies the string you pass it. You pass it a string literal.
You're not allowed to modify string literals.

Change

char *input1 = "Hello, World!";

to

char input1[] = "Hello, World!";


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Back to top
CBFalconer
Guest





PostPosted: Thu May 17, 2007 8:48 am    Post subject: Re: strtok segfaults in CLI but not in GDB Reply with quote

Pietro Cerutti wrote:
Quote:
Chris Dollin wrote:

You're not allowed to write into a string literal: that gets you
undefined behaviour.

An implementation may just write into the string.

Uh? So you mean that a string literal isn't unmodifiable by
definition?

Undefined behaviour includes working the way you think i should.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net



--
Posted via a free Usenet account from http://www.teranews.com
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.