Skip to content

This program is a special version of the malloc and free library routines that checks on common heap allocation and usage mistakes.

Notifications You must be signed in to change notification settings

xzheng97/safe_malloc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CS 537 Programming Assignment #4

This program is a special version of the malloc and free library routines that checks on common heap allocation and usage mistakes. This version of malloc and free will be called malloc537 and free537. The program also has one extra function, memcheck537, for checking if a heap allocated memory address range is safe to use.

NOTE: There is no main.c, users should write their own main.c file and can simply call malloc537/realloc537/free537/memcheck537 in order to use them.

Methods functions:

void *malloc537(size_t size): 
 
In addition to actually allocating the memory by calling malloc(), this function will record a tuple (addri, leni), for the memory that you allocate in the heap. (If the allocated memory was previously freed, this will be a bit more complicated.) You will get the starting address, addri, from the return value from malloc() and the length, leni, from the size parameter. You can check the size parameter for zero length (this is not actually an error, but unusual enough that it is worth reporting).

void free537(void *ptr): 
 
This function will first check to make sure that freeing the memory specified by ptr makes sense, then will call free() to do the actual free. Some of the error conditions that you should check for include:
Freeing memory that has not be allocated with malloc537().
Freeing memory that is not the first byte of the range of memory that was allocated.
Freeing memory that was previously freed (double free).
When an error is detected, then print out a detailed and informative error message and exit the program (with a -1 status). If all checks pass,then this function indicates that the tuple for addr = ptr is no longer allocated, and then calls free().

void *realloc537(void *ptr, size_t size): 
 
If ptr is NULL,then this follows the specification of malloc537() above. If size is zero and ptr is not NULL,then this follows the specification of free537() above. Otherwise, in addition to changing the memory allocation by calling realloc(), this function will first check to see if there was a tuple for the (addr = ptr, and removes that tuple, then adds a new one where addr is the return value from realloc() and len is size

void memcheck537(void *ptr, size_t size): 
 
This function checks to see the address range specified by address ptr and length size are fully within a range allocated by malloc537() and memory not yet freed by free537(). When an error is detected, then print out a detailed and informative error message and exit the program (with a -1 status). 

Authors

Youmin Han and Xianjie Zheng worked on the construction and improvement of the code together.

Online sources:  Red Black Tree Algorithms
                  https://www.geeksforgeeks.org/c-program-red-black-tree-insertion/
                  https://towardsdatascience.com/red-black-binary-tree-maintaining-balance-e342f5aa6f5
                  https://github.com/amitbansal7/Data-Structures-and-Algorithms/edit/master/9.Red-Black-tree/RedBlackTrees.c
                  https://www.geeksforgeeks.org/avl-tree-set-1-insertion/
                  https://www.cs.auckland.ac.nz/software/AlgAnim/red_black.html

About

This program is a special version of the malloc and free library routines that checks on common heap allocation and usage mistakes.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published