I\'m a bit new to malloc and C in general. I wanted to know how I can, if needed, extend the size of an otherwise fixed-size array with malloc.
Example:
You want to use realloc (as other posters have already pointed out). But unfortunately, the other posters have not shown you how to correctly use it:
POINTER *tmp_ptr = realloc(orig_ptr, new_size);
if (tmp_ptr == NULL)
{
// realloc failed, orig_ptr still valid so you can clean up
}
else
{
// Only overwrite orig_ptr once you know the call was successful
orig_ptr = tmp_ptr;
}
You need to use tmp_ptr
so that if realloc
fails, you don't lose the original pointer.