2

Possible Duplicate:
Is array name a pointer in C?
C++ Static array vs. Dynamic array?

I'm learning C and I'm confused at what the difference is between the following two arrays:

int a[10];

and

int *b = (int *) malloc(10 * sizeof(int));

Just on the most basic level, what is the difference between these two?

Community
  • 1
  • 1
Joey Franklin
  • 6,423
  • 7
  • 25
  • 22
  • http://www.cplusplus.com/forum/articles/416/ – jonvuri Sep 22 '12 at 14:53
  • Note that `int a[10];` only declares a _static_ array if the declaration is at file scope. – Daniel Fischer Sep 22 '12 at 15:15
  • @DanielFischer what do you mean? Are you saying that if I have that line in a function it isn't called static or that it won't do what I want? – Joey Franklin Sep 22 '12 at 15:21
  • 1
    In a function, that declares a local array of automatic storage duration, not an array of static storage duration (`static int a[10];` would declare an array of static storage duration in block/function scope). I think you've been led astray by the common misnomer "dynamic array" for `malloc`ed objects, `static` has a technical meaning that is not the complement of "dynamic" in the above sense. – Daniel Fischer Sep 22 '12 at 15:26
  • @DanielFischer Ok, so it's just a terminology thing. Thanks – Joey Franklin Sep 22 '12 at 15:28
  • Yes. Sorry for not being more clear from the beginning. (More pedanticism: the standard doesn't say where `int a[10];` or `int *b = malloc(10*sizeof *b);` are allocated, so the answers saying the one is on the stack, the other on the heap can theoretically be wrong. In all normal cases [might be different for some embedded devices] though, that's where things go.) – Daniel Fischer Sep 22 '12 at 15:34

3 Answers3

6
int a[10];

is allocated on stack and is de-allocated as soon as the scope ends.

int *b = (int *) malloc(10 * sizeof(int));

is allocated on heap and is alive throughout the lifetime of the program unless it's explicitly freed.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • But what if I declare `int a[10]` as a global variable? Then does it make a difference? – Joey Franklin Sep 22 '12 at 14:58
  • Global variables are alive throughout the program. But note that's not equivalent to a malloc'ing a variable. – P.P Sep 22 '12 at 15:01
  • @Zboson `alloca` is neither part of ISO C nor POSIX. I'd avoid such functions unless I have no other options left. – P.P Oct 17 '14 at 12:06
  • Oh, never mind, I see your answer is addressing the OP's question only. – Z boson Oct 17 '14 at 14:11
  • 1
    @Zboson The word "stack" might cause a little confusion. C definitions are based on storage duration: 1) Automatic storage - local variables, arrays etc 2) Static stroage - Global variables, arrays, any variable `static` qualifier etc. Whether VLAs are allocated on "stack"(automatic) or "heap" (dynamic) is an implementation detail. As such, VLAs should be treated like a "stack" allocated variable. – P.P Oct 17 '14 at 14:13
  • You answer is fine. It's this one I'm unhappy with https://stackoverflow.com/questions/2672085/c-static-array-vs-dynamic-array. It seems to imply that all static arrays are created on the stack when in fact they could be created in the data segment. This is the best answer https://stackoverflow.com/questions/17775066/c-c-performance-of-static-arrays-vs-dynamic-arrays – Z boson Oct 17 '14 at 14:16
  • 1
    @Zboson Agreed that answer is a misleading a bit. I added a comment there. Hopefully, the answerer will edit it or at least it'll serve as a caution to future readers. – P.P Oct 17 '14 at 14:30
1

The static array will be destroyed as soon as you leave the current stack frame (basically, when the function you're in returns). The dynamic array sticks around forever until you free() it.

1''
  • 26,823
  • 32
  • 143
  • 200
  • So if I did: `int a[10]; int *ptr = &a;` inside a method and then returned `ptr` then `ptr` would be pointing to garbage after this method returned? – Joey Franklin Sep 22 '12 at 15:02
  • @JoeyFranklin Not necessarily garbage. It may still be pointing to the same address and *seem* to work. But it's an undefined behaviour which means it may crash later at some point. – P.P Sep 22 '12 at 15:09
1

The first lives on the stack (= lives as long as the scope of the variable), the second lives on the heap (= lives until freed). The first has a fixed size, whereas the second can be re-sized.

marcelnijman
  • 586
  • 4
  • 10