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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:56:36+00:00 2026-06-07T04:56:36+00:00

Possible Duplicate: Can placement new for arrays be used in a portable way? I

  • 0

Possible Duplicate:
Can placement new for arrays be used in a portable way?

I want to allocate array of object T and initialize object using object constructor. This is easy using c++ new :

T * pointerT = new T [arraySize];

It will call T constructor for all of the arraySize objects.
However, for some reason I have to use C memalign instead of new. In this case I end up to use following code

T * pointerT = (T*) memalign(64,arraySize * sizeof(T)); 
new (pointerT) T(); 

new (pointerT) T() calls T constructor only one time. However, I need to call T constructor for all objects not only the first one.

I do appreciate your help.

  • 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-07T04:56:38+00:00Added an answer on June 7, 2026 at 4:56 am

    Do new (pointerT) T() in a loop. Please keep pointerT inside an object whose destructor destroys the objects and calls free (call it eg. aligned_vector), and in the constructor, do:

    ptrdiff_t k = 0;
    
    try
    {
        for (; k < n; k++)
            new (pointerT + k) T();
    }
    
    catch (...)
    {
        for (; k > 0; k--) (pointerT + k)->~T();
        free(pointerT);
        throw;
    }
    

    This way, if a construction fails, you can bail out transactionally and not leak memory or resources.

    For this purpose, the simplest in terms of reusability would be to implement your own alignment aware allocator and use std::vector, which takes care of exception safety (and many other goodies) for you.

    Here is a sample allocator, C++11, with compile time alignment specification (please suggest enhancements and corrections):

    template <typename T, size_t align>
    struct aligned_allocator
    {
        typedef T value_type;
        typedef T* pointer;
        typedef const T* const_pointer;
        typedef T& reference;
        typedef const T& const_reference;
        typedef size_t size_type;
        typedef ptrdiff_t difference_type;
    
        template <typename U>
        struct rebind { typedef aligned_allocator<U, align> other; };
    
        T* address(T& t) { return &t; }
    
        T* allocate(size_t n, const T* = 0) 
        {
            if (T* ans = memalign(align, n * sizeof(T))) return ans;
            else throw std::bad_alloc();
        }
    
        T* deallocate(T* p, size_t) { free(p); }
    
        size_t max_size() const 
        {
            return size_t(-align) / sizeof(T); 
        }
    
        template <typename U, typename... Args>
        void construct(U* p, Args&&... args)
        {
            ::new((void *)p U(std::forward<Args>(args)...);
        }
    
        template <typename U>
        void destroy(U* p) { p->~U(); }
    };
    

    Sample usage: std::vector<T, aligned_allocator<T, 64>> v(42); constructs a vector with aligned storage and 64 elements.

    You can also do, in C++11

    template <typename T, size_t align>
    using aligned_vector = std::vector<T, aligned_allocator<T, align>>;
    

    and you can now use

    aligned_vector<MyType, 64> v;
    

    and enjoy an exception safe, move aware, iterators aware, range-based-for-loop aware, etc, vector with aligned storage.

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

Sidebar

Related Questions

Possible Duplicate: Can I comment a JSON file? We are using a .json file
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: Can you do Desktop Development using JavaScript? I am very familiar with
Possible Duplicate: Can I set a breakpoint on 'memory access' in GDB? I want
Possible Duplicate: Can Google Maps/Places 'autocomplete' API be used via AJAX? There seems to
Possible Duplicate: Can we post image on twitter using twitter API in Android? I
Possible Duplicate: Can you write object oriented code in C? Object oriented programming in
Possible Duplicate: Can you compile C# without using the .Net framework? im sure it's
Possible Duplicate: Can I develop blackberry applications using C#.NET on windows7 can anybody give
Possible Duplicate: Can a single Java variable accept an array of either primitives or

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.