In a recent thread i posted i was having a problem with SetPixel

As it turned out the problem was that my local variable was not initiallized with a value.

Can anybody explain to me why a local has to be initialized.

Isn't the variable given value zero when declared?

Thoughts appreciated

:alright:
Posted on 2001-09-09 19:59:06 by titan
Local variables are created with bytes that reside in the stack. (Stack Frame) The stack, this big chunk of memory, is used over and over. Thus , chances are that when you create a local var , it WON'T be zero-initialized but it'll have some value.
Hope that helps :)

Latigo
Posted on 2001-09-09 20:23:59 by latigo
Local variables are allocated (created) simply by changing the value of ESP. No storage is written to. No time is wasted storing a zero, so you can initialize to another value (e.g., one) as quickly as to zero.
Posted on 2001-09-10 17:21:09 by tank
although:

add esp, 0ch
is the same amount of clocks as having
mov mydata, 0

but then again global data is already declared in the exe, so you have to not even mov anything :)
Posted on 2001-09-10 17:26:08 by Kenny
But

sub esp,12

allocates THREE dwords.

So you need to compare timing to

mov data1,0
mov data2,0
mov data3,0

And just because globals get initialized to zero, it doesn't mean they don't need to be re-initialized to zero.
Posted on 2001-09-10 19:04:51 by tank
ok check this:

mydata dd 0 ; don't need to initalize this at all because it is 0 when PE loads.

mov mydata, eax
mov eax, mydata

now local:

sub esp,4h
mov esp,eax
lea esp,eax

If you have an AGI stall, that would indeed be slower, and you don't have to initialize it to begin with...
Posted on 2001-09-10 19:11:38 by Kenny
For mulitple processors all code should be designed to be re-entrant, and this can't be done easily with globals alone - at every point in the program you have to ask yourself about the chance of the other processors running the same code at the same time - this can be easily done with locals and different stacks.

And as tank said already many times you need to have your data re-initialized. Or another case is recursion - where the array of globals would not provide a dynamic enough area. Globals only serve linear solutions - which are indeed the fastest solutions, but usually too limited algoritmically.

It's a little tricky, but if you have many initialized values you want to put on the stack and still have the code re-entrant/recursive then you might want to try the method I used in THIS POST. It's fun to work the bugs out of software like this. ;)
Posted on 2001-09-10 19:30:47 by bitRAKE
Ummm yeah, that stuff makes my head hurt... I think I'll stick to global from now on...

Maybe later you can teach me how to do that :) later...
Posted on 2001-09-10 20:57:33 by Kenny
Titan,

Initialising local variables is a standard for basic compilers but there is no need to do it. A LOCAL variable is simply space allocated on the stack. If you decompile an EXE file and have a look at how its done, you will get an address something like which is the base pointer minus the offset for the variable.

Initialising local variables made basic easier to use but it means if you need a LOCAL variable of another value, it is being done more than once which is not particularly efficient code.

Regards,

hutch@pbq.com.au
Posted on 2001-09-10 21:39:18 by hutch--