Storage space of #defines

R

red floyd

LeTubs said:
Hi All

I'm just checking if my assumptions are correct
If I have the following line

#define CLIENT_MSG_HELLO 9

I know the define will be stored in a memory location, but
what will it be stored as ie short, int, unisigned int ?
Are there any general rules or is it platform/OS dependant?

Thanks
David

Your assumptions are incorrect. The define is textually substituted
into the source. There is no storage allocated for it except as
defined by usage context.

e.g.

int x = CLIENT_MSG_HELLO; // an int named x is allocated and initialized
with 9

x = x + CLIENT_MSG_HELLO; // no storage allocated, 9 is added to x

x = f(CLIENT_MSG_HELL0); // the literal 9 is passed to the function f.
 
J

Joona I Palaste

LeTubs <[email protected]> scribbled the following
I'm just checking if my assumptions are correct
If I have the following line
#define CLIENT_MSG_HELLO 9
I know the define will be stored in a memory location, but
what will it be stored as ie short, int, unisigned int ?
Are there any general rules or is it platform/OS dependant?

You have a misconception. Macros (i.e. #defines) aren't stored. All they
are is a formal version of search-and-replace. When the compiler creates
the object file, which is then linked into an executable, there is no
sign of the #define anywhere. It has been replaced by its expansion and
the situation is the same as if the expansion was there all along.
In other words, given your macro definition above, CLIENT_MSG_HELLO is
*completely identical* to the number 9. The storage rules for this
number 9 are the same as for any other number 9 - it depends on the way
it is used. If you assign it into a short, it's stored as a short. If
you assign it into an int, it's stored as an int. As a value by itself,
it's stored as an int.
These are general rules, because they are in fact defined by the
standard. So it does not depend on the platform or OS.
 
?

=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

red floyd said:
Your assumptions are incorrect. The define is textually substituted
into the source. There is no storage allocated for it except as
defined by usage context.

The preprocessor will obviously have to store all macros somewhere
while running, but from the OP's question it doesn't seem like he was
referring to this.
 
L

LeTubs

Hi All

I'm just checking if my assumptions are correct
If I have the following line

#define CLIENT_MSG_HELLO 9

I know the define will be stored in a memory location, but
what will it be stored as ie short, int, unisigned int ?
Are there any general rules or is it platform/OS dependant?

Thanks
David
 
E

Emmanuel Delahaye

LeTubs wrote on 02/08/04 :
I'm just checking if my assumptions are correct
If I have the following line

#define CLIENT_MSG_HELLO 9

I know the define will be stored in a memory location, but
what will it be stored as ie short, int, unisigned int ?

#define A 9 /* int */
#define B 9u /* (or U) unsigned int) */
#define C 9l /* (or L) long */
#define D 9ul /* (or UL) unsigned long */
#define E 9f /* (or F) float */
#define F 9.0 /* double */
#define G "9.0" /* char * */
 
J

j

#define G "9.0" /* char * */

Depends on the use of G. In object context, it is not a pointer to char
but instead an array of type char. Which I am sure you are already
aware of but just overlooked it.
(Just pointing it out incase it gives anyone the wrong idea)
 
K

Keith Thompson

Emmanuel Delahaye said:
LeTubs wrote on 02/08/04 :

#define A 9 /* int */
#define B 9u /* (or U) unsigned int) */
#define C 9l /* (or L) long */
#define D 9ul /* (or UL) unsigned long */
#define E 9f /* (or F) float */
#define F 9.0 /* double */
#define G "9.0" /* char * */

How does that address the OP's question?

None of those macros takes up any space at run time. An invocation of
any of them might (indirectly) cause some memory to be allocated, but
if none of them are invoked, it's exactly as if they didn't exist.
(We're talking about memory space in the running program, of course.)
 
P

Pascal Bourguignon

LeTubs said:
Hi All

I'm just checking if my assumptions are correct
If I have the following line

#define CLIENT_MSG_HELLO 9

I know the define will be stored in a memory location, but
what will it be stored as ie short, int, unisigned int ?
Are there any general rules or is it platform/OS dependant?

Your assumptions are false.

All the lines starting with '#' are only processed by the C
pre-processor. The C compiler don't even see them (but for the
'# line-number file-name' directives).

Try:

cat>sample.c <<EOF
#define CLIENT_MSG_HELLO 9
int main(void) { printf("hello = %d\n",CLIENT_MSG_HELLO); return(0); }
EOF
gcc -E -o /dev/stdout sample.c

To see what's input into the C compiler.



Now, if you're asking how the pre-processor works, the best is to have
alook at tts sources, but it most probably keep the value of the macro
as strings stored in the heap.
 
J

James

Hi All

I'm just checking if my assumptions are correct
If I have the following line

#define CLIENT_MSG_HELLO 9

I know the define will be stored in a memory location,

Obviously the *compiler* will store it in memory (along with the rest
of the source code) - but a #define does not appear in the compiled
program in any way. Instead, every occurrence of 'CLIENT_MSG_HELLO' in
your source code is *replaced* with '9'. So, the following two code
segments are identical in compilation terms:

printf("%d\n",CLIENT_MSG_HELLO);

printf("%d\n",9);
but
what will it be stored as ie short, int, unisigned int ?

No. The compiler doesn't store "CLIENT_MSG_HELLO" anywhere in the
finished binary.

Adding 100,000,000 different #define lines to the start of a program,
assuming your compiler survives the experience, will make no
difference to the finished binary: they're all removed right at the
beginning, and substituted in everywhere the defined term appears.

As Pascal pointed out, you can see this happen using gcc's -E switch:
you'll see your #define line disappear, and every instance of
CLIENT_MSG_HELLO replaced with a 9. Once that's done, your program is
compiled as if you'd just entered a 9 in the first place.


James.
 
K

Karthick S.

LeTubs said:
Hi All

I'm just checking if my assumptions are correct
If I have the following line

#define CLIENT_MSG_HELLO 9

I know the define will be stored in a memory location, but
what will it be stored as ie short, int, unisigned int ?
Are there any general rules or is it platform/OS dependant?

Thanks
David

Not if I am correct. #define is used for stupid (I am sorry if I hurt
someone designing a preprocessor with this word) substitution.
Let us see some preprocessors as examples as the explanation
might be clearer that way:
Case 1:
#define NAME value
Here value is substituted in every location in which NAME is
identified as a separate token(meaning when it is not part of a token
but is the whole token).
Eg: #define PI 3.14159

