Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #define QUANTITY 512
- // Piece size
- #define PIECE (1024*1024)
- void *pieces[QUANTITY];
- void allocate()
- {
- for(size_t i = 0; i < QUANTITY; ++i)
- {
- pieces[i] = malloc(PIECE);
- memset(pieces[i], 0xff, PIECE);
- }
- }
- void fragment()
- {
- for(size_t i = 0; i < QUANTITY; i+=2)
- {
- free(pieces[i]);
- pieces[i] = NULL;
- }
- }
- void deallocate()
- {
- for(size_t i = 0; i < QUANTITY; ++i)
- free(pieces[i]);
- }
- int main()
- {
- // Allocate, fragment and deallocate memory and see
- // if system virtual memory is really freed by deallocation
- for(;;)
- {
- allocate();
- puts("Allocated");
- getchar();
- fragment();
- puts("Fragmented");
- getchar();
- deallocate();
- puts("Deallocated");
- getchar();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment