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 ?

16th Aug 2022, 2:24 PM
Ronit Zinzuvadiya
Ronit Zinzuvadiya - avatar
3 ответов
0
what is your option? interesting to find out
16th Aug 2022, 2:35 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
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;
17th Aug 2022, 6:52 AM
lion
lion - avatar
0
int* arr = malloc(4 * sizeof(int)); arr = realloc(arr, 8 * sizeof(int));
11th Dec 2022, 9:13 AM
Nikhil kokate
Nikhil kokate - avatar