1

What is the difference between declaring an array "dynamically",

[ie. using realloc() or malloc(), etc... ]

vs

declaring an array within main() with Global scope?,

eg.

int main()
{
    int array[10];

    return 0;
}

I am learning, and at the moment it feels that there is not much differnce between

declaring a variable (array, whatever) -with Global scope,

when compared to a

dynamically allocated variable (array, whatever) -AND never calling free() on it AND allowing it to be 'destoryed' when the program ends'

What are the consequences of either option?

EDIT

Thank you for your responses.

Global scope should have been 'local scope' -local to main()

carl13
  • 69
  • 1
  • 1
  • 7
  • 1
    Firstly, your example does not show any variables in global scope. `array` is local to `main`. No other function can access that variable unless it is passed into the function as a parameter. As for your question, please take a look at: [When is malloc necessary in C?](http://stackoverflow.com/questions/3889450/when-is-malloc-necessary-in-c) – kaylum Feb 07 '16 at 05:58
  • Possible duplicate of [What are some useful examples of malloc() in C?](http://stackoverflow.com/questions/4084950/what-are-some-useful-examples-of-malloc-in-c) – Ravichandra Sutrave Feb 07 '16 at 06:08

2 Answers2

1

When you declare an array like int arr[10] in a function, the space for the array is allocated on the stack. The memory will be freed when your function exits.

When you declare an array or any other data structure using malloc() or realloc(), you allocated the space on the heap and the memory will only be freed afer the program exits. So when the program is running, you are responsible for freeing it using free() after you no longer want to use it. If you don't free it and make your array pointer point to something else, you will create a memory leak. However, your computer will always be able to retrieve all the program's used memory after the program ends because of virtual memory.

SegFault
  • 2,526
  • 4
  • 21
  • 41
0

As kaylum said in comment below your question, the array in your second example does not have global scope. Its scope is limited to main(), and it is inaccessible in other scopes unless main() explicitly makes it available (e.g. passes it by argument to another function).

Dynamic memory allocation means that the programmer explicitly allocates memory when needed, and explicitly releases it when no longer needed. Because of that, the amount of memory allocated can be determined at run time (e.g. calculated from user input). Also, if the programmer forgets to release the memory, or reallocates it inappropriately, memory can be leaked (still allocated by the program, but not accessible by the program). For example;

  /*  within a function  */

  char *p = malloc(100);
  p = malloc(200);
  free(p);

leaks 100 bytes, every time this code is executed, because the result of the first malloc() call is never released, and it is then inaccessible to the program because its value is not stored anywhere.

Your second example is actually an array of automatic storage duration. As far as your program is concerned, it only exists until the end of the scope in which it is created. In your case, as main() returns, the array will cease to exist.

An example of an array with global scope is

int array[10];

void f() {array[0] = 42;}

int main()
{
      array[0] = 10;
      f();
      /*  array[0] will be 42 here */
}

The difference is that this array exists and is accessible to every function that has visibility of the declaration, within the same compilation unit.

One other important difference is that global arrays are (usually) zero initialised - a global array of int will have all elements zero. A dynamically allocated array will not have elements initialised (unless created with calloc(), which does initialise to zero). Similarly, an automatic array will not have elements initialised. It is undefined behaviour to access the value of something (including an array element) that is uninitialised.

So

 #include <stdio.h>
 int array[10];

 int main()
 {
      int *array2;
      int array3[10];

      array2 = malloc(10*sizeof(*array2));

      printf("%d\n", array[0]);   /* okay - will print 0 */
      printf("%d\n", array2[0]);   /* undefined behaviour.  array2[0] is uninitialised */

      printf("%d\n", array3[0]);   /*  undefined behaviour.  array3[0] uninitialised */

      return 0;
 }

Obviously the way to avoid undefined behaviour is to initialise array elements to something valid before trying to access their value (e.g. printing them out, in the example above).

Peter
  • 35,646
  • 4
  • 32
  • 74