3

Does a macro allocate memory in a segment? If yes, in which memory segment NUMBER is stored?

#define NUMBER 10
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    From: https://gcc.gnu.org/onlinedocs/cpp/Object-like-Macros.html#Object-like-Macros. _"An object-like macro is a simple identifier which will be replaced by a code fragment...then the C preprocessor will recognize and expand the macro...C compiler will see the same tokens as it would if you had written"_ – Adriano Repetti Jul 05 '16 at 06:58
  • 1
    `NUMBER` allocates dynamic memory at compile time (in compiler's heap). There is no allocation in compiled program - at least not for the macro. – i486 Jul 05 '16 at 07:00
  • Well, to be pedantic it will occupy memory into `.text` ([code segment](https://en.wikipedia.org/wiki/Code_segment)) section. ;) For each time is it used in code. – LPs Jul 05 '16 at 07:04
  • 2
    http://stackoverflow.com/questions/31261207/how-memory-is-allocated-to-macros-in-c – nunojsa Jul 05 '16 at 07:07

6 Answers6

7

No, macros don't work like that.

They are substituted by the preprocessor. The compiler doesn't get to see NUMBER. All it will see is the literal 10.

In this respect, C and C++ are identical.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

No memory is allocated for the macro. When compiling the code, the macro is mechanically replaced with its value in all places it is used.

martinkunev
  • 1,364
  • 18
  • 39
2

No, Macro does not allocate the memory.

These statements are not like variable assignment, no memory is allocated. Remember, the preprocessor acts before compilation.

NUMBER is relevant only in pre-processing stage and does not have any identity at run time. During pre-processing all instances of NUMBER are replaced with 10. so there is no memory requirement at run time because 10 is an integer literal(Compile time).

Please, read reference link.

LPs
  • 16,045
  • 8
  • 30
  • 61
msc
  • 33,420
  • 29
  • 119
  • 214
2

No, in general, a #define directive does not allocate any memory as such.

To elaborate, #define is a replacement directive. All it does is a textual replacement during the preprocessing stage.

So, a textual code like

#define NUMBER 10
.
.
.
if (val == NUMBER)

looks like

if (val == 10)

after preprocessing. So, the MACRO substitution defined by #define does not take any separate memory. FWIW, The replacement (literal) value will take up usual place in the code segment (same as if you've written the later format, with direct litreal value, without a MCARO).

Related, from C11, chapter §5.1.1.2/ p4, Translation phases

Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. [...]

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

This define does not allocate memory in your program.

It just tells the preprocessor (part of the compiler) to replace further instances of the token NUMBER by 10.

It probably allocates some bytes in the preprocessor, though.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
1

A #define by itself does not take up any memory, what happens is the compiler - during pre-compilation stage - replaces all occurrences of NUMBER in your code with the definition, in your case 10.

Ishay Peled
  • 2,783
  • 1
  • 23
  • 37