1

Possible Duplicate:
What is the difference between char a[] = “string”; and char *p = “string”;

Will the array version allocate the array memory, so a 100 byte string will use 100 bytes on the constant section and 100 on the static array, or will it use only 100 bytes total? And the pointer version, will it allocate the word size for the pointer besides the 100 bytes of the string, or will the pointer be optimized to the constant section address altogether?

Community
  • 1
  • 1
Spidey
  • 2,508
  • 2
  • 28
  • 38
  • The `static` governs the linkage and storage in addition to what the above marked duplicate says. – Alok Save Jan 12 '13 at 14:07
  • This is not the same question, as the static modifier can result in optimizations in the memory allocation. – Spidey Jan 12 '13 at 14:59

1 Answers1

1

If you use a common computer, with a .rodata section:

#include <stdio.h>

static const char *s = /* string of 100 characters */;

int main(void)
{
  puts(s);
  return 0;
}

It allocates 100 + sizeof(char *) bytes in the .rodata section.

#include <stdio.h>

static const char s[100] = /* string of 100 characters */;

int main(void)
{
  puts(s);
  return 0;
}

It allocates 100 bytes in the .rodata section.

md5
  • 23,373
  • 3
  • 44
  • 93