473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Storage space of #defines

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
Nov 14 '05 #1
16 1417
LeTubs <dl*****@yahoonospam.co.uk> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"That's no raisin - it's an ALIEN!"
- Tourist in MTV's Oddities
Nov 14 '05 #2
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.
Nov 14 '05 #3
red floyd <no*****@here.dude> 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
mr*@kth.se
Nov 14 '05 #4
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"

Nov 14 '05 #5
Emmanuel Delahaye <em***@YOURBRAnoos.fr> 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) ks***@mib.org <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.
Nov 14 '05 #6
j

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> 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
Nov 14 '05 #7
"LeTubs" <dl*****@yahoonospam.co.uk> 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
Nov 14 '05 #8
On Mon, 02 Aug 2004 16:41:10 GMT, "LeTubs" <dl*****@yahoonospam.co.uk>
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.
Nov 14 '05 #9
"LeTubs" <dl*****@yahoonospam.co.uk> wrote in message news:<G8***************@newsfe3-gui.ntli.net>...
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 (mr*@kth.se) mentioned.

Rgds,
Karthick S.
Nov 14 '05 #10
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...

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

"C is a sharp tool"

Nov 14 '05 #11
Emmanuel Delahaye wrote:

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


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

There's also ##

--
pete
Nov 14 '05 #12

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

On Tue, 3 Aug 2004, pete wrote:

Emmanuel Delahaye wrote:
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...


#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
Nov 14 '05 #13
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

--
pete
Nov 14 '05 #14
j

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
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...


#line
#undef

--
j
Nov 14 '05 #15
"j" <ja**********@bellsouth.net> writes:
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
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...


#line


#line is passed on to the compiler, which may or may not store it
somewhere.
#undef


That's more likely to free some memory.

--
Måns Rullgård
mr*@kth.se
Nov 14 '05 #16
Måns Rullgård wrote on 04/08/04 :
#undef


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

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

"C is a sharp tool"

Nov 14 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Galina | last post by:
Hello I am trying to create a table, which includes a field type LONG. The wizard hasn't allowed me to enter storage settings for this table. It set the initial size is 64K and not available to...
2
by: Troels Arvin | last post by:
Hello, For some very data-intensive projects it's interesting how much space the DBMS uses for the storage of data, so I'm investigating how space efficient different DBMSes are. In the...
5
by: aneesh | last post by:
Hi all, I have a program, this works fine but if we declare static below "int i" it shows different storage class specifier. what will be the reason. #include <stdlib.h> static int i ; int...
13
by: S.Tobias | last post by:
I'm examining the existence of temporary objects by looking at their addresses. The trick is to create a structure that contains an array as its first member. In an expression the array rvalue...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Group, After a long week end I am back again. Its nice and refreshing after a short vacation so lets get started with .NET once again. Today we will discuss about Isolated Storage. This is...
7
by: StupidScript | last post by:
>From the manual "Storage Requirements": "ENUM('value1','value2',...) =1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)" This seems to mean: "a" = 1 byte...
14
by: Richard Harter | last post by:
Apologies for the length - this post is best viewed with fixed font and a line width >= 72. Below is the source code for a C header file that provides a suite of storage management macros. I am...
4
by: beena | last post by:
All, I'm new to the concept of automatic storage... I'm looking at the database setup by a vendor. I see few tablespaces showing up with automatic storage - Yes. Tablespace ID ...
6
by: cristizaharioiu | last post by:
Hello, I am beginner with db2 ( DB2 v9.1.0.1 running on RHEL 4); this is my first post. I have this error " SQL0968C The file system is full. SQLSTATE=57011"" in my instance configured with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.