2

Are the following two methods of creating arrays in C equivalent?

int main( void )
{
    int *array = malloc(sizeof(int) * 10);
    int array2[10];
}

My thought is that method two is syntactic sugar for method one, but I'm not sure.

Also, what do array and array2 contain after these declarations? I know array is a pointer to the start of an array, but what is the array initialized to? How about for array2?

user3386109
  • 34,287
  • 7
  • 49
  • 68
Adam
  • 8,752
  • 12
  • 54
  • 96
  • No they are not the same. Do some reading on stack vs heap memory and what malloc actually does. – CollinD Jan 28 '16 at 00:28
  • One declares an array. The other declares a pointer, calls `malloc` and makes the pointer to hold the value that `malloc` returned. – user253751 Jan 28 '16 at 02:19

1 Answers1

5

They are not remotely equivalent. This:

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

will allocate a block of memory of the heap, and leave you with a pointer to that memory.

This:

int array2[10];

will allocate some memory on the stack. Read this excellent answer about stack and heap memory: What and where are the stack and heap?

Community
  • 1
  • 1
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • So `array` will persist until the program exits whereas `array2` will go out of scope as soon as the function it is created in returns. For both, we have no way of knowing what the values are intially (i.e. they're not all initialized to 0 or something). – Adam Jan 28 '16 at 00:33
  • Or you could say that `array` is allocated at runtime (dynamically) whereas `array2` is allocated statically at compile time. Assuming `array2` is declared in the main() function. – Adam Jan 28 '16 at 00:34
  • @user3386109 Fair enough. Say this was all happening in `main()` then. Then the two methods would be essentially equivalent (except we might run into problems if we tried to create a really large array on the stack). – Adam Jan 28 '16 at 00:46
  • @user3386109 If I did `int array[10]` inside some function I call in main then `array` would go out of scope as soon as the function returns. – Adam Jan 28 '16 at 00:47
  • @Adam You provided the context in the edit to your second comment, so I withdrew my comment. Note that if `int array2[10]` appears outside of any function, i.e. at global scope, the answer changes. But since you have specified that `array2` is declared in `main`, then it is allocated on the stack at run time. – user3386109 Jan 28 '16 at 00:58
  • 2
    @Adam You seem to be saying that they are "basically equivalent, except for ". Along those lines, statues and oranges are basically equivalent, except that statues are art pieces and oranges are fruit, statues are bigger than oranges, oranges are edible while statues aren't, oranges are a commodity while statues are often unique, and so on. – user253751 Jan 28 '16 at 02:16
  • @immibis I'm not saying that they're perfectly equivalent in all situations. I provided a specific circumstance (within `main()`) where they behave in a very similiar way. – Adam Jan 28 '16 at 21:50
  • @Adam, you can access memory and store numbers with `array[x]` but that is where the similarities end. Heap and stack memory is very different. Also make sure you use `free` whenever you `malloc` new memory. – Fantastic Mr Fox Jan 28 '16 at 21:51