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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:29:31+00:00 2026-06-02T10:29:31+00:00

Is it legal to cast this way? void probability(void **value) { double v =

  • 0

Is it legal to cast this way?

void probability(void **value)
{
    double v = 0.1234;
    *value = (void *) *(uint64_t *) &v;
}

I know, this is a bad thing, but I’m 100% sure that sizeof(double) = sizeof(void *) = sizeof(uint64_t) on target machine.

  • 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-02T10:29:34+00:00Added an answer on June 2, 2026 at 10:29 am

    It’s undefined behavior, because your code violates the strict aliasing rules.

    The compiler is permitted to assume that pointers to most unrelated types don’t point to the same memory. You create a uint64_t * (the result of the cast) that points to memory which is “actually” a double, and you expect reading from that pointer to give you a value that has something to do with the double.

    The purpose of the strict aliasing rules is to allow the compiler to make various optimizations that will break this code – the most likely is that the compiler can “deduce” that v is unused and never initialize it, since it’s never accessed via any valid name or pointer, only invalid aliases.

    I haven’t checked with this specific code, but GCC does in fact rely on strict aliasing at high optimization, and will break this sort of code.

    The way to fix strict aliasing problems is with memcpy:

    assert(sizeof(*value) == sizeof(v));
    memcpy(value, &v, sizeof(*value));
    

    Once this is done, or if you’re using a compiler that doesn’t rely on strict aliasing, or for which that reliance can be avoided (--no-strict-aliasing), then you still have a problem. The standard doesn’t actually guarantee that every numeric value of the same size as an address, can actually be represented in void*. For example, it’s legal for the implementation to (in effect) have padding bits in pointers, and to crash if you try to create a pointer value with the wrong values in the padding bits. In practice that isn’t going to happen on any hardware that you’d call “normal”, but nevertheless the standard doesn’t permit your code.

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

Sidebar

Related Questions

Is it legal to define a default value for a parameter the way I
I know that it's legal to use dynamic_cast to do a cross-cast across a
Is it legal? class SomeClass { public: static void f(); }; using SomeClass::f; Edit:
Is this legal and/or good practice? #define SOFTWARE_VERSION_NUMBER 7.0v1.1 Want struct to always contain
You may argue that this question has a legal flavor to it, and that
Sorry for the non-descript title, but I'm having a hard way with words right
It's legal to nest SVG documents inside XHTML documents; but is it legal to
Is this legal? If not, will the following code allow this? class Foo {
Does this code violate strict aliasing? struct {int x;} a; *(int*)&a = 3 More
Is this legal in c++11: std::unordered_map<X, Y> xy_map; X my_x; Y my_y; xy_map.insert(decltype(xy_map)::value_type(my_x, my_y));

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.