Raised This Month: $51 Target: $400
 12% 

#define or new const


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 07-21-2010 , 03:29   #define or new const
Reply With Quote #1

I keep seeing all sorts of opinions regarding the best way of storing repeated strings... I need this clarified :}

So, #define or new const ?
__________________
Hunter-Digital is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 07-21-2010 , 03:45   Re: #define or new const
Reply With Quote #2

const.
Better.
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-21-2010 , 11:35   Re: #define or new const
Reply With Quote #3

I agree with ot. When you use a preprocessor define you are essentially 'hard coding' it into your plugin source every single time you use it. If you use a const then it is only stored once. So, if you use a string 10 times you are using 10 times the memory when using a define.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-21-2010 , 13:56   Re: #define or new const
Reply With Quote #4

Just in addition to fysiks post:

If a value is only being used once then it doesn't matter since the compiler will simply replace the #define symbol in your code with the value; using a const instead of a #define will provide no benefit since a const will store the value in memory just one time identical to how #define would with one usage. If the value is used more than once using #define, you will be consuming X times the amount of memory. With a const, the value is stored in a variable so the same memory is referenced each time it is used in your code.
__________________
Bugsy is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 07-21-2010 , 21:15   Re: #define or new const
Reply With Quote #5

I was living a lie !!!! well, thanks for replies

Hmm... also, is there any diff between "new const" and just "const" ?
__________________
Hunter-Digital is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-21-2010 , 21:21   Re: #define or new const
Reply With Quote #6

No difference, but new is required when defining an array.
__________________
Bugsy is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 07-22-2010 , 01:30   Re: #define or new const
Reply With Quote #7

For arrays i understand why const is better, but for integers :

why :

const m_LastHitGroup = 75

would be better than

#define m_LastHitGroup 75

??
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-22-2010 , 08:17   Re: #define or new const
Reply With Quote #8

Quote:
Originally Posted by ConnorMcLeod View Post
For arrays i understand why const is better, but for integers :

why :

const m_LastHitGroup = 75

would be better than

#define m_LastHitGroup 75

??
I don't think there is a difference besides the define having no type (tag checking in Pawn) checking and potential order-of-operations issues (which parenthesis can likely fix). There is plenty of discussion on the net regarding this.
__________________
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-22-2010 , 18:31   Re: #define or new const
Reply With Quote #9

Quote:
Originally Posted by ConnorMcLeod View Post
For arrays i understand why const is better, but for integers :

why :

const m_LastHitGroup = 75

would be better than

#define m_LastHitGroup 75

??
It's still the same logic. It's just not as significant becuase it is only 4 bytes in stead of 4*(strlen + 1).
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-22-2010 , 18:54   Re: #define or new const
Reply With Quote #10

Quote:
Originally Posted by fysiks View Post
It's still the same logic. It's just not as significant becuase it is only 4 bytes in stead of 4*(strlen + 1).
In this case, the question is whether or not a memory address occupies a cell of memory when a variable is used. If so there will be no difference between #define and const for integers.
__________________
Bugsy is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 13:47.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode