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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:28:41+00:00 2026-06-01T08:28:41+00:00

I have a structure which aims at storing user defined data (i.e. from a

  • 0

I have a structure which aims at storing user defined data (i.e. from a plugin). It has a such a char[] with a given maximum size to store that data.

struct A
{
    // other members omitted
    // data meant to be type punned, only contains PODs
    char data[256];
};

Then there’s a sample user structure that has a static function to cast itself from A.

struct B
{
    int i;
    double d;

    static B& FromA_ref(A& a)
    {
        // static_assert that sizeof(B) < sizeof(A::data)
        return * reinterpret_cast<B*>(a.data);
    }
};

I compile with g++ -O3 -std=c++0x -Wall -o test test.cpp (GCC 4.6.1).

This triggers a dereferencing type-punned pointer will break strict-aliasing rules warning. I thought that would be ok since I have used a char[] as storage which I thought would follow the same rules as char*. I find it strange that it doesn’t. Don’t you ? Well, … I can’t change it right now, so let’s move on.

Now let’s consider the following method:

struct B
{
    ....
    static B* FromA_ptr(A& a)
    {
        // static_assert that sizeof(B) < sizeof(A::data)
        return reinterpret_cast<B*>(a.data);
    }
}

Since I am not dereferencing anything here GCC doesn’t output any warning. Neither does it when I use my pointer to B later on.

A a;
auto b = B::FromA_ptr(a);
b->i = 2; // no warnings.

But is it safe to do so ? I feel like I have been working my way around the problem rather than solving it. For me -> is still dereferencing the variable somehow.

Alternatively, are there any better way to achieve the effect ? I.E. get a modifiable reference (or pointer) casted from a storage inside another struct ? (Union won’t work since the set of types stored are not known when A is defined and some may be added via plug-ins, memcpy will force me to copy the data back and forth though it seems to be the only safe way so far)

  • 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-01T08:28:42+00:00Added an answer on June 1, 2026 at 8:28 am

    The answer is no, it is not safe (see this SO question)

    GCC will assume that the pointers cannot alias. For instance, if you assign through one then read from the other, GCC may, as an optimisation, reorder the read and write – I have seen this happen in production code, it is not pleasant to debug.

    attribute((may_alias)) on the types used is probably the closest you can get to disabling the assumption for a particular section of code.

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

Sidebar

Related Questions

I have a data structure which is as given below: class File { public
Guys, I have a data structure which has 25 distinct keys (integer) and a
I have a data structure object which is loaded from an XML file inside
I have a data structure which has, <Book title>, <Author>, and <rate> Since Book
I have a Java data structure which results from deserialising this JSON: { 'level1
I have a structure which has 3 identifier fields and one value field. I
I have a data structure which uses composite ids (Which I dont wish to
I have a C code in which I have a structure declaration which has
We have redesigned the structure to a website which has several business units. Now
I have a data structure which consists of pairs of values, the first of

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.