Pelles C forum

C language => Beginner questions => Topic started by: Gary Willoughby on December 30, 2009, 07:09:41 PM

Title: Where is this string allocated?
Post by: Gary Willoughby on December 30, 2009, 07:09:41 PM
When i define a pointer like this:


char *a = "Hello World";


Where does it place the string? On the heap?
Title: Re: Where is this string allocated?
Post by: JohnF on December 30, 2009, 10:21:50 PM
Quote from: Gary Willoughby on December 30, 2009, 07:09:41 PM
When i define a pointer like this:


char *a = "Hello World";


Where does it place the string? On the heap?

Normally one doesn't need to know. A string literal is held in read only memory, somewhere. Maybe someone else knows exactly where it is, I don't.

John


Title: Re: Where is this string allocated?
Post by: AlexN on January 03, 2010, 07:38:33 PM
Quote from: Gary Willoughby on December 30, 2009, 07:09:41 PM
When i define a pointer like this:


char *a = "Hello World";


Where does it place the string? On the heap?
If you really want to know it, you can translate you source with an option like /Tx86-asm. Then you can look in the generated assembler source in witch section your string is stored.
Title: Re: Where is this string allocated?
Post by: frankie on January 05, 2010, 11:19:12 PM
You are creating an initialized variable, even if you use its address not its value, and is placed in ".rdata".
You can force the creation of initialized data in the code section with the compiler directive "/cbstring" normally used for drivers compilation.
You can also create your sections and place data there usinge the pragma "data_seg( [ "name" ] )".
Read the help file you'll find a lot of info.