LearnFrenzy
Sign in Take a free test

Memory Allocation · Exercise 3 of 4

Memory Allocation Find the Error

  • 8 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Eight real-world memory bugs: leaks, double frees, overflows, invalid frees and unsafe realloc. Most of them crash under AddressSanitizer, which is how the answers were confirmed.

Question 1 Easy

The program below has a memory bug. Which statement correctly describes it?

main.c
#include <stdlib.h>

int main(void) {
    char *buf = malloc(32);
    buf = malloc(64);
    free(buf);
    return 0;
}
A malloc() cannot be called twice
B free(buf) frees both blocks
C The first 32-byte block is lost when buf is overwritten, so it can never be freed (a leak)
D There is no problem
Question page & discussion
Question 2 Easy

The program below has a memory bug. Which statement correctly describes it?

main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = malloc(sizeof *p);
    if (p == NULL)
        return 1;
    *p = 5;
    printf("%d\n", *p);
    free(p);
    free(p);
    return 0;
}
A printf() must come after free()
B free() needs the size of the block
C p is freed twice; the second free() corrupts the allocator (undefined behaviour)
D There is no problem
Question page & discussion
Question 3 Medium

The program below has a memory bug. Which statement correctly describes it?

main.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    size_t n = SIZE_MAX / 8;
    double *data = malloc(n * sizeof *data);
    data[0] = 1.5;
    printf("%f\n", data[0]);
    free(data);
    return 0;
}
A double cannot be allocated
B SIZE_MAX is not defined
C There is no problem
D The request is impossibly large, malloc() fails, and data[0] writes through a NULL pointer – the result was never checked
Question page & discussion
Question 4 Easy

The program below has a memory bug. Which statement correctly describes it?

main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char *s = "LearnFrenzy";
    char *copy = malloc(strlen(s));
    if (copy == NULL)
        return 1;
    strcpy(copy, s);
    puts(copy);
    free(copy);
    return 0;
}
A strcpy() cannot copy into heap memory
B malloc(strlen(s)) has no room for '\0', so strcpy() writes one byte past the block
C copy must be const
D There is no problem
Question page & discussion
Question 5 Medium

The program below has a memory bug. Which statement correctly describes it?

main.c
#include <stdlib.h>

int main(void) {
    int x = 10;
    int *p = &x;
    free(p);
    return 0;
}
A p points to a stack variable; free() may only receive pointers from malloc/calloc/realloc
B x must be initialised with malloc first
C free() needs a cast to void *
D There is no problem
Question page & discussion
Question 6 Medium

The program below has a memory bug. Which statement correctly describes it?

main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = malloc(4 * sizeof *p);
    if (p == NULL)
        return 1;
    for (int i = 0; i < 4; i++)
        *p++ = i;
    free(p);
    return 0;
}
A p was advanced past the start of the block, so free(p) does not receive the address malloc() returned
B *p++ = i is invalid
C The loop writes past the end
D There is no problem
Question page & discussion
Question 7 Hard

The program below has a memory bug. Which statement correctly describes it?

main.c
#include <stdio.h>
#include <stdlib.h>

int grow(int **p, size_t n) {
    *p = realloc(*p, n * sizeof **p);
    return *p != NULL;
}

int main(void) {
    int *data = malloc(4 * sizeof *data);
    if (data == NULL || !grow(&data, 1000))
        return 1;
    data[999] = 1;
    printf("%d\n", data[999]);
    free(data);
    return 0;
}
A If realloc() fails it returns NULL; assigning that to *p loses the old block, which then leaks
B realloc() cannot grow a block
C data[999] is out of bounds
D There is no problem
Question page & discussion
Question 8 Medium

The program below has a memory bug. Which statement correctly describes it?

main.c
#include <stdio.h>
#include <stdlib.h>

struct record {
    char name[24];
    int score;
};

int main(void) {
    struct record *r = malloc(sizeof r);
    if (r == NULL)
        return 1;
    r->score = 90;
    printf("%d\n", r->score);
    free(r);
    return 0;
}
A struct record cannot be allocated
B sizeof r is the pointer size (8), not the 28-byte struct, so r->score is outside the block
C r->score should be r.score
D There is no problem
Question page & discussion
Up next · Exercise 4Memory Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links