chris yo
chris yo

Reputation: 1277

How much memory space does Macro definition takes?

I have a lot of unused macros in my code. So, I am wondering.. If a macro is unused, does it takes up memory space in your program?

The type of macros I have are just the basic ones. Example:

#define TEST_ID 0

Upvotes: 5

Views: 3593

Answers (3)

David Ranieri
David Ranieri

Reputation: 41026

No, doesn't takes space until is used, for this two pieces of code:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("%d %s\n", argc, argv[0]);
    return 0;
}

and

#include <stdio.h>

#define TEST_ID 0

int main(int argc, char *argv[])
{
    printf("%d %s\n", argc, argv[0]);
    return 0;
}

The ASM generated with gcc -S is the same.

Upvotes: 2

Eric Z
Eric Z

Reputation: 14515

Macros will be expanded during preprocessing phase so they don't exist in your program. They just take some space in your source code.

Edit:

In response to Barmar's comment, I did some research.

MSVC 2012: In debug build (when all optimizations are disabled, /Od), adding lines of macros won't cause the growth of the size of your program.

GCC: does provide a way to include macro in debugging information as long as you compile your program with a specific flag. See here. (I didn't know that before myself. Thank you, @Barmar, @Sydius)

Upvotes: 6

4simplicity
4simplicity

Reputation: 31

macro is replaced by preprocessor before compilng start. if you define a macro, and doesn't use it, the compiler will never see it.

Upvotes: 1

Related Questions