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

  • Home
  • SEARCH
  • 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 7583283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:38:23+00:00 2026-05-30T18:38:23+00:00

I have a class as follows: typedef struct grid_cell_type { int x; int y;

  • 0

I have a class as follows:

typedef struct grid_cell_type {
int x;
int y;
grid_cell_type(int x0, int y0){
    x=x0;
    y=y0;
}

} grid_cell;

I’ll be pumping approximately 100 million of these through a queue.

Right now, this happens as follows:

my_queue.push(new grid_cell(x0,y0));

The individual piece-wise allocation of all these objects seems as though it is probably not as quick as some mass-allocation.

Any thoughts as to the best strategy to pursue here?

  • 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-05-30T18:38:25+00:00Added an answer on May 30, 2026 at 6:38 pm

    These are small and self-contained objects – put them directly in the queue instead of putting the pointers.

    • In fact, on a 64-bit system and assuming int is 32-bit (which it is, for example, under Visual C++), the pointer will be as large as the object itself! So even if you have a bulk allocator, you still pay this price.
    • The general memory allocator will not just be expensive time-wise, it will also have a per-object overhead, which in this case will dwarf the object itself (does not apply for bulk allocator).

    While you could devise a fairly efficient “bulk” allocation scheme, I think it’s simpler to sidestep the issue and altogether avoid the individual object allocations.

    — EDIT —

    You can push elements to the std::queue like this:

    struct grid_cell {
    
        grid_cell(int x0, int y0) {
            x=x0;
            y=y0;
        }
    
        int x;
        int y;
    
    };
    
    // ...
    
    std::queue<grid_cell> q;
    
    q.push(grid_cell(0, 0));
    q.push(grid_cell(0, 1));
    q.push(grid_cell(0, 2));
    q.push(grid_cell(1, 0));
    q.push(grid_cell(1, 1));
    q.push(grid_cell(1, 2));
    

    For the std::priority_queue, you’d need to decide how you want to order the elements.

    — EDIT 2 —

    @Richard Your code is quite different.

    • For each push, your code would allocate a new block of dynamic memory, construct the object in it (i.e. assign x and y) and then push the pointer to that block of memory to the queue.
    • My code constructs the object directly in its “slot” within the larger memory block that was pre-allocated by the queue itself. And as you already noted, few big allocations
      are better than many small ones.

    Your code is:

    1. prone to memory leaks
    2. you pay for extra storage for pointers,
    3. prone to memory fragmentation and
    4. there is a per-object overhead, as I already mentioned.

    A specialized bulk allocator could remove the last two problems but why not remove them all?

    — EDIT 3 —

    As for speed, the general dynamic memory allocation is expensive (about 40-50 machine instructions for best allocators).

    The specialized block allocator would be much faster, but you still have an issue of memory latency: keeping everything nicely together is guaranteed to achieve better cache locality and be much more suitable for CPU’s prefetching logic than repeatedly “jumping” between the queue and the actual objects by de-referencing pointers.

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

Sidebar

Related Questions

If I have a class as follows class Example_Class { private: int x; int
Assuming I have a class A as follows: class A{ int id; int getId(){};
I have a Point2D class as follows: class Point2D{ int x; int y; public:
I have a class as follows: private class LanePair { public int cameraNumber; public
I have a struct A that is defined as follows: typedef struct A {
I have a class as follows: Class A { virtual int doSomethingCool() = 0;
I have a class A as follows: class A { public: A() { printf(A
I have a class as follows :- interface IFilterCondition { List<Name> ApplyFilter(List<Name> namesToFilter); }
I have a class that inherits from a generic dictionary as follows: Class myClass
I have the ViewValue class defined as follows: class ViewValue { private Long id;

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.