Allocate from buffer in C

Posted by Grimless on Stack Overflow See other posts from Stack Overflow or by Grimless
Published on 2012-06-24T21:01:37Z Indexed on 2012/06/24 21:16 UTC
Read the original article Hit count: 136

I am building a simple particle system and want to use a single array buffer of structs to manage my particles. That said, I can't find a C function that allows me to malloc() and free() from an arbitrary buffer. Here is some pseudocode to show my intent:

Particle* particles = (Particle*) malloc( sizeof(Particle) * numParticles );
Particle* firstParticle = <buffer_alloc>( particles );
initialize_particle( firstParticle );
// ... Some more stuff
if (firstParticle->life < 0)
    <buffer_free>( firstParticle );

// @ program's end
free(particles);

Where <buffer_alloc> and <buffer_free> are functions that allocate and free memory chunks from arbitrary pointers (possibly with additional metadata such as buffer length, etc.). Do such functions exist and/or is there a better way to do this? Thank you!

© Stack Overflow or respective owner

Related posts about c

    Related posts about memory-management