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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:40:33+00:00 2026-06-09T02:40:33+00:00

I read this When should I worry about alignment? but I am still do

  • 0

I read this When should I worry about alignment? but I am still do not know if I have to worry about not aligned pointer returned by placement new operator – like in this example:

class A {
public:
   long double a;
   long long b;
   A() : a(1.3), b(1234) {}
};

char buffer[64];

int main() {
   // (buffer + 1) used intentionally to have wrong alignment
   A* a = new (buffer + 1) A(); 
   a->~A();
}

__alignof(A) == 4, (buffer + 1) is not aligned to 4. But everything works fine – full example here: http://ideone.com/jBrk8

If this depends on architecture then I am using: linux/powerpc/g++ 4.x.x.

[UPDATE] Just after posting this question I read this article: http://virtrev.blogspot.de/2010/09/memory-alignment-theory-and-c-examples.html.
Maybe the only drawbacks in my case would be performance penalty, I mean unaligned access cost more than aligned?

  • 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-09T02:40:36+00:00Added an answer on June 9, 2026 at 2:40 am

    When you call placement new on a buffer:

    A *a = new (buf) A;
    

    you are invoking the built-in void* operator new (std::size_t size, void* ptr) noexcept as defined in:

    c++11

    18.6.1.3 Placement forms [new.delete.placement]

    These functions are reserved, a C++ program may not define functions that displace the versions in the
    Standard C++ library (17.6.4). The provisions of (3.7.4) do not apply to these reserved placement forms of
    operator new and operator delete.

    void* operator new(std::size_t size, void* ptr) noexcept;
    Returns: ptr.
    Remarks: Intentionally performs no other action.

    The provisions of (3.7.4) include that the returned pointer should be suitably aligned, so it’s fine for void* operator new (std::size_t size, void* ptr) noexcept to return a nonaligned pointer if one is passed in. This doesn’t let you off the hook, though:

    5.3.4 New [expr.new]

    [14] Note: when the allocation function returns a value other than null, it must be a pointer to a block of storage
    in which space for the object has been reserved. The block of storage is assumed to be appropriately aligned
    and of the requested size.

    So if you pass unaligned storage to a placement-new expression you’re violating the assumption that the storage is aligned, and the result is UB.


    Indeed, in your program above, if you replace long long b with __m128 b (after #include <xmmintrin.h>) then the program will segfault, as expected.

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

Sidebar

Related Questions

Having read around it appears this should be possible. I have the following content
I just learned about ThreadLocal this morning. I read that it should always be
I've read a lot of conflicting information about whether or not URLForUbiquityContainerIdentifier: should be
Recently I read a book( CleanCode ) in this book, FUNCTION SHOULD DO ONE
I've read some posts stating that using this method is not good, shouldn't be
EDIT : To people building tagging systems. Don't read this. It is not what
I have a question about Java style. I've been programming Java for years, but
First of all, I know there is a lot of questions about this topic.
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
I read this post where the author advices to store session files in a

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.