Jump to content
  • Advertisement
Sign in to follow this  
darkzerox

define vs typedef

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by darkzerox
ah. 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 this post


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

No. Why would it?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I LOVE MACROS!!!! :P I use them in every game i create for example
#define SPRITE_BALL 0
#define SPRITE_PADDLE 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 this post


Link to 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 this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I LOVE MACROS!!!! :P I use them in every game i create for example
#define SPRITE_BALL 0
#define SPRITE_PADDLE 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


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by pragma Fury
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;

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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!