A C program has a live allocation for 9 integer elements, all initialized, and p points to its beginning. The program creates saved as an alias, another pointer referring to the same allocation, then executes this fragment. The requested byte count is positive and representable. Select every statement guaranteed by C's allocation rules.
int *saved = p; int *tmp = realloc(p, 16 * sizeof *p); if (tmp != NULL) { p = tmp; }
After success, the original nine values remain accessible through the updated p.
After success, the seven added integer elements are automatically initialized to zero.
After failure, the original nine values remain accessible through the unchanged p.
After success, saved remains usable whenever the returned address is unchanged.
Unlock full course notes by signing up
Account for live allocation sizes
A C analysis routine begins with no dynamically allocated storage and performs these events in order. Every allocation and resize succeeds. An integer occupies 4 bytes and a double occupies 8 bytes. Ignore allocator overhead and any temporary internal storage used during resizing.
Stage A: Allocate an array of 7 integers.
Stage B: Allocate a separate array of 11 doubles, retaining the integer array.
Sequence a safe array-growth procedure
A C routine owns a live integer array through p and records its element capacity in capacity. It must grow the array to n elements, where n is positive and greater than capacity. All existing elements are initialized, and the required headers are included. The variables n, capacity, and bytes have type size_t, and tmp has type int *.
Arrange this procedure in execution order. The routine must check that the requested byte count is representable before performing the multiplication. A rejected size or failed resize exits the procedure while retaining the original array. The final initialization step must use the updated owning pointer p.
Count the cost of a row-by-row grid
A function builds a grid of double values with rows = 6 and cols = 50, using one allocation for an array of row pointers and one allocation per row:
On this system sizeof(double) is 8 bytes and sizeof(double *) is 8 bytes. Count requested bytes only, ignoring any allocator overhead; a call that returns NULL reserves nothing. Give each answer as a whole number:
the bytes requested by the row-pointer allocation;