Descriptions of test program:
memtest.c
tests:
- Correctness of
mymalloc()
andmyfree()
- Error detection for invalid
free()
calls - Proper coalescing of free chunks
- Edge cases such as
malloc(0)
andfree(NULL)
- Purpose: Checks whether
mymalloc()
properly allocates memory andmyfree()
correctly deallocates it. - Procedure:
- Allocate
OBJECTS
small memory chunks. - Fill each chunk with a unique byte pattern.
- Verify the memory is correctly assigned.
- Free all allocated memory.
- Allocate
- Expected Behavior:
- Each chunk should retain its value.
- Memory should be correctly freed without corruption.
- Leak detector should report no leaks.
- Purpose: Tests whether
mymalloc()
correctly merges adjacent free chunks when a large allocation is requested. - Procedure:
- Allocate and free
OBJECTS
small chunks sequentially. - Attempt to allocate a large chunk equal to the total size of all freed memory.
- Allocate and free
- Expected Behavior:
- Allocation should succeed only if forward coalescing is implemented.
- If
mymalloc()
does not coalesce properly, allocation will fail.
- Purpose: Ensures
myfree()
correctly merges adjacent free chunks. - Procedure:
- Allocate
OBJECTS
small memory chunks. - Free them in reverse order.
- Attempt to allocate a single large chunk.
- Allocate
- Expected Behavior:
- Allocation should succeed if myfree() coalesces forward chunks.
- If
myfree()
does not merge properly, allocation will fail.
- Purpose: Ensures
mymalloc()
correctly detects and rejects requests larger than available memory. - Procedure:
- Request a block larger than
MEMLENGTH - HEADERSIZE
. - Request a block equal to
MEMLENGTH
. - Request
malloc(0)
.
- Request a block larger than
- Expected Behavior:
- Large allocations should return NULL with an error message.
malloc(0)
should return NULL.
- Purpose: Ensures
mymalloc()
does not allocate when memory is exhausted. - Procedure:
- Allocate
OBJECTS + 1
chunks. - Free all
OBJECTS
valid chunks. - Check the last allocation.
- Allocate
- Expected Behavior:
malloc()
should fail when requesting memory beyond capacity.- If it succeeds, there's a bug in allocation logic.
- Purpose: Ensures
myfree()
detects and prevents freeing non-heap pointers. - Procedure:
- Call
free()
on an unallocated stack variable. - Call
free(NULL)
. - Call
free()
on an arbitrary address.
- Call
- Expected Behavior:
free(&x)
should terminate with an error.free(NULL)
should be ignored (no crash).- Freeing an invalid pointer should terminate with an error.
- Purpose: Ensures
myfree()
rejects freeing an address that is not at the start of a chunk. - Procedure:
- Allocate a chunk.
- Try to
free(ptr + 1)
.
- Expected Behavior:
myfree()
should detect and reject the invalid free.
- Purpose: Ensures
myfree()
detects double frees. - Procedure:
- Allocate a chunk.
- Free it twice.
- Expected Behavior:
- The second
free()
should trigger an error and terminate.
- The second
mymalloc()
splits free chunks when possible.- If the chunk is only slightly larger than requested, it allocates the entire chunk.
myfree()
only merges free chunks in the forward direction.- It does not merge with previous chunks.
Case | Behavior |
---|---|
malloc(0) |
Returns NULL |
free(NULL) |
Does nothing |
malloc(large_size) |
Fails with error message |
free(outside_heap_ptr) |
Fails with error and exits |
free(not_chunk_start_ptr) |
Fails with error and exits |
double free() |
Fails with error and exits |