linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* strtok, bus error
@ 2009-02-17  2:52 Fundu
  2009-02-17  3:57 ` Bryan Christ
  2009-02-17  4:04 ` ben
  0 siblings, 2 replies; 6+ messages in thread
From: Fundu @ 2009-02-17  2:52 UTC (permalink / raw)
  To: linux-c-programming

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!


      

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: strtok, bus error
  2009-02-17  2:52 strtok, bus error Fundu
@ 2009-02-17  3:57 ` Bryan Christ
  2009-02-17  6:31   ` Fundu
  2009-02-17  4:04 ` ben
  1 sibling, 1 reply; 6+ messages in thread
From: Bryan Christ @ 2009-02-17  3:57 UTC (permalink / raw)
  To: fundu_1999; +Cc: linux-c-programming

 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.

On Mon, Feb 16, 2009 at 8:52 PM, Fundu <fundu_1999@yahoo.com> wrote:
> 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!
>
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>



-- 
Bryan
<><

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: strtok, bus error
  2009-02-17  2:52 strtok, bus error Fundu
  2009-02-17  3:57 ` Bryan Christ
@ 2009-02-17  4:04 ` ben
  2009-02-17 20:15   ` Glynn Clements
  1 sibling, 1 reply; 6+ messages in thread
From: ben @ 2009-02-17  4:04 UTC (permalink / raw)
  To: linux-c-programming

Fundu a écrit :
> 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 #";

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...

> 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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: strtok, bus error
  2009-02-17  3:57 ` Bryan Christ
@ 2009-02-17  6:31   ` Fundu
  0 siblings, 0 replies; 6+ messages in thread
From: Fundu @ 2009-02-17  6:31 UTC (permalink / raw)
  To: Bryan Christ; +Cc: linux-c-programming

Thanks Bryan and Ben. 
it makes sense now.


--- On Mon, 2/16/09, Bryan Christ <bryan.christ@gmail.com> wrote:

> From: Bryan Christ <bryan.christ@gmail.com>
> Subject: Re: strtok, bus error
> To: fundu_1999@yahoo.com
> Cc: linux-c-programming@vger.kernel.org
> 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.
> 
> On Mon, Feb 16, 2009 at 8:52 PM, Fundu
> <fundu_1999@yahoo.com> wrote:
> > 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!
> >
> >
> >
> > --
> > To unsubscribe from this list: send the line
> "unsubscribe linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at 
> http://vger.kernel.org/majordomo-info.html
> >
> 
> 
> 
> -- 
> Bryan
> <><
> --
> To unsubscribe from this list: send the line
> "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at 
> http://vger.kernel.org/majordomo-info.html


      

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: strtok, bus error
  2009-02-17  4:04 ` ben
@ 2009-02-17 20:15   ` Glynn Clements
  2009-02-17 21:05     ` ben
  0 siblings, 1 reply; 6+ messages in thread
From: Glynn Clements @ 2009-02-17 20:15 UTC (permalink / raw)
  To: ben; +Cc: linux-c-programming


ben wrote:

> > 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 #";
> 
> 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.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: strtok, bus error
  2009-02-17 20:15   ` Glynn Clements
@ 2009-02-17 21:05     ` ben
  0 siblings, 0 replies; 6+ messages in thread
From: ben @ 2009-02-17 21:05 UTC (permalink / raw)
  To: linux-c-programming

thank you for that precise explanation.
objdump is now less cryptic to me...
- ben

Glynn Clements a écrit :
> ben wrote:
>> 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.
> 
> 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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-02-17 21:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-17  2:52 strtok, bus error Fundu
2009-02-17  3:57 ` Bryan Christ
2009-02-17  6:31   ` Fundu
2009-02-17  4:04 ` ben
2009-02-17 20:15   ` Glynn Clements
2009-02-17 21:05     ` ben

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).