Raised This Month: $ Target: $400
 0% 

Cleaning a bit of memory by "const" or "#define"


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
dFF
sıɹɹoɥɔ ʞɔnu
Join Date: Oct 2009
Old 06-28-2012 , 04:45   Cleaning a bit of memory by "const" or "#define"
Reply With Quote #1

For example, If I used much plugin who use a lot of 'datas' (offset, custom values, etc), what is the best method to hold these 'datas' whit "const" or "#define" ?
If I use "#define" while clear a bit of memory, for example:
PHP Code:
#define MY_OFFSET 114
#define OFF_DIFF 5
#define MY_VALUE 3
set_pdata_intidMY_OFFSETMY_VALUEOFF_DIFF )
// after compile set:
set_pdata_intid1143
and If I used "const" while set:
PHP Code:
set_pdata_intidMY_OFFSETMY_VALUEOFF_DIFF 
right ?

What is the better solution ?

Thanks, and sorry for stupid question.
dFF is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-28-2012 , 07:02   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #2

I think the difference is trivial as long as you tak about numbers.
Backstabnoob is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-28-2012 , 07:13   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #3

In such case, meaning defining a simple value, both #define and const are equivalent.
In others words, both values will be replaced at compilation time.
The advantages using const is to have a better error checking and the ability to create tagged constants.
I would use always const to define simple values.
__________________
Arkshine is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-28-2012 , 13:18   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #4

Quote:
Originally Posted by Arkshine View Post
The advantages using const is to have a better error checking and the ability to create tagged constants.
Example of 'better error checking' statement would be greatly apprcieated.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
MyPc
Senior Member
Join Date: Sep 2011
Old 06-28-2012 , 14:07   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #5

Quote:
Originally Posted by ConnorMcLeod View Post
Example of 'better error checking' statement would be greatly apprcieated.
Wouldnt it ?
MyPc is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-28-2012 , 15:25   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #6

Not sure why it needs examples...

Random and stupid ones :

#define SIZE 2.5
new array[ SIZE ]; // Compiler happy

const SIZE = 2.5
new array[ SIZE ]; // Tag mismatch

#define RANDOM "LoL" // or random value like 66.6.
#define MACRO(%0) ( %0 * RANDOM ) // Compiler happy

const RANDOM = "LoL" // error
const RANDOM = 66.6
#define MACRO(%0) ( %0 * RANDOM ) // tag mismatch
__________________
Arkshine is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-28-2012 , 17:26   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #7

Quote:
Originally Posted by Arkshine View Post
const RANDOM = 66.6
Not sure you can even write that single line.
Thanks for clarification.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-28-2012 , 18:42   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #8

Quote:
Originally Posted by Arkshine View Post
In such case, meaning defining a simple value, both #define and const are equivalent.
In others words, both values will be replaced at compilation time.
I always thought that constants were actually variable and were not retrieved until execution. Is this a special case for Pawn or does it also do it at compile time for other languages too?
__________________
fysiks is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-28-2012 , 19:16   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #9

Yes, special case for Pawn. I've checked with amxxdump.
__________________

Last edited by Arkshine; 06-28-2012 at 19:19.
Arkshine is offline
dFF
sıɹɹoɥɔ ʞɔnu
Join Date: Oct 2009
Old 06-29-2012 , 02:50   Re: Cleaning a bit of memory by "const" or "#define"
Reply With Quote #10

I can't remember where I read, If You know what type of datas you set on functions is better for memory to declare/use these datas with "#define". Not sure if that's true or doesn't matter.

Last edited by dFF; 06-29-2012 at 03:42.
dFF is offline
Reply


Thread Tools
Display Modes

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:12.


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