I am a relatively new coder, so this may be a silly question but I've as yet to find an adequate answer, or any answer at all for that matter.
Why use malloc/free? In what particular situations should they be used? My only guess, so far, is to use them to conserve memory. So...if one were to save large amounts of data in a variable, say a struct or very large array, instead of initializing the struct or array in its full size, then add/remove it from memory as you need it? Does the variable need to be defined as a pointer or typical varible?
Example:
I also read that it was necessary for proper use of nested arrays or linked structs but with no explanation why. I'm not 100% I understand how they work anyways, or why you'd use them. My understanding of a linked list is thus:Code:{ int *p; /* some kind of code */ p = malloc(999999); /* arbitrary number for arguments sake */ /* error check then do something with int array */ free(p); p = 0; /* or NULL */ /* rest of code */ }
I think I must be missing the point of using a linked list...Code:struct linkedlist { int x; struct linkedlist link; };
Thanks in advance,
Rob