AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   malloc in pawn? (https://forums.alliedmods.net/showthread.php?t=185585)

Hyper Nova 05-19-2012 10:41

malloc in pawn?
 
Hello, i need to know what is malloc in pawn,

I need to found this function but i did not found it in any include file.
However i search from google and it seems like there are not that function in pawn like malloc.

Can someone help me? :)

<VeCo> 05-19-2012 10:47

Re: malloc in pawn?
 
Doesn't exist in AMXX Pawn.

ConnorMcLeod 05-19-2012 11:06

Re: malloc in pawn?
 
The only data size in pawn is cell (4 bytes), you can only declare variables that have that size or array of such type :

new var
new Float:var
new bool:var

All of them uses a cell.

You can declare arrays :

new string[32]
new bool:IsPlayerCheater[33]


Note that pawn let you declare char arrays doing : new array[128 char] but it is not supported by any amxx native.
You could use it anyway instead of player bool variable :

new bool:g_HasParachute[33]
-> g_HasParachute[player] = true , g_HasParachute[player] = false

Instead you can do :
new g_HasParachute[33 char]
g_HasParachute{id} = 1

It will use less memory.



If you don't want to allocate memory each time, consider using static variables inside functions, or global variables.

t3hNox 05-19-2012 13:08

Re: malloc in pawn?
 
I would like to clearify something.
In terms of memory usage it means creating Array[33 char] would be more efficient than Array[33] ?

However, creating a simple variable (new SomeVar) would be even more effecient than Array[33 char] if bit shifting is used to assign values to SomeVar (but for simple variables there is a strict limitation as to how many values can be assigned) ?

ConnorMcLeod mentioned that this char functionality is not supported by any amxx native yet I have seen plugins using arrays with chars without having any appearant problems. For example if I'm using iMoney[33 char] array to store every player's money, would it cause any problems (I'm simply talking about setting and retrieving values from array, not formatting or anything like that) ?

And lastly, would this work ?
new Array[95 char]
Array{83} = 235315

ConnorMcLeod 05-19-2012 13:27

Re: malloc in pawn?
 
Create [33 char] uses 33 bytes or memory, create [33] uses 132, though it's not a big deal nowadays, and also, i'm not sur but i think that a fix amount of memory is reserved to each plugin.
You can only set a value between 0 and 255, so you won't buy a lot of thing to such few money :P
When i said no native supports it, i meant this for strings.

t3hNox 05-19-2012 15:01

Re: malloc in pawn?
 
Ok, thanks.


All times are GMT -4. The time now is 00:28.

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