Raised This Month: $32 Target: $400
 8% 

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
dFF
sıɹɹoɥɔ ʞɔnu
Join Date: Oct 2009
Old 06-08-2011 , 13:10   Re: Stupid question: "const" vs "#define"
Reply With Quote #6

Ok, many thanks guys for replies. I will make some tests and I will see wich is more fine, const/define.
dFF is offline
Devil259
Veteran Member
Join Date: Dec 2009
Location: France (59)
Old 06-08-2011 , 13:50   Re: Stupid question: "const" vs "#define"
Reply With Quote #7

You can't see that in a code,

the two methods work fine
__________________
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 #8

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 #9

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
Dr7sTyLe
Senior Member
Join Date: Dec 2010
Old 06-08-2011 , 14:05   Re: Stupid question: "const" vs "#define"
Reply With Quote #10

Quote:
Originally Posted by Devil259 View Post
And where are the defines in this example ? ...
there is no defines in this ex -_-
Dr7sTyLe 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 06:20.


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