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 850353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:15:06+00:00 2026-05-15T07:15:06+00:00

I’m building an application which will have dynamic allocated objects of type A each

  • 0

I’m building an application which will have dynamic allocated objects of type A each with a dynamically allocated member (v) similar to the below class

class A {
int a;
int b;
int* v;
};

where:

  • The memory for v will be allocated in the constructor.
  • v will be allocated once when an object of type A is created and will never need to be resized.
  • The size of v will vary across all instances of A.

The application will potentially have a huge number of such objects and mostly need to stream a large number of these objects through the CPU but only need to perform very simple computations on the members variables.

  • Could having v dynamically allocated could mean that an instance of A and its member v are not located together in memory?
  • What tools and techniques can be used to test if this fragmentation is a performance bottleneck?
  • If such fragmentation is a performance issue, are there any techniques that could allow A and v to allocated in a continuous region of memory?
  • Or are there any techniques to aid memory access such as pre-fetching scheme? for example get an object of type A operate on the other member variables whilst pre-fetching v.
  • If the size of v or an acceptable maximum size could be known at compile time would replacing v with a fixed sized array like int v[max_length] lead to better performance?

The target platforms are standard desktop machines with x86/AMD64 processors, Windows or Linux OSes and compiled using either GCC or MSVC compilers.

  • 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-15T07:15:07+00:00Added an answer on May 15, 2026 at 7:15 am

    If you have a good reason to care about performance…

    Could having v dynamically allocated could mean that an instance of A and its member v
    are not located together in memory?

    If they are both allocated with ‘new’, then it is likely that they will be near one another. However, the current state of memory can drastically affect this outcome, it depends significantly on what you’ve been doing with memory. If you just allocate a thousand of these things one after another, then the later ones will almost certainly be “nearly contiguous”.

    If the A instance is on the stack, it is highly unlikely that its ‘v’ will be nearby.

    If such fragmentation is a performance issue, are there any techniques that could
    allow A and v to allocated in a continuous region of memory?

    Allocate space for both, then placement new them into that space. It’s dirty, but it should typically work:

    char* p = reinterpret_cast<char*>(malloc(sizeof(A) + sizeof(A::v)));
    char* v = p + sizeof(A);
    A* a = new (p) A(v);
    
    // time passes
    
    a->~A();
    free(a);
    

    Or are there any techniques to aid memory access such as pre-fetching scheme?

    Prefetching is compiler and platform specific, but many compilers have intrinsics available to do it. Mind- it won’t help a lot if you’re going to try to access that data right away, for prefetching to be of any value you often need to do it hundreds of cycles before you want the data. That said, it can be a huge boost to speed. The intrinsic would look something like __pf(my_a->v);

    If the size of v or an acceptable maximum size could be known at compile time
    would replacing v with a fixed sized array like int v[max_length] lead to better
    performance?

    Maybe. If the fixed size buffer is usually close to the size you’ll need, then it could be a huge boost in speed. It will always be faster to access one A instance in this way, but if the buffer is unnecessarily gigantic and largely unused, you’ll lose the opportunity for more objects to fit into the cache. I.e. it’s better to have more smaller objects in the cache than it is to have a lot of unused data filling the cache up.

    The specifics depend on what your design and performance goals are. An interesting discussion about this, with a “real-world” specific problem on a specific bit of hardware with a specific compiler, see The Pitfalls of Object Oriented Programming (that’s a Google Docs link for a PDF, the PDF itself can be found here).

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

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Standards are, by definition, arbitrary restrictions on potential solutions. By… May 15, 2026 at 1:53 pm
  • Editorial Team
    Editorial Team added an answer Yes i want to answer my own question. What i… May 15, 2026 at 1:53 pm
  • Editorial Team
    Editorial Team added an answer You can set a condition for your CA execution: <InstallExecuteSequence>… May 15, 2026 at 1:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.