1

I have a question about how compiler stores C string?Here is some pieces of code:

#define STRING_MACRO   "macro"
const char * string_const = "w";

int main(void){
    printf("%u\n", sizeof(STRING_MACRO));
    printf("%u\n", sizeof(string_const));

    return 0;
}

output:

6

4 -- my system is x86, so it is 4

So I am confused about how compiler stores c string?Is it the same between macro style and value style?

I think most people misunderstanding my question.So I tried another code by myself.

#define TEST "a"

int main(void)
{
    char hello[] = "aa";
    char (*a)[10] = &hello;

    printf("%u\n", sizeof(TEST));
    printf("%u\n", sizeof(hello));
    printf("%u\n", sizeof(*a));

    return 0;
}

output:

2
3
10

So I got a conclusion the compiler store c string of macro style in char[] type, not char * type.

MYMNeo
  • 818
  • 5
  • 9

5 Answers5

3
sizeof(STRING_MACRO);

is seen by the compiler as:

sizeof("macro");

This gives you the size of the string literal "macro", string literals are stored in an implementation defined read only region.


const char * string_const = "w";

string_cost is a pointer which points to a string literal "w".

so,

sizeof(string_const);

gives the size of the pointer i.e const char * which is apprantly 4 on your system.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

The string "macro" is defined as a macro in your code and not as a variable.

If you build your code with gcc -E you will get a prepocessor code. and in this code you will find that

printf("%u\n", sizeof(STRING_MACRO);

is replaced with

printf("%u\n", sizeof("macro"));

the prprocessor code is the code generated by your compilator before the compilation. in this code the compilator replace the macros in your origin code with the content of the macro.

And for "w" is literal string and string_const is a pointer pointing on that literal string. and the sizeof pointer is 4 for 32-bits systems and 8 for 64-bits systems.

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • so what type of the "macro",is it char[]? – MYMNeo Mar 29 '13 at 09:48
  • answer updated. writing in your code `sizeof(STRING_MACRO)` is equivalent to write `sizeof("macro")`. it's the same. macros are not variable. answer updated – MOHAMED Mar 29 '13 at 09:52
2

sizeof() returns the object's memory size.

#define STRING_MACRO   "macro"

This is 6 because the compiler allocates 6 bytes for 'macro' (5) + (1) for the string terminator

const char * string_const = "w";

This is 4 because it's a pointer, and you're working on a 32bit platform, so 4 bytes for the pointer to char.

FrizzTheSnail
  • 1,048
  • 11
  • 21
0

Compiler doesn't store anything. It evaluates the size of the constant during the parsing. Size of STRING_MACRO is evaluated as the length of the string + the terminator character(\0). Size of string_const is evaluated as size of pointer (because that's what it is) and on your system the pointer size is 4 bytes (corresponds to 32-bit system).

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
0

In your second code,

  • TEST is nothing but a name for a piece of code, namely the literal a. This name is expanded to the actual code during the pre-processor stage.
  • All string literals are NULL terminated. This is why,

sizeof("a") =   1   // the size of the character 'a'
              + 1   // size of the '\0' character 
             -----
              = 2

  • "a" and "aa" are stored in read-only parts of memory (implementation defined).
  • hello is stored in the stack and it points to the read-only part of the memory where "aa" is stored.
  • Since hello is declared as a character array (whose size is implicitly 3 since no size was explicitly specified), sizeof(hello) = length of the array = length ("aa\0") = 3.
  • a is a pointer to a character array of length 10.
  • sizeof(a) would be 4 (in your x86 system) since a is a pointer.
  • sizeof(*a) = 10 because *a is the base pointer of the character array of length 10.
Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63