Pointers to Heap Memory and Functions
For the execution of this program fragment:
int main() {
int *arr1;
arr1 = malloc(sizeof(int)*10);
if(!arr1) {
printf("malloc error\n");
exit(1);
}
init_array(arr1, 10);
...
}
void init_array(int *arr, int size) {
int i;
for(i=0; i< size; i++) {
arr[i] = i;
}
// STACK DRAWN HERE
}
The contents of memory will look like this (note that through the
parameter arr, the contents of the passed array can be modified
by the function):
STACK
|===============|
init_array: | |
| ---- |
| size | 10 | |
| ---- |
| --- |
| arr| *-----------
| --- | | HEAP
|===============| | |====================================|
main: | | | | |
| ---- | |--------------->|---------------------| |
| arr1 | *-------------------------->| 0 | 1 | 2 | ... | 9 | |
| ---- | | |---------------------| |
| | | |
|===============| |====================================|