Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:33:54+00:00 2026-06-06T06:33:54+00:00

I am building a simple particle system and want to use a single array

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:33:56+00:00Added an answer on June 6, 2026 at 6:33 am

    Yeah, you’d have to write your own. It’s so simple it’s really silly, but its performance will scream in comparison to simply using malloc() and free() all the time….

    static const int maxParticles = 1000;
    
    static Particle particleBuf[maxParticles]; // global static array
    
    static Particle* headParticle;
    
    void initParticleAllocator()
    {
        Particle* p = particleBuf;
        Particle* pEnd = &particleBuf[maxParticles-1];
        // create a linked list of unallocated Particles
        while (p!=pEnd)
        {
            *((Particle**)p) = p+1;
            ++p;
        }
        *((Particle**)p) = NULL; // terminate the end of the list
        headParticle = particleBuf; // point 'head' at the 1st unalloc'ed one
    }
    
    Particle* ParticleAlloc()
    {
        // grab the next unalloc'ed Particle from the list
        Particle* ret = headParticle;
        if (ret)
            headParticle = *(Particle**)ret;
        return ret; // will return NULL if no more available
    }
    
    void ParticleFree(Particle* p)
    {
        // return p to the list of unalloc'ed Particles
        *((Particle**)p) = headParticle;
        headParticle = p;
    }
    

    You could modify the approach above to not start with any global static array at all, and use malloc() at first when the user calls ParticleAlloc(), but when Particles are returned, don’t call free() but instead add the returned ones to the linked list of unalloc’ed particles. Then the next caller to ParticleAlloc() will get one off the list of free Particles rather than use malloc(). Any time there are no more on the free list, your ParticleAlloc() function could then fall back on malloc(). Or use a mix of the two strategies, which would really be the best of both worlds: If you know that your user will almost certainly be using at least 1000 Particles but occasionally might need more, you could start with a static array of 1000 and fall back on calling malloc() if you run out. If you do it that way, the malloc()’ed ones do not need special handling; just add them to your list of unalloc’ed Particles when they come back to ParticleFree(). You do NOT need to bother calling free() on them when your program exits; the OS will free the process’es entire memory space, so any leaked memory will clear up at that point.

    I should mention that since you question was tagged “C” and not “C++”, I answered it in the form of a C solution. In C++, the best way to implement this same thing would be to add “operator new” and “operator delete” methods to your Particle class. They would contain basically the same code as I showed above, but they override (not overload) the global ‘new’ operator and, for the Particle class only, define a specialized allocator that replaces global ‘new’. The cool thing is that users of Particle objects don’t even have to know that there’s a special allocator; they simply use ‘new’ and ‘delete’ as normal and remain blissfully unaware that their Particle objects are coming from a special pre-allocated pool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building simple HTML pages and I want to use this kind of
I'm building a simple C++ program and I want to temporarily substitute a system
I am building a simple Django app that will use scribd to display documents.
I'm building a simple order system and want to send an email after the
I am building simple notificiation system and I just wanted to know what is
I am building a simple Point of Sale system for my project. This system
I'm building simple application on as3. Kind of starship game. What I want to
I am building a simple shop cart and I am getting the following array
I'm building a simple chat server in java , where users can have private
I am building a simple website that helps students and instructors in universities. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.