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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:38:11+00:00 2026-06-18T14:38:11+00:00

Is the following well-defined: char* charPtr = new char[42]; int* intPtr = (int*)charPtr; charPtr++;

  • 0

Is the following well-defined:

char* charPtr = new char[42];
int* intPtr = (int*)charPtr;

charPtr++;
intPtr = (int*) charPtr;

The intPtr isn’t properly aligned (in at least one of the two cases). Is it illegal just having it there? Is it UB using it at any stage? How can you use it and how can’t you?

  • 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-18T14:38:12+00:00Added an answer on June 18, 2026 at 2:38 pm

    First, of course: the pointer is guaranteed to be aligned in the
    first case (by §5.3.4/10 and §3.7.4.1/2), and may be correctly
    aligned in both cases. (Obviously, if sizeof(int) == 1, but
    even when this is not the case, an implementation doesn’t
    necessarily have alignment requirements.)

    And to make things clear: your casts are all reinterpret_cast.

    Beyond that, this is an interesting question, because as far as
    I can tell, there is no difference in the two casts, as far as
    the standard is concerned. The results of the conversion are
    unspecified (according to §5.2.10/7); you’re not even guaranteed
    that converting it back into a char* will result in the
    original value. (It obviously won’t, for example, on machines
    where int* is smaller than a char*.)

    In practice, of course: the standard requires that the return
    value of new char[N] be sufficiently aligned for any value
    which may fit into it, so you are guaranteed to be able to do:

    intPtr = new (charPtr) int;
    

    Which has exactly the same effect as your cast, given that the
    default constructor for int is a no-op. (And assuming that
    sizeof(int) <= 42.) So it’s hard to imagine an implementation
    in which the first part fails. You should be able to use the
    intPtr just like any other legally obtained intPtr. And the
    idea that converting it back to a char* would somehow result
    in a different value from the original char* seems
    preposterous.

    In the second part, all bets are off: you definitely can’t
    dereference the pointer (unless your implementation guarantees
    otherwise), and it’s also quite possible that converting it back
    to char* results in something different. (Imagine a word
    addressed machine, for example, where converting a char* to an
    int* rounds up. Then converting back would result in
    a char* which was sizeof(int) higher than the original. Or
    where an attempt to convert a misaligned pointer always resulted
    in a null pointer.)

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

Sidebar

Related Questions

Is the following well-defined in C++, or not? I am forced to 'convert' exceptions
According to the C++11 standard, is the following program well-formed and portable C++? int
Is the following C++ code well-formed? void consumer(char const* p) { std::printf("%s", p); }
I want to plot a line from one well-defined point to another and then
Does the following result in well-defined behavior? That is, if you cast a non-vararg
Is the behavior of the following code well defined? int32_t i = -0x80000000; uint32_t
Following A well earned retirement for the SOAP Search API from Google announcing they
The following is a well known implementation of singleton pattern in C++. However, I'm
The following CSS works well under firefox but doesn't work under IE browser, Why?
The following code plays well on the simulator but the audio doesnt play on

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.