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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:55:45+00:00 2026-05-17T06:55:45+00:00

In this I need C++ array class template, which is fixed-size, stack-based and doesn't

  • 0

In this I need C++ array class template, which is fixed-size, stack-based and doesn't require default constructor answer I posted a piece of code, that is using placement new with char array. For me, this is something absolutely normal. But according to comments this code is wrong.

Can anyone explain in more detail?

Specifically what can go wrong with the array. What I understand from the comments is that T x[size]; might not fit into char x[size*sizeof(T)];. I don’t believe this is true.

EDIT:

I’m just more and more confused. I know what alignment is in case of structures. Yes, when you have a structure the attributes start on different offsets then you might think.

OK, now we are back to arrays. You are telling me that T x[size]; is the same size as char x[size*sizeof(T)];, yet I cannot access the char array as T array because there might be some alignment. How can there be alignment when the arrays have the same size?

EDIT 2:

OK I finally get it, it may start on a wrong address.

EDIT 3:

Thx everyone, you can stop posting 🙂 Phew, this total blew my mind. I just never realized this was possible.

  • 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-17T06:55:45+00:00Added an answer on May 17, 2026 at 6:55 am

    A T x[size] array will always fit exactly into size * sizeof(T) bytes, meaning that char buffer[size*sizeof(T)] is always precisely enough to store such an array.

    The problem in that answer, as I understood it, was that your char array is not guaranteed to be properly aligned for storing the object of type T. Only malloc-ed/new-ed buffers are guaranteed to be aligned properly to store any standard data type of smaller or equal size (or data type composed of standard data types), but if you just explicitly declare a char array (as a local object or member subobject), there’s no such guarantee.

    Alignment means that on some platform it might be strictly (or not so strictly) required to allocate, say, all int objects on, say, a 4-byte boundary. E.g. you can place an int object at the address 0x1000 or 0x1004, but you cannot place an int object at the address0x1001. Or, more precisely, you can, but any attempts to access this memory location as an object of type int will result in a crash.

    When you create an arbitrary char array, the compiler does not know what you are planning to use it for. It can decide to place that array at the address 0x1001. For the above reason, a naive attempt to create an int array in such an unaligned buffer will fail.

    The alignment requirements on some platform are strict, meaning that any attempts to work with misaligned data will result in run-time failure. On some other platforms they are less strict: the code will work, but the performance will suffer.

    The need for the proper alignment sometimes means that when you want to create an int array in an arbitrary char array, you might have to shift the beginning of an int array forward from the beginning of the char array. For example, if the char array resides at 0x1001, you have no choice but to start your constructed-in-place int array from the address 0x1004 (which is the char element with the index 3). In order to accommodate the tail portion of the shifted int array, the char array would have to be 3 bytes larger than what the size * sizeof(T) evaluates to. This is why the original size might not be enough.

    Generally, if your char array is not aligned in any way, you will really need an array of size * sizeof(T) + A - 1 bytes to accommodate an aligned (i.e. possibly shifted) array of objects of type T that must be aligned at A-byte boundary.

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

Sidebar

Related Questions

I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
Consider following class of custom array template <typename element, unsigned int size> class array
I currently have a class that uses template arguments. I need an array of
Basically I have this string $str=word1 word2 word3; I need array( 'word1', 'word2', 'word3'
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I need to change this array's keys to 0-5, why isn't this working? $arr
i need a structure like this array(){ [0] => array(){ [0] => array(){ //
I need to sort an array that can look like this: $array[4][0] = array('id'
I need to extract Asia from this array. How can i do that. Here
I have this array and I need to sort it, first by key_1 and

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.