Coder Social home page Coder Social logo

better-malloc-c's Introduction

The file memlib.h contains the function my_malloc() and my_free(), standings for the memory allocation and freeing programs included in the standard C library.
Also included is the function morecore(), which requests more memory from the system in the case where the user attempts to allocate memory on a full heap.

The functions themselves are based heavily on the functions written in K&R C, Second Edition, on pages 185-189.
I also made use of the stackoverflow question here: http://stackoverflow.com/questions/13159564/explain-this-implementation-of-malloc-from-the-kr-book.
This was not my question, but the code and explanation provided by stackoverflow.com/users/584518/lundin there were extremely helpful in building off of and better understanding the code included in K&R C.

The function my_malloc allocates memory for the user when called, allocates a block of the requested size, and returns a pointer to it.
Like the regular malloc function, it will return a NULL pointer when it fails.
It will fail in the case where there is no more dynamic memory available to be allocated
(i.e., where morecore determines that that there is less memory available than the minimum size for a block.)

my_free is passed a pointer to a block of memory. It first checks to see if the block is currently allocated, or has been freed already, using recognition patters the library functions write into the block at different points.
If my_free detects that it has neither been allocated nor freed, it will refuse to free it and assume that it was allocated or freed by another program.
If it detects that it has already been freed by my_free, it will refuse to free it again.
If it detects that it is currently allocated, it will free it by placing the block in the free list.

The ErrorHandler function is used to catch errors thrown in the other library functions and output them to the user with useful information.
The function catches the above errors in my_free, as well as the error when there is no more dynamic memory available to be allocated.

 A test program mallocme.c is included.

better-malloc-c's People

Contributors

jkotzker avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.