UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2004-09-22 02:11 PM

lenna
Member
Registered: 2004-08-21
Posts: 77

Re: size of define

#define X 003

what is the size of X in memory? a char?

in a line like char x = X;
can be a loss of data?

Offline

#2 2004-09-22 03:42 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: size of define

Defines are preprocessor stuff and handled totally different than the rest of your code. With your example, at compile time all instances of 'X' in your code will be replaced with '003', so if you have "sizeof(X)" if will be first translated to "sizeof(003)", and numbers without type are ints by default, so it will be the same as sizeof(int).

Offline

#3 2004-09-23 02:05 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: size of define

A #define does exactly the same as the editors "find & replace" function.
So your code at this point will act exactly as it would if you'd typed the
value at that point in the first place. Of course you have to watch the
usage of the value, if it doesn't fit into the target variable you might only
get a compiler warning (or none at all). Defines are normally used as a
method to create better readable code by giving the cryptic numeral
constants a face to work with. A SHUT_WR or EPOLLOUT says more than a
mere 1 or 3 in the code. Still, my C++ book tells me to use const
variables instead where possible to allow compiler type checking.

#define can also be used to create (not so) simple macros. These have
several advantages over regular function calls, like being faster at
runtime. However, C++ encourages you to use the inline directive instead
as it is more secure to use and allows the compiler to work its optimising
magic better.

Offline

Board footer

Powered by FluxBB