0
_________* arr = malloc(4 * sizeof(int)); arr = ________(arr, 8 * sizeof(int));
Fill in the blanks to allocate memory for an int array and then expand the memory for more elements. (Dynamic Array) Anyone know the solution ?
3 ответов
0
what is your option? interesting to find out
0
should be:
int *arr = (int*)malloc(4 * sizeof(int));
arr = (int*)realloc(arr, 8 * sizeof(int));
But just so you know, although this should work in 99.9999% of normal use cases (unless you intentionally cause it to fail), if realloc() fails, it returns NULL and the old arr will be lost and wasted - can no longer be used nor freed. So, imo, the recommended use of realloc() would be smth like:
int *tmp = (int*)realloc(arr, 8 * size(int));
if(tmp) arr = tmp;
0
int* arr = malloc(4 * sizeof(int));
arr = realloc(arr, 8 * sizeof(int));