В каком случае в процессе выполнения программы выполняется перераспределение выделенной памяти с целью ее увеличения?
(Отметьте один правильный вариант ответа.)
Варианты ответа
#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <string.h>int main(void) { int n, m; char *ptr; printf("\n Enter a dimention of character array: "); scanf_s("%d", &n); _flushall(); ptr = (char *)malloc((n+1)*sizeof(char)); if (!ptr) { printf("\n\t 1st Error! "); printf("\n\n Press any key: "); _getch(); return -1; } printf(" Enter a character array of no more than %d characters: ", n); gets_s(ptr, n+1); m = strlen(ptr); printf("\n Start line:\n"); printf(" %s\n", ptr); ptr = (char *)realloc(ptr, (m+2)*sizeof(char)); if (!ptr) { printf("\n\t 2nd Error! "); printf("\n\n Press any key: "); _getch(); return -1; } strcat_s(ptr, m+2, "!"); printf("\n Start line and character \"%c\":\n", '!'); printf(" %s\n", ptr); free (ptr); printf("\n\n Press any key: "); _getch(); return 0; }
#include <stdio.h>#include <stdlib.h>#define N 10int main(void){ int i, n = 0; int arr1[N] = {1,2,3,4,5}, *arr; arr = (int *)calloc(N, sizeof(int)); if (!arr) { printf("\nERROR! Out of memory in calloc() function. Press any key: "); _getch(); exit(1); }; for (i=0; i<N; i++) { arr[i] = i+1; n++; printf("arr[%d] = %d\n", i, arr[i]); } arr = (int *)realloc(arr, (N+2)*sizeof(int)); if (!arr) { printf("\nERROR! Out of memory in realloc() function. Press any key: "); _getch(); exit(1); }; for (i=N; i<N+2; i++) { arr[i] = i+1; printf("arr[%d] = %d\n", i, arr[i]); } free(arr); return 0;} (Верный ответ)
#include <stdio.h>#include <stdlib.h>#define N 10int main(void){ int i, n = 0; int arr1[N] = {1,2,3,4,5}, *arr; arr = (int *)calloc(N, sizeof(int)); if (!arr) { printf("\nERROR! Out of memory in calloc() function. Press any key: "); _getch(); exit(1); }; for (i=0; i<5; i++) { arr[i] = i+1; n++; } arr = (int *)realloc(arr, n*sizeof(int)); if (!arr) { printf("\nERROR! Out of memory in realloc() function. Press any key: "); _getch(); exit(1); }; free(arr); return 0;}