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?

6
  • cplusplus.com/forum/articles/416
    – jonvuri
    Sep 22, 2012 at 14:53
  • Note that int a[10]; only declares a static array if the declaration is at file scope. Sep 22, 2012 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? Sep 22, 2012 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 malloced objects, static has a technical meaning that is not the complement of "dynamic" in the above sense. Sep 22, 2012 at 15:26
  • @DanielFischer Ok, so it's just a terminology thing. Thanks Sep 22, 2012 at 15:28

3 Answers 3

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.

7
  • But what if I declare int a[10] as a global variable? Then does it make a difference? Sep 22, 2012 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, 2012 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, 2014 at 12:06
  • 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, 2014 at 14:13
  • 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, 2014 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.

2
  • 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? Sep 22, 2012 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, 2012 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.