Array initialization (or lack thereof)

Array initialization (or lack thereof)

Mate de Vita
Kelli

2008 Oct 4 • 2453
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.
 
 
2011 Jul 25 at 06:57 PDT — Ed. 2011 Jul 25 at 06:57 PDT
SRAW
Rocket Man

2007 Nov 6 • 2525
601 ₧
why don't you try out yourself
Free Steam Games
 
 
2011 Jul 25 at 07:42 PDT
Mate de Vita
Kelli

2008 Oct 4 • 2453
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.
 
 
2011 Jul 25 at 09:43 PDT
phoenix_r

2009 May 13 • 898
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.
BOO
 
 
2011 Jul 25 at 10:22 PDT
SuperJer
Websiteman

2005 Mar 20 • 6253
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 PDT
Mate de Vita
Kelli

2008 Oct 4 • 2453
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.
 
 
2011 Jul 25 at 13:52 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Stack variables are the ones you declare in a function, your 'int i's and the like. In java, if you care, variables in your arrays are initialised to their default values, often zero for primitives. But as mentioned, not in C.
 
 
2011 Jul 26 at 15:17 PDT
SuperJer
Websiteman

2005 Mar 20 • 6253
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 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
I'm looking at C for my University project right now, and on my travels I found a malloc() which will, in fact, zero everything for you. I can't remember what it's called though.
 
 
2011 Sep 27 at 11:41 PDT
SuperJer
Websiteman

2005 Mar 20 • 6253
calloc()
 
 
2011 Nov 6 at 19:11 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
superjer said:
calloc()

Don't call the Wizard of Oz!
 
 
2011 Nov 6 at 19:13 PST
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
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 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
No, but I guessed you would.
 
 
2011 Nov 6 at 19:20 PST
Page [1]