C String Constants, Arrays of Pointers, Dynamic Memory Alllocation?
Hi, I was wondering if some of you could help me out, Im learning C and I want to be sure I have this all straight. Please correct me if Im wrong on anything, Id be very apprecitave. I am trying to relate what is going on in memory - comparing string constants, arrays and functions such as calloc. I can use the functions and pointers ok, but it really helps for me to understand what is going on behind the scenes.
(also, yes I know this is a C++ comminity; however, I couldnt find a C community)
Ok so from what I understand it goes something like this:
you can enter data into strings when you declare them using an array or by initializing a pointer variable to point to a string constant (see example below). The string constant is stored in a different area of memory than the array is, the array is stored by all the other variables with high memory addresses while the string constant is stored in an area with low memory addresses. You then pull out these string constants using an array of pointers.
void main(void){
char aa[] = "array used to store a string";
*char bb = "array of pointers";
}
So now with dynamic memory allocation, your have a 1D array for your string and another 1D array of pointers. Put a string into the 1D array, use strlen(aa) to get the length, and then use calloc(strlen(aa), sizeof(char)) to reseve the memory. The calloc function also returns the reserved memory address, which is then stored into the array of pointers. You then copy the string from aa[] into bb[i] and there is just enough room.
So... The memory address of the 1D char array and 1D array of pointers are located in the stack, and the allocated memory (where the pointers in bb[i] point to) is in the heap.
Relating this to the paragraph above the code example, are string constants stored in the heap as well? It seems like it would be so, as they are in the other end of memory with respect to the array of pointers pointing to them. Also, if you decalare a fixed 2D array rather than using an array of pointers, the data is stored in the stack rather than the heap?
Lastly, what is the big difference between the stack and the heap, other than being on opposite end of memory? They both shrink and grow, so whats the point to having it split?
Wow, thanks for getting all the way thought this, sorry if it was wordy/confusing but its the best way I can explain it.
(also, yes I know this is a C++ comminity; however, I couldnt find a C community)
Ok so from what I understand it goes something like this:
you can enter data into strings when you declare them using an array or by initializing a pointer variable to point to a string constant (see example below). The string constant is stored in a different area of memory than the array is, the array is stored by all the other variables with high memory addresses while the string constant is stored in an area with low memory addresses. You then pull out these string constants using an array of pointers.
void main(void){
char aa[] = "array used to store a string";
*char bb = "array of pointers";
}
So now with dynamic memory allocation, your have a 1D array for your string and another 1D array of pointers. Put a string into the 1D array, use strlen(aa) to get the length, and then use calloc(strlen(aa), sizeof(char)) to reseve the memory. The calloc function also returns the reserved memory address, which is then stored into the array of pointers. You then copy the string from aa[] into bb[i] and there is just enough room.
So... The memory address of the 1D char array and 1D array of pointers are located in the stack, and the allocated memory (where the pointers in bb[i] point to) is in the heap.
Relating this to the paragraph above the code example, are string constants stored in the heap as well? It seems like it would be so, as they are in the other end of memory with respect to the array of pointers pointing to them. Also, if you decalare a fixed 2D array rather than using an array of pointers, the data is stored in the stack rather than the heap?
Lastly, what is the big difference between the stack and the heap, other than being on opposite end of memory? They both shrink and grow, so whats the point to having it split?
Wow, thanks for getting all the way thought this, sorry if it was wordy/confusing but its the best way I can explain it.
