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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:53:42+00:00 2026-05-13T17:53:42+00:00

I am writing a memory-managing template class in which I want to create a

  • 0

I am writing a memory-managing template class in which I want to create a C-style array of fixed size, to serve as a heap. I keep the objects stored in an array like this:

T v[SIZE];

As this only serves the role as a heap that can hold T objects, I don’t want the T default constructor to get automatically called for every object in the array.

I thought about the solution to define the heap like this:

char v[SIZE * sizeof(T)];

…but this will give me alignment problems.

Is there any better way to achieve this?

ADD: As I have special run time requirements, it is essential that this class doesn’t do any allocations on the global heap.

ADD 2: SIZE is a template argument and known at compile-time.

  • 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-13T17:53:43+00:00Added an answer on May 13, 2026 at 5:53 pm

    The standard containers use allocators to seperate allocation/deallocation from construction/destruction. The standard library supplies a single allocator which allocates on the heap.

    This code declares an array big enough to hold SIZE elements of type T with the correct allignment:

    typedef typename std::tr1::aligned_storage<sizeof(T),std::tr1::alignment_of<T>::value>::type aligned_storage;
    aligned_storage array[SIZE];
    

    The solution using std::allocator can’t be used to declare an array on the stack, and as the standard containers require that custom allocators hold no state, a custom allocator can’t be portably used to allocate on the stack either.

    If your compiler doesn’t support std::tr1::alignment_of you can use boost::alignment_of instead.

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

Sidebar

Related Questions

I'm writing the memory manager for an application, as part of a team of
I have just been getting into low level programming (reading/writing to memory that sort
I am writing a C library that reads a file into memory. It skips
Writing some test scripts in IronPython, I want to verify whether a window is
I am writing a program to leak memory( main memory ) to test how
I'm writing an caching app that consumes large amounts of memory. Hopefully, I'll manage
The app I am writing is suffering quite dramatically from a memory leak. Pretty
I'm writing a program (a theorem prover as it happens) whose memory requirement is
I've been trying to diagnose a memory leak in an Android application I'm writing.
Writing the code for the user authentication portion of a web site (including account

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.