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.
-
4values are not allocated. The space where the values are stored is.Eugene Sh.– Eugene Sh.2019-02-11 19:09:36 +00:00Commented Feb 11, 2019 at 19:09
-
2Show a code example? You’re probably copying values into the array, which is fine.Ry-– Ry- ♦2019-02-11 19:10:17 +00:00Commented Feb 11, 2019 at 19:10
-
Generally speaking, if you heap allocate values and copy their values to an array, you will cause a memory leak, unless you explicitly deallocate them immediately after copying them to an array (which, practically, rarely makes sense). The point of an array is that it contains a set of values. Dynamic allocation of an array allows it to store a set of values, without needing to dynamically allocate more memory for each value.Peter– Peter2019-02-11 20:27:53 +00:00Commented Feb 11, 2019 at 20:27
-
You can't pass arrays into functions in C -- you can only pass pointers to/into arrays. If you declare a function argument as an array, the compiler will silently turn it into a pointer. If you use an array as an argument, it will silently be turned into a pointer to the 0th element.Chris Dodd– Chris Dodd2019-02-11 21:28:59 +00:00Commented Feb 11, 2019 at 21:28
-
please be more specific with your question. Create a minimal reproducible example with what you want to do. As it is, we are left guessing what you actually mean an the answers you get reflect this.bolov– bolov2019-02-11 21:59:09 +00:00Commented Feb 11, 2019 at 21:59
3 Answers
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.
Comments
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.
Comments
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.