H.K
H.K

Reputation: 241

How to dynamically allocate an array of integers in C

A portion of my C code is shown below.

int data[10]={1,3,6,8,1,7,9,1,1,1}; 
b=10;
int out[b];
process(data, &b, out);
alpha (out, b);

data and out are int arrays. The function process takes the array data whose length is pointed by b (=10) and performs mathematical operation and then returns an array out whose length is then again returned by b (unknown and hence required to be dynamically allocated). Then the array out is sent with function alpha. Right now the function alpha always sends out[10] since b has been declared as 10 in second line of code. How can I allocate array out dynamically so that it contains only valid data returned after function process.

Upvotes: 5

Views: 44949

Answers (3)

mousomer
mousomer

Reputation: 2838

You need to know the difference between dynamic and static allocations.

There are 3 alternatives:

  • Static allocation:

You need to know in advance the array length. It must be a number and not a variable:

int out[10];

Array is static and is only locally scoped. So if you do:

function do_something()
{
   int out[10];
}

you can't use the out array outside the function. But you can define out outside and send it like this:

function do_something(int* out)
{
   // do things
}
...
{
   int out[10];
   do_something(out);
}
  • Automatic allocation

When you do

int b = 100;
int out[b];

(which won't compile on gcc without the -std=c99 or -std=c11 flag), you get an automatic variable, which is very convenient if you don't use out out of scope, but can be a bit dangerous. The resulting array is generated in the Stack, and is destroyed when it goes out of scope (which is why it can get you into trouble if you use it freely). See https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Variable-Length.html

We suggest you use:

  • Dynamic allocation

Where the array is generated on the Heap and you are responsible to clean it up when you're done with it. The down side is you need to clean it up yourself. The up side is you can use pass it around and use it anywhere.

int b=100;
int* out = (int*) malloc(b * sizeof(int));
// do things with out
free(out);

VERY IMPORTANT: Do not change the value of the pointer out. If you do, then you won't free the right amount of memory. A nice thing to do is to copy the pointer, and use the copied address for free:

   int b=100;
   int* out = (int*) malloc(b * sizeof(int));
   int* out_copy = out;
   // do things with out. don't touch out_copy
   free(out_copy);

Upvotes: 8

molbdnilo
molbdnilo

Reputation: 66371

You need out to be a pointer - not an array - and you need to pass a pointer to out to the function, just like you do with b.

Example:

void f(int **a, int *size)
{
    *a = malloc(23 * sizeof(**a));
    *size = 23;
}


/* ... */
int *p = NULL;
int b = 0;
f(&p, &b);
/* 'p' has been allocated and 'b' has its size. */

Upvotes: 0

Issac Saji
Issac Saji

Reputation: 106

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

This will produce array out of integer type with size 10.

Upvotes: 1

Related Questions