Question 4
Back to Exercise
What is the output of the following C program on a 64-bit system?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int *a = malloc(4 * sizeof *a);
if (a == NULL)
return 1;
memset(a, 0, 4 * sizeof *a);
a[1] = -1;
memset(a + 2, 0xFF, sizeof *a);
printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
free(a);
return 0;
}
- A 0 -1 -1 0 Correct
- B 0 -1 255 0
- C 0 -1 -1 -1
- D 0 -1 0xFF 0
Correct Answer: Option A
The first memset zeroes all 16 bytes. The second sets the 4 bytes of a[2] to 0xFF, and an int whose bits are all 1 is −1 in two's complement. 0 and −1 are the only int values memset can produce this way.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!