New and Static Question
#1

Hello, I've got a question, as I'm not very fluent when it comes to pawn.

What would be the difference between using new; and static; ?

for instance

say I want to create a variable that will be used by almost every command (like in strtok / cmd,tmp)

so: new cmd[256], tmp[256];

I know that using strtok and cmd/tmp with 256 is way too much, but dont tell me, i've been told 201 times.

so, lets say after that, I took out new, and put:

static cmd[256], tmp[256];

I noticed when i did this, that heap file / code size stuff you see in the compiler dissepeared, as I have wanted it to, but what I didnt know, is how static is used, and if I am using it in the right way, or in the wrong way.
Reply
#2

Quote:
Originally Posted by [NaB
Hiitch - No srsly, Im a NaB ! ]
Hello, I've got a question, as I'm not very fluent when it comes to pawn.

What would be the difference between using new; and static; ?

for instance

say I want to create a variable that will be used by almost every command (like in strtok / cmd,tmp)

so: new cmd[256], tmp[256];

I know that using strtok and cmd/tmp with 256 is way too much, but dont tell me, i've been told 201 times.

so, lets say after that, I took out new, and put:

static cmd[256], tmp[256];

I noticed when i did this, that heap file / code size stuff you see in the compiler dissepeared, as I have wanted it to, but what I didnt know, is how static is used, and if I am using it in the right way, or in the wrong way.
Static is static variable, you can't modify it's value.
Reply
#3

so basically, static is creating a var that you can't edit? (Confused)
Reply
#4

Quote:
Originally Posted by [NaB
Hiitch - No srsly, Im a NaB ! ]
so basically, static is creating a var that you can't edit? (Confused)
Exactly, it's storage isn't allocated and deallocated on the call stack like normal variables

EDIT: Sorry, my mistake! I was confused.
Static variables are variables that are executed at compile-time, unlike the normal variables that are executed at run time.

New variables allocate the memory dynamically, Dynamic Memory Allocation definition

Static variables allocate the memory statically, Static Memory Allocation definition
Reply
#5

Quote:
Originally Posted by Y_Leѕѕ
I think I'll clarify a bit here, as those definitions were a little bit off. In a script you have global memory and stack/heap memory. The global memory is where all your global variables are stored, as they're not different in different functions. This memory is actually included into the .amx, which is why a tiny .pwn can produce a huge .amx. The stack and heap are allocated when you execute the script and store function local variables, this is because a function can call itself, so you need two copies of the same variable - if they were global they would be the same variable.

Local static variables are essentially global variables, but you can only use them in one function. If you decompile a .amx with static locals you will see them listed as globals - them being limited to one function is purely a compiler restriction, there's actually nothing in the VM itself to enforce this.

A const variable is not variable, it is just like a macro in that it always has the same value - it's just useful for defining things.

A global static variable is limited to only being used in the file in which it's declared (technically section, a file can have multiple sections, but a section can't have multiple files and the default is one section per file). These are used extensively through YSI to hide implementation details from people and stop them breaking things. But again, this is purely a compiler restriction.

Edit: You said you've been told 201 times, but it's clearly not sunk in yet, so I'm telling you a 202nd time: Don't use 256 for command variables, it's entirely pointless and a waste, the effects of which you are clearly seeing in your code or you wouldn't be getting heap space overflows when you compile.

I'm also going to tell you to use zcmd and sscanf - you say that you use those variables in almost every command, you'll use them in next to none if you bother to code properly, plus your code will be faster and more secure.
Thank you, but what I said was correct?
Reply
#6

Alright, that made complete sense, I will try to learn zcmd and sscanf, but the thing is, where do I get the sscanf stock, if there is one?
Reply
#7

Have a look what "stock" means while you are there also, half the people just throw 'public' or 'stock' onto functions because they don't understand what they mean and have seen it done like that. That's the annoying thing about most so called "tutorials" around here, they thinking just showing you the code is enough but I see that as an example and not a tutorial, they are completely different.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)