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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:20:35+00:00 2026-05-31T12:20:35+00:00

Does this code violate strict aliasing? struct {int x;} a; *(int*)&a = 3 More

  • 0

Does this code violate strict aliasing?

struct {int x;} a;
*(int*)&a = 3

More abstractly, is it legal to cast between different types as long as the primitive read/write operations are type correct?

  • 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-05-31T12:20:36+00:00Added an answer on May 31, 2026 at 12:20 pm

    First, it is legal to cast in C. §6.7.2.1/13:

    Within a structure object, the non-bit-field members and the units in
    which bit-fields reside have addresses that increase in the order in
    which they are declared. A pointer to a structure object, suitably
    converted, points to its initial member (or if that member is a
    bit-field, then to the unit in which it resides), and vice versa.
    There may be unnamed padding within a structure object, but not at its
    beginning.

    The aliasing rule reads as follows (§6.5/7):

    An object shall have its stored value accessed only by an lvalue expression that has one of
    the following types:

    • a type compatible with the effective type of the object,
    • a qualified version of a type compatible with the effective type of the object,
    • a type that is the signed or unsigned type corresponding to the effective type of the
      object,
    • a type that is the signed or unsigned type corresponding to a qualified version of the
      effective type of the object,
    • an aggregate or union type that includes one of the aforementioned types among its
      members (including, recursively, a member of a subaggregate or contained union), or
    • a character type.

    Here you would be accessing it via pointers of a “type compatible with the effective type of the object” and “an aggregate or union type that includes one of the aforementioned types among its members”, so no problem with aliasing either. So in C, it is indeed perfectly legal to access the first member of a structure by casting the pointer to the structure to the type of the member in question.

    In C++, however, you’ll often find vtables and other things at the start of a C++ object. In your specific case, however, your structure is of standard layout, and so this is explicitly allowed (§9.2/20 in n3290, thanks Luc Danton! – C++03 apparently has a similar rule, expressed in terms of POD objects).

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

Sidebar

Related Questions

Does this code cause a memory leak: int main(){ int * a = new
Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
Does code like this (potentially) violate the iPhone Developer License Agreement? Class clazz =
Does it violate strict aliasing rules to move items of any type around using
Why does this code: class A { public: explicit A(int x) {} }; class
Does this code result in defined behavior? class A { int x; }; class
Why does this code work in C but not in C++? int i =
Does this code work across all standard compliant C++ compilers (it works with g++)?
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
why does this code crash? is using strcat illegal on character pointers? #include <stdio.h>

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.