Reputation: 345
I was wondering if I had a heap allocated array and I passed it into my function. If I fill the heap allocated array in my function without heap allocating the values (ie. Just assigning values to the heap array) will that create an error, and will I be able to access my heap array values outside of the function or will they disappear because stack variables disappear after the function call ends.
Upvotes: 0
Views: 1524
Reputation: 368
When using malloc for an array, what it's really doing is allocating space for however many elements are to be stored in the array. This is why you give malloc the datatype * the capacity, nothing else needs to be stored in the heap.
As said in earlier replies, when you pass the array to a function, all that's being stored on the stack is the temp pointer containing the location of the array on the heap. So if you change a value, it will follow the pointer and change it there.
In this case, it doesn't matter where the values were stored beforehand (a variable on the stack or other value on the heap) because when it's placed in the array, the only thing being transferred is the value.
The problem you're thinking of would arise if instead of copying the values from variables on the stack, you filled the array with pointers to the stack variables, in which case the values could get overwritten. As long as the values are in the heap memory when the function exits, they're safe and sound.
Having to use malloc for every item you want to add to an array would be a nightmare and defeat the purpose of allocating the array in the first place.
Upvotes: 2
Reputation: 398
I think what your asking, is if you malloc
an array and send it to a function:
I passed it into my function.
What happens is that a pointer to that memory (array) is created in the stack frame of the function. The array is not sent into the function. And as long as you have enough room, you can treat it like any other array. When the function exit, the pointer in it get popped off with the frame, but all the data is in the array in the calling function (heap). Remember whenever you send an array to a fcn, what really get sent is a pointer to it.
The fact that alloc memory is the heap, does not change anything. Just make sure you have enough room, and do not overflow the memory.
EDIT
If I fill the heap allocated array in my function without heap allocating the values
Not sure what your asking, If your array say int *p1 = malloc(50*sizeof(int));
Then in the function you can put 50 ints in it. If you send just int *p
in, then no. Plus *p is in the stack anyway. You would need to malloc
memory for it.
Upvotes: 1
Reputation: 21552
If you're asking "can I copy the value from a variable allocated on the stack to a variable allocated on the heap", the answer is yes.
You can copy data from the heap to the stack, and vice versa.
int i = 50;
int *p = malloc(sizeof(int));
*p = i;
printf("%d\n", *p);
Assuming malloc
hasn't failed, the output of this program is 50
.
Upvotes: 1