# array dynamic allocation

**URL:** <https://community.unix.com/t/array-dynamic-allocation/236265>\
**Category:** Programming\
**Created:** [March 26, 2009, 6:39am UTC](https://community.unix.com/t/array-dynamic-allocation/236265 "2009-03-26T06:39:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![littleboyblu](https://community.unix.com/letter_avatar/littleboyblu/32/6_449c533f586939b6e5f246632a46421f.png) [@littleboyblu](https://community.unix.com/u/littleboyblu)\
**Post date:** [March 26, 2009, 6:39am UTC](https://community.unix.com/t/array-dynamic-allocation/236265/1 "2009-03-26T06:39:57Z")

</div>

Hi,  
I have the following problem: i must allocate a dynamic array from a subroutine which should return such array to main function. The subroutine has already a return parameter so i thought of pass the array as I/O parameter. I tried the following program but it doesn't work (segmentation fault), i think because i should have a pointer to array, but i'm not sure.

```nohighlight
void main(){
int *array;
int **ptr = &array;
sub(ptr);
}

void sub(int **ptr){
....//calculates the array dimension
*ptr = calloc(n,sizeof(int));
for(i=0;i<n;i++)*ptr=i;
}

```

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/6_449c533f586939b6e5f246632a46421f.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [March 26, 2009, 12:50pm UTC](https://community.unix.com/t/array-dynamic-allocation/236265/2 "2009-03-26T12:50:03Z")

</div>

Because you are skipping a step in allocating memory...\*ptr has been calloced w/o first allocating memory for ptr which is a dual-level pointer while \*ptr is a single-level.

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [March 26, 2009, 1:32pm UTC](https://community.unix.com/t/array-dynamic-allocation/236265/3 "2009-03-26T13:32:46Z")

</div>

Also: main always returns an int. It is never void. If your compiler does not complain, it is probably an ancient one. There are a lot of reasons here are two

1. You will get random garbage returned to the calling program or shell

2. Some compilers implement the stack frame for void functions differently than for int functions - for optimization reasons - resulting in a almost impossible to debug crash when main() ends.

---

<div class="post-metadata">

**Author:** ![littleboyblu](https://community.unix.com/letter_avatar/littleboyblu/32/6_449c533f586939b6e5f246632a46421f.png) [@littleboyblu](https://community.unix.com/u/littleboyblu)\
**Post date:** [March 27, 2009, 10:47am UTC](https://community.unix.com/t/array-dynamic-allocation/236265/4 "2009-03-27T10:47:53Z")

</div>

shamrock could you correct my code please??

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/6_449c533f586939b6e5f246632a46421f.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [March 27, 2009, 12:01pm UTC](https://community.unix.com/t/array-dynamic-allocation/236265/5 "2009-03-27T12:01:02Z")

</div>

```nohighlight
void sub(int **ptr) {
    ptr=(int**)calloc(n,sizeof(int*));
    for(i=0;i<n;i++) {
         ptr=(int*)calloc(1,sizeof(int));
        *ptr=i;
    }
}

```

---

<div class="post-metadata">

**Author:** ![littleboyblu](https://community.unix.com/letter_avatar/littleboyblu/32/6_449c533f586939b6e5f246632a46421f.png) [@littleboyblu](https://community.unix.com/u/littleboyblu)\
**Post date:** [March 27, 2009, 2:43pm UTC](https://community.unix.com/t/array-dynamic-allocation/236265/6 "2009-03-27T14:43:31Z")

</div>

ok but in this way i lose the array when sub returns...

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/6_449c533f586939b6e5f246632a46421f.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [March 27, 2009, 4:48pm UTC](https://community.unix.com/t/array-dynamic-allocation/236265/7 "2009-03-27T16:48:55Z")

</div>

> [@littleboyblu](#):
>
> ok but in this way i lose the array when sub returns...

If you pass a pointer to the array you won't lose it then.

---

<div class="post-metadata">

**Author:** ![pshaikh](https://community.unix.com/letter_avatar/pshaikh/32/6_449c533f586939b6e5f246632a46421f.png) [@pshaikh](https://community.unix.com/u/pshaikh)\
**Post date:** [March 28, 2009, 2:54am UTC](https://community.unix.com/t/array-dynamic-allocation/236265/8 "2009-03-28T02:54:07Z")

</div>

Hi Shamrock,

\*ptr has already been initialized with address of local variable 'array' as -

\*ptr = &array; in statement int \*\*ptr = &array;

ptr(int\*\*) -\> array(int\*) -\> (uninitialized/invalid reference to integer).

ptr = &array;

_ptr (or array) = (int_)calloc(n,sizeof(int)); will initialize array to hold address of allocated memory.

---

<div class="post-metadata">

**Author:** ![pshaikh](https://community.unix.com/letter_avatar/pshaikh/32/6_449c533f586939b6e5f246632a46421f.png) [@pshaikh](https://community.unix.com/u/pshaikh)\
**Post date:** [March 28, 2009, 2:57am UTC](https://community.unix.com/t/array-dynamic-allocation/236265/9 "2009-03-28T02:57:51Z")

</div>

Correction -

\*ptr has already been initialized with address of local variable 'array' as -  
\*ptr = &array; in statement int \*\*ptr = &array;

ptr has already been initialized with address of local variable 'array' as -  
ptr = &array; in statement int \*\*ptr = &array;

---

<div class="post-metadata">

**Author:** ![littleboyblu](https://community.unix.com/letter_avatar/littleboyblu/32/6_449c533f586939b6e5f246632a46421f.png) [@littleboyblu](https://community.unix.com/u/littleboyblu)\
**Post date:** [March 28, 2009, 1:22pm UTC](https://community.unix.com/t/array-dynamic-allocation/236265/10 "2009-03-28T13:22:13Z")

</div>

Sorry Shamrock but I did not understand your answer.Should ptr be a pointer to array??  
I tried the following and it seems to work fine:

int main(){  
int \*array;  
int \*\*ptr = &array;  
sub(ptr);  
}  
void sub(int \*_ptr){  
//calculates n...  
int \*tmp=(int_)calloc(n,sizeof(int));  
for(i=0;i\<n;i++)  
tmp[i]=i;  
\*ptr=tmp;  
}

---

<div class="post-metadata">

**Author:** ![pshaikh](https://community.unix.com/letter_avatar/pshaikh/32/6_449c533f586939b6e5f246632a46421f.png) [@pshaikh](https://community.unix.com/u/pshaikh)\
**Post date:** [March 29, 2009, 2:00am UTC](https://community.unix.com/t/array-dynamic-allocation/236265/11 "2009-03-29T02:00:22Z")

</div>

littleboyblu,

Can you try this one -

void main(){  
int \*array;  
int \*\*ptr = &array;  
sub(ptr);  
}

void sub(int \*\*ptr){  
....//calculates the array dimension  
\*ptr = calloc(n,sizeof(int));  
for(i=0;i\<n;i++)  
(\*ptr)[i]=i; // subscript operator [] have higher precedence than dereference \* operator  
}

---

<div class="post-metadata">

**Author:** ![littleboyblu](https://community.unix.com/letter_avatar/littleboyblu/32/6_449c533f586939b6e5f246632a46421f.png) [@littleboyblu](https://community.unix.com/u/littleboyblu)\
**Post date:** [March 30, 2009, 3:43am UTC](https://community.unix.com/t/array-dynamic-allocation/236265/12 "2009-03-30T03:43:36Z")

</div>

thanks pshaikh
