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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:20:49+00:00 2026-06-18T12:20:49+00:00

In the excellent blog post What Every Programmer Should Know About Undefined Behavior ,

  • 0

In the excellent blog post What Every Programmer Should Know About Undefined Behavior, the section “Violating Type Rules” says:

It is undefined behavior to cast an int* to a float* and dereference it (accessing the “int” as if it were a “float”). C requires that these sorts of type conversions happen through memcpy: using pointer casts is not correct and undefined behavior results. The rules for this are quite nuanced and I don’t want to go into the details here (there is an exception for char*, vectors have special properties, unions change things, etc).

I’d like to understand the rules in their full nuancedness. Where are they in the C++11 spec? Or failing that, the C spec (C90, C99, C11)?

In the C++11 spec linked from this Stack Overflow question, N3485, I’m looking in 5.2.10 “Reinterpret cast” but don’t see language for an exception for char* or unions. So that’s probably not the right place. So where is the right place?

  • 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-18T12:20:51+00:00Added an answer on June 18, 2026 at 12:20 pm

    The rule you’re looking for is in §3.10/10 (in C++11):

    If a program attempts to access the stored value of an object
    through a glvalue of other than one of the following types the
    behavior is undefined:
    — the dynamic type of the object,

    — a cv-qualified version of the dynamic type of the object,

    — a type similar (as defined in 4.4) to the dynamic type of the object,

    — a type that is the signed or unsigned type corresponding to the dynamic type of the object,
    — a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type
    of the object,

    — an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic
    data members (including, recursively, an element or non-static data member of a subaggregate
    or contained union),

    — a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,

    — a char or unsignedchar type.

    There are different types (or motivations) for undefined
    behavior.

    In the case of casting an int* to float* and then
    dereferencing it, it is clear that the standard cannot define
    it, since what might happen will depend on the architecture, and
    the value of the int. On the other hand, the quoted paragraph
    is completely wrong—using memcpy to do the conversion is
    also undefined behavior, for largely the same reasons.

    One of the motivations for undefined behavior is to
    allow implementations to define it, in a manner that makes sense
    for the target architecture, if such exists. This is such
    a case. A compiler which intentionally causes it to fail is
    defective. Of course, if we suppose 32 bit 2’s complement
    int, and 32 bit IEEE float, we may expect certain values of
    the int to correspond to trapping NaN, which will cause the program
    to fail. This is part of the reason the behavior is
    undefined; to allow such things to happen. But if we are
    familiar with the low level details of the hardware,
    it should work as expected, provided the compiler can see
    the cast.
    If it doesn’t, this is a QoI problem with the compiler, and such
    a compiler should be avoided for such types of work.

    As hinted at above, this particular case, and in fact, in all
    cases which involve type punning (writing to one member of
    a union, and reading from another, for example), do pose
    a problem, to which the standard has yet to find adequate
    wording. The problem occurs because normally, the compiler is
    allowed to assume that pointers to different types (except
    character types) do not alias; that an int* can never point to
    the same object as a float*. And proving that two pointers
    cannot alias is important for optimization. A compiler that
    breaks code where the pointer cast or the union is clearly visible is
    just broken, even if the standard says it is undefined behavior.
    A compiler that breaks code where all it sees are two pointers
    to unrelated types is understandable, even in cases where the
    standard says the behavior is well defined.

    Using memcpy avoids this problem by using two different
    objects, which don’t alias. It still encounters undefined
    behavior because putting the bit pattern of an int into
    a float, then accessing the float, doesn’t have any defined
    behavior. (Or vice-versa; I know of at least one machine where
    copying the bits of a float into an int may result in an
    illegal int value.)

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

Sidebar

Related Questions

After going this excellent blog post by Simon I came to know that model
I'm reading through Rob Ashton's excellent blog post on RavenDB: http://codeofrob.com/archive/2010/05/09/ravendb-an-introduction.aspx and I'm working
In Bryan Helmkamp's excellent blog post called 7 Patterns to Refactor Fat ActiveRecord Models
Using Marius Gedminas's excellent blog post , I have created a custom traverser for
Phil Haack has an excellent blog post on how to use JSON, data binding,
I was about to deploy WordPress on Microsoft Azure Cloud following this excellent blog:
Consider the following abbreviated code from this excellent blog post : import System.Random (Random,
I have used the excellent post How to create trapezoid tabs in WPF tab
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
I followed an excellent article by Rudy Velthuis about using C++ classes in DLL's.

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.