strtok, bus error
i'm trying to do pretty simple replacement using strtok.
but it looks like i have missed some subtle difference between the two following
char src[] = "hello world #"; char *other = "hello world #";
because if i use "char * other" with strtok it fails with bus error but i use src it works, don't understand what's the difference.
here's the code for strtok.
char delims[] = "#";
char *result = NULL;
// this works
result = strtok( src, delims );
// this doesnot work and give a bus error
// result = strtok(other, delims);
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}what am i missing here ? i thought both way of declaration(mentioned above ) were the same apparently the r not, whats the diff ?
any insight would be appreciated, TIA!
char src[] = "hello world #"; is an automatic variable (char array)
which is both read and write.
char *other = "hello world #"; is a char * (which points to a string) of text allocated in the program's text (Data/BSS) area which is read only.
Quoted 31 lines · Fundu
i'm trying to do pretty simple replacement using strtok.
but it looks like i have missed some subtle difference between the two followingchar src[] = "hello world #";
char *other = "hello world #";because if i use "char * other" with strtok it fails with bus error but i use src it works, don't understand what's the difference.
here's the code for strtok.
char delims[] = "#"; char *result = NULL; // this works result = strtok( src, delims ); // this doesnot work and give a bus error // result = strtok(other, delims); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims ); }what am i missing here ? i thought both way of declaration(mentioned above ) were the same apparently the r not, whats the diff ?
any insight would be appreciated, TIA!
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Bryan <><
Quoted 5 lines · Fundu
i'm trying to do pretty simple replacement using strtok.
but it looks like i have missed some subtle difference between the two followingchar src[] = "hello world #"; char *other = "hello world #";
i don't know if it is a compiler feature (storage behavior into the DATA segment), or a linux kernel feature, or if it is specified in ANSI, but the second way leads to pointing to a _constant_ string. If someone can enlighten...
Quoted 2 lines
because if i use "char * other" with strtok it fails with bus error but i use src it works, don't understand what's the difference. any insight would be appreciated, TIA!
strtok manpage says it has to _write_ into the string pointed to by its first parameter. this is not allowed if the pointer points to a constant string.
then,
you have to point into a string that have been created at runtime.
char* src;
src = malloc (12);
strncpy (src, "hello#world", 11);
...
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Thanks Bryan and Ben.
it makes sense now.
Quoted 68 lines · Bryan Christ
From: Bryan Christ <[email protected]>
Subject: Re: strtok, bus error
To: [email protected]
Cc: [email protected]
Date: Monday, February 16, 2009, 7:57 PM
char src[] = "hello world #"; is an automatic variable (char array)
which is both read and write.
char *other = "hello world #"; is a char * (which points to a string)
of text allocated in the program's text (Data/BSS) area which is read
only.Quoted 1 line · Fundu
i'm trying to do pretty simple replacement using
strtok.
Quoted 1 line
but it looks like i have missed some subtle difference
between the two following
Quoted 5 lines
char src[] = "hello world #";
char *other = "hello world #";because if i use "char * other" with strtok
it fails with bus error but i use src it works, don't understand what's the difference.
Quoted 10 lines
here's the code for strtok.
char delims[] = "#"; char *result = NULL; // this works result = strtok( src, delims ); // this doesnot work and give a bus error // result = strtok(other, delims); while( result != NULL ) { printf( "result is\"%s\"\n", result );
Quoted 4 lines
result = strtok( NULL, delims ); }what am i missing here ? i thought both way of
declaration(mentioned above ) were the same apparently the r not, whats the diff ?
Quoted 7 lines
any insight would be appreciated, TIA!
--
To unsubscribe from this list: send the line"unsubscribe linux-c-programming" in
Quoted 2 lines
the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html
Quoted 1 line
Signature · 8 lines
Bryan <>< -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 10 lines · ben
Quoted 5 lines
i'm trying to do pretty simple replacement using strtok.
but it looks like i have missed some subtle difference between the two followingchar src[] = "hello world #"; char *other = "hello world #";i don't know if it is a compiler feature (storage behavior into the DATA segment), or a linux kernel feature, or if it is specified in ANSI, but the second way leads to pointing to a _constant_ string. If someone can enlighten...
ANSI C says that string literals "may" be read-only. On platforms with memory protection they usually are read-only.
On Linux, string literals are stored in the "rodata" segment, which is read-only, and thus can be shared between all processes which are using a given executable or shared library.
You can list the segments which make up an executable or shared library using "objdump -h", e.g.:
$ objdump -h /bin/ls
/bin/ls: file format elf32-i386 Sections: Idx Name Size VMA LMA File off Algn 0 .interp 00000013 08048174 08048174 00000174 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 1 .note.ABI-tag 00000020 08048188 08048188 00000188 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 2 .hash 00000298 080481a8 080481a8 000001a8 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA ... 12 .text 0000fbb4 08049880 08049880 00001880 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE ... 14 .rodata 00003d3c 08059460 08059460 00011460 2**5 CONTENTS, ALLOC, LOAD, READONLY, DATA ... 23 .data 0000024c 0805f160 0805f160 00016160 2**5 CONTENTS, ALLOC, LOAD, DATA 24 .bss 0000046c 0805f3c0 0805f3c0 000163ac 2**5 ALLOC 25 .comment 00000bd0 00000000 00000000 000163ac 2**0 CONTENTS, READONLY
[snipped]
The main ones are text, rodata, data, and bss.
The text segment holds code, and is read-only and executable (CODE flag).
The others hold static data: global variables, "static" local variables, string literals, and intialisers for automatic (non-"static" local) arrays.
Read-only data (literals, initialisers, "const" variables) goes into the rodata segment, which is read-only.
Mutable variables with explicit initialisers go into the data segment.
Mutable variables without initialisers (i.e. implicitly initialised to zero) go into the bss segment. As the entire bss segment is initially zero, it doesn't need to be stored in the file (this is indicated by the lack of the CONTENTS, LOAD, and CODE/DATA flags).
The other segments tend to be architecture-specific.
Signature · 1 line
Glynn Clements <[email protected]>
thank you for that precise explanation.
objdump is now less cryptic to me...
- ben
Quoted 29 lines · Glynn Clements
Quoted 4 lines · ben
i don't know if it is a compiler feature (storage behavior into the DATA segment), or a linux kernel feature, or if it is specified in ANSI, but the second way leads to pointing to a _constant_ string. If someone can enlighten...
ANSI C says that string literals "may" be read-only. On platforms with memory protection they usually are read-only.
On Linux, string literals are stored in the "rodata" segment, which is read-only, and thus can be shared between all processes which are using a given executable or shared library.
You can list the segments which make up an executable or shared library using "objdump -h"
[snipped]The main ones are text, rodata, data, and bss.
The text segment holds code, and is read-only and executable (CODE flag).
The others hold static data: global variables, "static" local variables, string literals, and intialisers for automatic (non-"static" local) arrays.
Read-only data (literals, initialisers, "const" variables) goes into the rodata segment, which is read-only.
Quoted 8 lines
Mutable variables with explicit initialisers go into the data segment.
Mutable variables without initialisers (i.e. implicitly initialised to zero) go into the bss segment. As the entire bss segment is initially zero, it doesn't need to be stored in the file (this is indicated by the lack of the CONTENTS, LOAD, and CODE/DATA flags).
The other segments tend to be architecture-specific.
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html