 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pietro Cerutti Guest
|
Posted: Wed May 16, 2007 9:11 am Post subject: strtok segfaults in CLI but not in GDB |
|
|
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
|
Posted: Wed May 16, 2007 9:11 am Post subject: Re: strtok segfaults in CLI but not in GDB |
|
|
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
|
Posted: Wed May 16, 2007 9:12 am Post subject: Re: strtok segfaults in CLI but not in GDB |
|
|
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
|
Posted: Thu May 17, 2007 8:48 am Post subject: Re: strtok segfaults in CLI but not in GDB |
|
|
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 |
|
 |
|
|
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
|
|