NO

Author Topic: Where is this string allocated?  (Read 3077 times)

Gary Willoughby

  • Guest
Where is this string allocated?
« on: December 30, 2009, 07:09:41 PM »
When i define a pointer like this:

Code: [Select]
char *a = "Hello World";

Where does it place the string? On the heap?

JohnF

  • Guest
Re: Where is this string allocated?
« Reply #1 on: December 30, 2009, 10:21:50 PM »
When i define a pointer like this:

Code: [Select]
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



Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Where is this string allocated?
« Reply #2 on: January 03, 2010, 07:38:33 PM »
When i define a pointer like this:

Code: [Select]
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.
best regards
 Alex ;)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Where is this string allocated?
« Reply #3 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.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide