Array initialization (or lack thereof)
Pages: [1]
|
|
Programming Help
|
Mate de Vita
Kelli

2008 Oct 4 • 2413
159 ₧
|
If I don't initialize an array (two-dimensional if it matters), are all its fields guaranteed to be initialized to zero?
...and that's the bottom line because Mate de Vita said so.
Who controls the past, controls the future. Who controls the present, controls the past.
|
|
|
|
|
(Edited 2011 Jul 25 at 06:57)
2011 Jul 25 at 06:57
|
|
|
|
|
Mate de Vita
Kelli

2008 Oct 4 • 2413
159 ₧
|
SRAW said: why don't you try out yourself 
Because I'm guessing it could be platform-dependent?
...and that's the bottom line because Mate de Vita said so.
Who controls the past, controls the future. Who controls the present, controls the past.
|
|
|
|
|
2011 Jul 25 at 09:43
|
|
|
phoenix_r

2009 May 13 • 623
17 ₧
|
More than platform-dependent, I would imagine it to be language-dependent. Are we talking about C here, perhaps? A few of the languages I use at work that both grew out of the same language treat this differently. Our legacy DBMS doesn't use typing, but you need to initialize any dimensions past the first in an array before you can read them. Our PCL form-printing solution, on teh udder hand, has some loose typing but will return null in this situation without initialization.
TL;DR: write a truth-test. Run it. Then run it in VMs/emus if you need to test cross-platform. Also tell us what language(s) you mean when asking programming questions.
|
|
|
|
|
2011 Jul 25 at 10:22
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
If it is C:
It depends on the array's storage duration. If it is static, then yes, it will be initialized to 0. Anything that is around from the very beginning to the very end of the program has static storage duration. This includes globals and locals with the static keyword.
On the other hand, when you allocate from the heap (malloc and friends) it is not initialized to anything in particular.
Also your stack variables are not initialized. This includes all your regular old non-static local variables.
|
|
|
|
|
2011 Jul 25 at 11:14
|
|
|
Mate de Vita
Kelli

2008 Oct 4 • 2413
159 ₧
|
phoenix_r said: TL;DR: write a truth-test. Run it. Then run it in VMs/emus if you need to test cross-platform. Also tell us what language(s) you mean when asking programming questions.
Sorry 'bout that, I meant C of course.
superjer said: Also your stack variables are not initialized. This includes all your regular old non-static local variables.
Stack variables?
...and that's the bottom line because Mate de Vita said so.
Who controls the past, controls the future. Who controls the present, controls the past.
|
|
|
|
|
2011 Jul 25 at 13:52
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
2011 Jul 26 at 15:17
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
I love talking about stacks!
The stack for your process has a certain starting address in memory, and grows from there.
Whenever you call a function, space for storing the function's arguments and its local variables is reserved on top of the stack.
If you call another function from within, more space is used on top of the stack to accommodate the new storage. The stack space is reclaimed when you return from a function, in which case all of the locals and args are freed/lost.
Although really, it's just a pointer that moves down the stack, indicating where the top is. Nothing is completely lost until it is overwritten. In fact you can use pointer tricks to read, say, the locals of a function that already returned, if they still happen to be there. Don't do this in a real program.
The stack is a LIFO structure (last in, first out) which works an awful lot like a stack of plates, assuming you don't slide plates under the pile like some sort of deranged psychopath.
If you do infinite recursion or create enormous local variables, you can get a stack overflow. That is, you can run out of stack space. How much space you have is very platform/compiler dependent, and may even be able to grow automatically.
|
|
|
|
|
2011 Jul 26 at 17:17
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
2011 Sep 27 at 11:41
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
calloc()
|
|
|
|
|
2011 Nov 6 at 19:11
|
|
|
sprinkles


2009 Sep 6 • 2480
10 ₧
|
superjer said: calloc()
Don't call the Wizard of Oz!
...then I got some ap, and shot a big ass lazar at everyone.
|
|
|
|
|
2011 Nov 6 at 19:13
|
|
|
Rockbomb
Dog fucker

2009 Nov 13 • 1935
-190 ₧
|
sprinkles said: superjer said: calloc()
Don't call the Wizard of Oz!
Did you just watch that on tv? Cuz I did.
|
|
|
|
|
2011 Nov 6 at 19:17
|
|
|
sprinkles


2009 Sep 6 • 2480
10 ₧
|
No, but I guessed you would.
...then I got some ap, and shot a big ass lazar at everyone.
|
|
|
|
|
2011 Nov 6 at 19:20
|
|
|
|
Pages: [1]
|