0

I have a hard time uderestanding how #define works when combined with pointers.

Here is my pseudocode:

#define ID "28"      // I need to keep it as string

int main()
{
    char * my_id = ID;
    ...
    ...
}

Now what is actually my_id pointing to ? I did not call alloc nor statically allocated memory for my variable so can data under address my_id be overwritten?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Michał
  • 691
  • 1
  • 5
  • 22

3 Answers3

1

A #define just does text substitution. So what you have is equivalent to:

char *my_id = "28";

What this means is that my_id points to the string constant "28". String constants are typically stored in a read-only data section, so there's nothing to allocate/deallocate.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

After preprocessing, your snippet looks like

 char * my_id = "28";

so, my_id points to the string literal "28".

For string literals, it basically is a null terminated character array with static storage duration. But, attempt to modify a(ny) string literal causes undefined behavior.

Quoting C11, chapter §6.4.5, String literals, paragraph 6,

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.78) The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence. [...]

and paragraph 7,

[...] If the program attempts to modify such an array, the behavior is undefined.

So, TL:DR, my_id points to the string literal, or for the sake of accuracy, it holds the address of the first element of the null-terminated array with static storage, initialized with the content "28".

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The string "28" will be stored in a section of your executable. The pointer, my_id, will point to the address of the string "28".

Edited to remove mistaken reference to 'stack'. Thank you commenters for your clarification.

Chris Rouffer
  • 743
  • 5
  • 14
  • 2
    No, string literals are not stored on the stack on any system I've ever worked with. Because they need to be preserved throughout the whole execution of the program. They tend to be stored in either `.rodata` or `.text` sections, or possibly in a dedicated section for string literals. Or do you mean that the pointer itself will likely be a stack variable? Please clarify. – Lundin May 10 '17 at 14:06
  • It totally depends on the platform. The C spec. doesn't require literals to be stored in any particular place. There is a good discussion here: http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go – Chris Rouffer May 10 '17 at 14:18
  • 1
    That would be because the C standard does not even give a name for different kinds of memory, so it is quite irrelevant here. What's relevant is to look at industry de facto standards for memory sections. All computers use similar section names. None stores string literals on the stack because that doesn't make any sense. – Lundin May 10 '17 at 14:25
  • 1
    @ChrisRouffer the moment you mentioned the word "stack", the C standard went out of the picture. You are now talking about one specific implementation which uses a stack and there it cannot place string literals on the stack since it would go out of scope once the stack frame is destroyed. Meaning the content will be open to be overwritten by whatever functions come in later. – Ajay Brahmakshatriya May 10 '17 at 14:32