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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:06:48+00:00 2026-06-13T09:06:48+00:00

I would like to allocate and array of classes on the stack without calling

  • 0

I would like to allocate and array of classes on the stack without calling the constructor. The following example clarifys:

template<class t,int SetNum> class set
{
private:
    t Storage[SetNum];
};

class myClass
{
private:
    int* Array;
public:
    myClass()
    {
        Array=new int[10];
    }
}
int main()
{
    set<myClass,10> Set;
}

I do not want to allocate the 10 new ints for Array that occurs when myClass‘s constructor is called, but still want the space allocated for myClass.

  • 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-13T09:06:49+00:00Added an answer on June 13, 2026 at 9:06 am

    You have to have an array of unsigned chars (or such) to use as “backing storage” for your elements, and then call the placement new operator (see e.g. here) to construct your instances there (which, by the way, is what std::vector already does).

    Warning: if you use placement new you have the responsibility to deallocate manually the objects you created with it, calling explicitly the destructor; also, the pointer you pass to placement new must be properly aligned for the objects you are creating, otherwise bad stuff may happen.

    See also this question.


    Example of a twisted mix of std::array and std::vector built with the techniques described (requires C++11 for the union trick to work):

    #include <cstddef>
    #include <memory>
    #include <stdexcept>
    #include <iostream>
    
    template<typename T, std::size_t N>
    class array_noinit
    {
        union storage_helper
        {
        private:
            // this member assures that the storage is properly aligned
            T t;
        public:
            unsigned char storage[sizeof(T)*N];
    
            // needed because T's constructor/destructor is implicitly deleted
            storage_helper() { };
            ~storage_helper() { };
        };
    
        storage_helper s;
    
        std::size_t _size;
        T * _storage;
    public:
        array_noinit() :
            _size(0), _storage((T*)s.storage)
        {}
    
        ~array_noinit()
        {
            while(_size>0)
                pop_back();
        }
    
        void push_back(const T & elem)
        {
            if(_size>=N)
                throw std::runtime_error("Not enough capacity.");
            new(_storage+_size) T(elem);
            _size++;
        }
    
        void pop_back()
        {
            if(_size>0)
            {
                _size--;
                _storage[_size].~T();
            }
        }
    
        T & at(std::size_t Idx)
        {
            if(Idx>=_size)
                throw std::out_of_range("Idx out of range.");
            return _storage[Idx];
        }
    
        std::size_t size() const
        {
            return _size;
        }
    
        std::size_t capacity() const
        {
            return N;
        }
    };
    
    class A
    {
        int _i;
    public:
        A(int i) : _i(i)
        {
            std::cout<<"In A constructor - "<<_i<<"\n";
        }
    
        A(const A & right)
            : _i(right._i)
        {
            std::cout<<"In A copy constructor - "<<_i<<"\n";
        }
    
        ~A()
        {
            std::cout<<"In A destructor - "<<_i<<"\n";
        }
    };
    
    int main()
    {
        std::cout<<"With ints\n";
        array_noinit<int, 4> arr;
        arr.push_back(1);
        arr.push_back(2);
        arr.push_back(3);
        arr.push_back(4);
    
        for(std::size_t i=0; i<4; i++)
            std::cout<<arr.at(i)<<" ";
        std::cout<<"\n";
    
        std::cout<<"With a class - we don't add anything\n";
        array_noinit<A, 10> arr2;
    
        std::cout<<"With a class - now we add stuff\n";
        array_noinit<A, 10> arr3;
        arr3.push_back(A(1));
        arr3.push_back(A(2));
        arr3.push_back(A(3));
        return 0;
    }
    

    Output:

    With ints
    1 2 3 4 
    With a class - we don't add anything
    With a class - now we add stuff
    In A constructor - 1
    In A copy constructor - 1
    In A destructor - 1
    In A constructor - 2
    In A copy constructor - 2
    In A destructor - 2
    In A constructor - 3
    In A copy constructor - 3
    In A destructor - 3
    In A destructor - 3
    In A destructor - 2
    In A destructor - 1
    

    Edit there’s a much simpler way to get aligned storage.

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

Sidebar

Related Questions

I would like to know how to allocate sequential memory for an array of
I would like to allocate space (dynamic size) with a byte array and get
I would like to write some class which maps int to something using templates.
I would like to dynamically allocate memory from the machine_init function in my arm
I have a structure that has an array of pointers. I would like to
I would like to create something like a pointer to a 2 dimensional array
I would like to read coordinates from an from an input file. An example
I would like to compile the following C program for 32- and 64-bit systems.
For a program, I would like to make an array copy of the arguments
I am writing C code and I would like to heap allocate 512*256 bytes.

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.