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

Stupid question: "const" vs "#define"


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
dFF
sıɹɹoɥɔ ʞɔnu
Join Date: Oct 2009
Old 06-08-2011 , 12:40   Stupid question: "const" vs "#define"
Reply With Quote #1

Which is better to hold some values ( like OFFSET_CSDEATHS = 444 ) const or define ?

PHP Code:
const OFFSET_CSDEATHS 444

or

#define OFFSET_CSDEATHS 444 
?
dFF is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-08-2011 , 12:49   Re: Stupid question: "const" vs "#define"
Reply With Quote #2

If you only use it once in your code, use #define.
Otherwise, use a constant.

This is because defines are basically just place holders until it is compiled.
When it is compiled, the define constants are replaced with their actual definition, so the defines are not really constant variables.

Code example:
Code:
#define VERSION "1.0" public plugin_init() {     register_plugin("Test", VERSION, "Exolent");         log_amx("Server is running v%s", VERSION); }

When compiled, it is translated to:
Code:
public plugin_init() {     register_plugin("Test", "1.0", "Exolent");         log_amx("Server is running v%s", "1.0"); }

The bad thing about this is that those 2 "1.0" strings are in different places of memory, but it is a waste of space since they are the same value.

When you use a constant variable, it is set in a specific place in memory.
When you access that constant variable anywhere, it will use that 1 place in memory.

It is better practice to use constants rather than defines for this reason.
Though, if it is only used once in your code, then a define is okay.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Dr7sTyLe
Senior Member
Join Date: Dec 2010
Old 06-08-2011 , 12:50   Re: Stupid question: "const" vs "#define"
Reply With Quote #3

i prefer define
Dr7sTyLe is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-08-2011 , 12:54   Re: Stupid question: "const" vs "#define"
Reply With Quote #4

Quote:
Originally Posted by Dr7sTyLe View Post
i prefer define
Opinions don't make code better.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Devil259
Veteran Member
Join Date: Dec 2009
Location: France (59)
Old 06-08-2011 , 13:02   Re: Stupid question: "const" vs "#define"
Reply With Quote #5

#define is better because it doesn't use memory -> when the code is compiled, the defines are deleted.
constants use memory because they aren't deleted.
__________________
You can do anything you set your mind to, man.

Devil259 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-08-2011 , 13:52   Re: Stupid question: "const" vs "#define"
Reply With Quote #6

Quote:
Originally Posted by Devil259 View Post
#define is better because it doesn't use memory -> when the code is compiled, the defines are deleted.
constants use memory because they aren't deleted.
You are completely wrong.

All strings, characters, numbers, etc. that are written out but not stored in variables, are still stored in memory.

Example:
Code:
log_amx("Testing"); log_amx("Testing"); log_amx("Testing"); log_amx("Testing");

"Testing" is in memory 4 times because it appears in 4 different places.

Code:
new const string[] = "Testing"; log_amx(string); log_amx(string); log_amx(string); log_amx(string);

Now, "Testing" is only in memory once.

Learn yourself before trying to teach others.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Devil259
Veteran Member
Join Date: Dec 2009
Location: France (59)
Old 06-08-2011 , 14:03   Re: Stupid question: "const" vs "#define"
Reply With Quote #7

Quote:
Originally Posted by Exolent[jNr] View Post
You are completely wrong.

All strings, characters, numbers, etc. that are written out but not stored in variables, are still stored in memory.

Example:
Code:
log_amx("Testing"); log_amx("Testing"); log_amx("Testing"); log_amx("Testing");

"Testing" is in memory 4 times because it appears in 4 different places.

Code:
new const string[] = "Testing"; log_amx(string); log_amx(string); log_amx(string); log_amx(string);

Now, "Testing" is only in memory once.

Learn yourself before trying to teach others.
And where are the defines in this example ? ...
__________________
You can do anything you set your mind to, man.

Devil259 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-08-2011 , 16:01   Re: Stupid question: "const" vs "#define"
Reply With Quote #8

Quote:
Originally Posted by Exolent[jNr] View Post
Opinions don't make code better.
This made me laugh. So true though.
__________________
fysiks is offline
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 08-20-2016 , 00:50   Re: Stupid question: "const" vs "#define"
Reply With Quote #9

Sorry for the necro here, but this is the thread that came up when I performed a google search on the topic.

I'm not buying into the claims being made here, unless some reasonable proof is provided. Typically (I mean in other languages) a #define will be faster than a constant, but if used in excess can bloat the size of the binary, that's the tradeoff. I think this logic is fairly straightforward. However, I replaced all the #defines with constants in one of my plugins, yet instead of shrink the output, the size of the .amxx increased by 7%.

If constants have more restricted usage, increased file size, and slower performance, I don't understand what the benefit is supposed to be, not that an increase of 2kb is going to sway me one way or another, but it does make me question the claims.
__________________
Bad_Bud is offline
dFF
sıɹɹoɥɔ ʞɔnu
Join Date: Oct 2009
Old 06-08-2011 , 13:10   Re: Stupid question: "const" vs "#define"
Reply With Quote #10

Ok, many thanks guys for replies. I will make some tests and I will see wich is more fine, const/define.
dFF 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 15:19.


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