Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Storage space of #defines

Reply
Thread Tools

Storage space of #defines

 
 
red floyd
Guest
Posts: n/a
 
      08-02-2004
LeTubs wrote:
> 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.
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      08-02-2004
LeTubs <> scribbled the following
on comp.lang.c:
> 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?


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.

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"That's no raisin - it's an ALIEN!"
- Tourist in MTV's Oddities
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=
Guest
Posts: n/a
 
      08-02-2004
red floyd <> writes:

> LeTubs wrote:
>> 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.


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.

--
Måns Rullgård

 
Reply With Quote
 
LeTubs
Guest
Posts: n/a
 
      08-02-2004
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


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-02-2004
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 * */

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

 
Reply With Quote
 
j
Guest
Posts: n/a
 
      08-02-2004

"Emmanuel Delahaye" <> wrote in message

<snip>

> #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)



--
j


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-02-2004
Emmanuel Delahaye <> writes:
> 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 * */


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.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Pascal Bourguignon
Guest
Posts: n/a
 
      08-02-2004
"LeTubs" <> writes:

> 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.

--
__Pascal Bourguignon__ http://www.informatimago.com/

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
 
Reply With Quote
 
James
Guest
Posts: n/a
 
      08-02-2004
On Mon, 02 Aug 2004 16:41:10 GMT, "LeTubs" <>
wrote:

>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.
 
Reply With Quote
 
Karthick S.
Guest
Posts: n/a
 
      08-03-2004
"LeTubs" <> wrote in message news:<G8uPc.551$_>...
> 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 () mentioned.

Rgds,
Karthick S.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to access the external storage unit of storage router =?Utf-8?B?SWduYXRpdXM=?= Wireless Networking 4 11-06-2006 06:40 AM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-24-2003 11:15 AM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57