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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:02:39+00:00 2026-05-26T16:02:39+00:00

I’m attempting to create a type-agnostic vector in C++ that is distinguished by two

  • 0

I’m attempting to create a type-agnostic vector in C++ that is distinguished by two things. First, it allocates the memory in the object itself, at least until a certain point, rather than maintaining the actual array of objects on the heap. Secondly, it can’t use C++’s copy/assign constructors, which appears to slow down the code and is not necessary.

While looking through the codebase I maintain on my computer, I found a class in LLVM’s codebase that pretty much perfectly describes what I’m looking for: SmallVector.h. Being relatively new to C++, I’m not entirely sure why some of the design decisions were made. For instance, why is the array allocated in terms of U rather than T? The comment gives a clue:

If T has a ctor or dtor, we don’t want it to be automatically run, so we need to represent the space as something else. An array of char would work great, but might not be aligned sufficiently. Instead we use some number of union instances for the space, which guarantee maximal alignment.

U, of course, refers to the following union:

union U {
    double D;
    long double LD;
    long long L;
    void *P;
} FirstEl;

So, I guess, here are my true questions: Why does allocating an array of T imply that constructors/destructors are called? Is there any way to move c++ object instances around, i.e. in and out of the vector, without calling these constructors/destructors? I guess I can just use LLVM’s SmallVector implementation, but I hate using code without understanding it.

Best,
Duane

  • 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-26T16:02:40+00:00Added an answer on May 26, 2026 at 4:02 pm

    You should look at the basic machinery behind the standard library allocators, which deal with a lot of the questions that you probably have!

    Here’s the basic allocation principle. We separate memory allocation and object construction. The big obstacle is, as you observed, that the memory should be aligned correctly for the object:

    // getting memory
    void * p = malloc(1000);  // version 1, system's allocator
    char q[1000];             // automatic array, this is also memory :-)
    
    // constructing an object
    T * m_x1 = ::new (p) T;   // default-initialized
    T * m_x2 = ::new (q) T(); // value-initialized
    T * m_x3 = ::new (q + sizeof(T)) T(1, 'a'); // some specific constructor
    
    // destroying the objects:
    m_x1->~T();
    m_x2->~T();
    m_x3->~T();
    

    To do what you have in mind, you could take the char array q that I used and make it a member of your class. That is, the class always carries around with it some memory in which to construct objects.

    The actual object construction is done with the global placement-new expression. Recall that objects thus constructed must be destroyed manually (which will be your responsibility).

    The standard library allocators do pretty much something like that.

    Separating memory allocation and object construction is at the heart of any sort of advanced memory managing, responsibility owning class.

    Note that once an object is constructed at a specific address, you must not move the memory around. The object may very well depend on its own location in memory! The only valid way to move objects around is to copy/move-construct a new object.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.