Case 2:
#define MACRO Expression(s)
Here the expression is substituted in the locations in which the
MACRO is referenced as a separate token. Things like arguments (try
passing a char * for an int) and order of execution (The famous square
problem) are not taken care of.
Eg: #define SQR((n)) ((n) * (n))

Case 3:
#ifdef MACRO1
.... Part1
#else
.... Part2
#endif

#ifndef MACRO1
.... Part1
#else
.... Part2
#endif
Here one of the parts is executed based on which of the macros is
defined. This is similar to the the if...else construct except that
the condition is whether the MACRO is defined or not.
Eg:
#ifndef __STDIO_H__
....
#define __STDIO_H__
#endif

Case 4:
#if CONDN1
.... Part1
#elif CONDN2
.... Part2
#else
.... Part3
#endif
This is exactly like the4e if...else construct in C.

Case 5:
#pragma

This is compiler specific.

(I hope I have not missed any directives)
So as you see there is no way for storage of macros except as Måns
Rullgård ([email protected]) mentioned.

Rgds,
Karthick S.
 
E

Emmanuel Delahaye

Karthick S. wrote on 03/08/04 :
#define NAME value
#ifdef MACRO1
#else
#endif
#ifndef MACRO1
#if CONDN1
#elif CONDN2
#pragma
(I hope I have not missed any directives)

#include
#error...
 
A

Arthur J. O'Dwyer

[Removed c.u.p crosspost; dunno how it got there]

#define str(s) # s
#define xstr(s) str(s)

There's also ##

Those (# and ##) aren't "directives," though, are they? I ask
merely for information. :)

-Arthur
 
P

pete

Arthur J. O'Dwyer wrote:
Those (# and ##) aren't "directives," though, are they? I ask
merely for information. :)

N869
6.10 Preprocessing directives
6.10.3.2 The # operator
6.10.3.3 The ## operator
 
E

Emmanuel Delahaye

Måns Rullgård wrote on 04/08/04 :
That's more likely to free some memory.

I don't think so. It's clearly a preprocessor directive. I find it
useful to 'recycle' a macro name (kinda 'local' macro).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top