AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Cleaning a bit of memory by "const" or "#define" (https://forums.alliedmods.net/showthread.php?t=188591)

dFF 06-28-2012 04:45

Cleaning a bit of memory by "const" or "#define"
 
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.

Backstabnoob 06-28-2012 07:02

Re: Cleaning a bit of memory by "const" or "#define"
 
I think the difference is trivial as long as you tak about numbers.

Arkshine 06-28-2012 07:13

Re: Cleaning a bit of memory by "const" or "#define"
 
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.

ConnorMcLeod 06-28-2012 13:18

Re: Cleaning a bit of memory by "const" or "#define"
 
Quote:

Originally Posted by Arkshine (Post 1737997)
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.

MyPc 06-28-2012 14:07

Re: Cleaning a bit of memory by "const" or "#define"
 
Quote:

Originally Posted by ConnorMcLeod (Post 1738244)
Example of 'better error checking' statement would be greatly apprcieated.

Wouldnt it ?

Arkshine 06-28-2012 15:25

Re: Cleaning a bit of memory by "const" or "#define"
 
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

ConnorMcLeod 06-28-2012 17:26

Re: Cleaning a bit of memory by "const" or "#define"
 
Quote:

Originally Posted by Arkshine (Post 1738334)
const RANDOM = 66.6

Not sure you can even write that single line.
Thanks for clarification.

fysiks 06-28-2012 18:42

Re: Cleaning a bit of memory by "const" or "#define"
 
Quote:

Originally Posted by Arkshine (Post 1737997)
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?

Arkshine 06-28-2012 19:16

Re: Cleaning a bit of memory by "const" or "#define"
 
Yes, special case for Pawn. I've checked with amxxdump.

dFF 06-29-2012 02:50

Re: Cleaning a bit of memory by "const" or "#define"
 
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.


All times are GMT -4. The time now is 06:12.

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