3

So if I do:

char * a = "hello";

"hello" is stored in the RODATA section and a points to it. If I do:

char a[10] = "hello";

"hello" is stored on the STACK in an array of 10 bytes called a.

What happens when I do:

char * a[10] = {"hello", "hi"}

So, we have an array of 10 character pointers which will be stored on the STACK. But what about the string literals? Do they go in the RODATA section?
Also, does the same thing happen with argv?

Bruce
  • 33,927
  • 76
  • 174
  • 262
  • 1
    Array pointers reside on stack and the data they pointing at reside in read only locations. Also it is not array of 10 characters but an array of 10 `char *`. Also I prefer `const char * a[10] = {"a","bC"};` – Mahesh Feb 27 '12 at 20:06
  • @Mahesh: Sorry about that. I changed it. – Bruce Feb 27 '12 at 20:10
  • There is nothing to sorry about. What I do sometimes is even worst than this :) – Mahesh Feb 27 '12 at 20:12

6 Answers6

4
char *a[10] = {"hello", "hi"}

The elements of the array are pointers to string literals. As any string literal they are also non-modifiable and the implementation can store them in read-only memory.

*a[0] = 'g';   // undefined behavior, modifying a string literal
a[0] = "bla";  // ok, modifying the pointer

Now for your second question:

Also, does the same thing happen with argv?

No, because they are not string literals. The pointers in argv and the strings pointed to can be modified. This is guaranteed by the C Standard.

(C99, 5.1.2.2.1p2) "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination."

ouah
  • 142,963
  • 15
  • 272
  • 331
  • @Bruce for `gcc` and the ELF format, assuming the array has block scope, the array elements (the pointers to the strings literals) are stored in the stack and the string literals are stored in .rodata section. – ouah Feb 27 '12 at 21:20
3

Well, running through gcc -S:

cnicutar@cvm:~$ head try.S 
    .file   "try.c"
    .text
.Ltext0:
    .section    .rodata
.LC0:
    .string "hello"
.LC1:
    .string "hi"

Alternatively, from the binary:

cnicutar@cvm:~$ objdump -s -j .rodata try

try:     file format elf32-i386

Contents of section .rodata:
 80484e8 03000000 01000200 68656c6c 6f006869  ........hello.hi
 80484f8 00                                   .               

This is the program:

int main()
{
    char * a[10] = {"hello", "hi"};
    (void)a;
    return 0;
}
cnicutar
  • 178,505
  • 25
  • 365
  • 392
2

Your first assumption may be true in your case, but not necessarily true on other platforms and compilers. But for the purposes of your question, let's assume it is.

Your second assumption is incorrect. "hello" is stored in the same place as in the first case. It's just that its characters(and the trailing 0) are copied into a which is on the stack (which is also not a well-defined term as far as C is concerned).

In your third example

char * a[10] = {"hello", "hi"}

"Hello" and "hi" are stored wherever they were stored in the first example, say, RODATA. The pointers to their first characters are stored as the first two elements of a

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
1
char * a[10] = {"hello", "hi"}

So, we have an array of 10 characters

This is not correct. We have 10 (local) pointers to chars (or char arrays). "hello", and "hi" will be stored in the data section like the first example.

perreal
  • 94,503
  • 21
  • 155
  • 181
1

In the third case you have an array of 10 pointers which uses 10 * sizeof(char*) bytes of memory. First two elements of which point to the corresponding constant strings in data segment.

1

There's a lengthy discussion at String literals: Where do they go?

They will be somewhere, with static storage duration. Beyond that, it will depend on your compiler.

You will have, for example (on a machine with 1-byte pointers, since I am lazy)

RODATA:
0x00:    hello0
0x06:    hi0

Stack:
0xA0: 0x00
0xA1: 0x06
0xA2: 0x00
[...]
Community
  • 1
  • 1
David Souther
  • 8,125
  • 2
  • 36
  • 53