Public Group

# define vs typedef

This topic is 4728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

is there any difference in performance or... anything in #define byte unsigned char vs typedef unsigned char byte; ? thanks :)

##### Share on other sites
Yes. The difference is that you should never use macros when you can possibly avoid it. For instance, with the typedef, a programmer could have a variable somewhere named "byte" and it wouldn't cause problems; with the macro, it would be a syntax error. Also, a typedef can be placed in a namespace to avoid polluting the global scope; macros, in contrast, do not obey scoping rules.

The object code produced by the two will be identical.

##### Share on other sites
ah. but it wouldn't be ever so slightly slower, or consume more memory then?

##### Share on other sites
Quote:
 Original post by darkzeroxah. but it wouldn't be ever so slightly slower, or consume more memory then?

They both have absolutely no impact on preformance and do not consume memory. The difference is the former does not respect the semantics of the language (as is the case with the pre-processor) as it simply subsutuites text while the later is part of the language and does respect it.

typedef creates a type alias, it can be used in part of template meta-programming for type computations at compile-time. You can't really do that with the former.

##### Share on other sites
Quote:
 Original post by darkzeroxah. but it wouldn't be ever so slightly slower, or consume more memory then?

No. Why would it?

##### Share on other sites
I LOVE MACROS!!!! :P I use them in every game i create for example
#define SPRITE_BALL 0

and plus I think thats how microsoft defines all of its customized variable type names. and macros are replaced with their value at run time, so its like an inline function, except it holds a value, you should consider useing typedefs in certain instances because of the fact of the previous post

##### Share on other sites
Quote:
 Original post by Anonymous Poster#define SPRITE_BALL 0#define SPRITE_PADDLE 0

O_O

Tell me, AP. Does the term "enumeration" mean anything to you?
Quote:
 and plus I think thats how microsoft defines all of its customized variable type names.

They used to, yes. That is one of the big reasons why the win32api is so ugly to use.
Quote:
 and macros are replaced with their value at run time,
No they aren't.
Quote:
 so its like an inline functio
No it isn't.
Quote:
 except it holds a value
No it doesn't.
Quote:
 you should consider useing typedefs in certain instances because of the fact of the previous post
Hey, good idea.

##### Share on other sites
Quote:
 Original post by Anonymous PosterI LOVE MACROS!!!! :P I use them in every game i create for example#define SPRITE_BALL 0#define SPRITE_PADDLE 0and plus I think thats how microsoft defines all of its customized variable type names. and macros are replaced with their value at run time, so its like an inline function, except it holds a value, you should consider useing typedefs in certain instances because of the fact of the previous post

There is absolutely no reason to do this... You're much better off doing something like:

const static int SPRITE_BALL = 0;
const static int SPRITE_PADDLE = 0;

You'll get exactly the same outcome, and you won't run into any problems because of the #defines.

Edit: And since when was MS's way of doing things the right way? They put a lot of thought into C# and their design for the .NET library interfaces, and you'll note that you can't do this anymore...

##### Share on other sites
Quote:
Original post by Sneftel
Quote:
 and macros are replaced with their value at run time,
No they aren't.
Quote:
 so its like an inline functio
No it isn't.
Quote:
 except it holds a value
No it doesn't.

Quoted for being concise and accurate pwnage.

##### Share on other sites
Quote:
 Original post by pragma FuryThere is absolutely no reason to do this... You're much better off doing something like:const static int SPRITE_BALL = 0;const static int SPRITE_PADDLE = 0;

In C++ you would rather do this (assuming both constants really are to be declared locally):
namespace {    const int SPRITE_BALL = 0;    const int SPRITE_PADDLE = 0;}

[wink]

• 10
• 19
• 14
• 19
• 15