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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:06:07+00:00 2026-05-10T16:06:07+00:00

When asking about common undefined behavior in C , people sometimes refer to the

  • 0

When asking about common undefined behavior in C, people sometimes refer to the strict aliasing rule.
What are they talking about?

  • 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. 2026-05-10T16:06:08+00:00Added an answer on May 10, 2026 at 4:06 pm

    A typical situation where you encounter strict aliasing problems is when overlaying a struct (like a device/network msg) onto a buffer of the word size of your system (like a pointer to uint32_ts or uint16_ts). When you overlay a struct onto such a buffer, or a buffer onto such a struct through pointer casting you can easily violate strict aliasing rules.

    So in this kind of setup, if I want to send a message to something I’d have to have two incompatible pointers pointing to the same chunk of memory. I might then naively code something like this:

    typedef struct Msg {     unsigned int a;     unsigned int b; } Msg;  void SendWord(uint32_t);  int main(void) {     // Get a 32-bit buffer from the system     uint32_t* buff = malloc(sizeof(Msg));          // Alias that buffer through message     Msg* msg = (Msg*)(buff);          // Send a bunch of messages         for (int i = 0; i < 10; ++i)     {         msg->a = i;         msg->b = i+1;         SendWord(buff[0]);         SendWord(buff[1]);        } } 

    The strict aliasing rule makes this setup illegal: dereferencing a pointer that aliases an object that is not of a compatible type or one of the other types allowed by C 2011 6.5 paragraph 71 is undefined behavior. Unfortunately, you can still code this way, maybe get some warnings, have it compile fine, only to have weird unexpected behavior when you run the code.

    (GCC appears somewhat inconsistent in its ability to give aliasing warnings, sometimes giving us a friendly warning and sometimes not.)

    To see why this behavior is undefined, we have to think about what the strict aliasing rule buys the compiler. Basically, with this rule, it doesn’t have to think about inserting instructions to refresh the contents of buff every run of the loop. Instead, when optimizing, with some annoyingly unenforced assumptions about aliasing, it can omit those instructions, load buff[0] and buff[1] into CPU registers once before the loop is run, and speed up the body of the loop. Before strict aliasing was introduced, the compiler had to live in a state of paranoia that the contents of buff could change by any preceding memory stores. So to get an extra performance edge, and assuming most people don’t type-pun pointers, the strict aliasing rule was introduced.

    Keep in mind, if you think the example is contrived, this might even happen if you’re passing a buffer to another function doing the sending for you, if instead you have.

    void SendMessage(uint32_t* buff, size_t size32) {     for (int i = 0; i < size32; ++i)      {         SendWord(buff[i]);     } } 

    And rewrote our earlier loop to take advantage of this convenient function

    for (int i = 0; i < 10; ++i) {     msg->a = i;     msg->b = i+1;     SendMessage(buff, 2); } 

    The compiler may or may not be able to or smart enough to try to inline SendMessage and it may or may not decide to load or not load buff again. If SendMessage is part of another API that’s compiled separately, it probably has instructions to load buff’s contents. Then again, maybe you’re in C++ and this is some templated header only implementation that the compiler thinks it can inline. Or maybe it’s just something you wrote in your .c file for your own convenience. Anyway undefined behavior might still ensue. Even when we know some of what’s happening under the hood, it’s still a violation of the rule so no well defined behavior is guaranteed. So just by wrapping in a function that takes our word delimited buffer doesn’t necessarily help.

    So how do I get around this?

    • Use a union. Most compilers support this without complaining about strict aliasing. This is allowed in C99 and explicitly allowed in C11.

        union {       Msg msg;       unsigned int asBuffer[sizeof(Msg)/sizeof(unsigned int)];   }; 
    • You can disable strict aliasing in your compiler (f[no-]strict-aliasing in gcc))

    • You can use char* for aliasing instead of your system’s word. The rules allow an exception for char* (including signed char and unsigned char). It’s always assumed that char* aliases other types. However this won’t work the other way: there’s no assumption that your struct aliases a buffer of chars.

    Beginner beware

    This is only one potential minefield when overlaying two types onto each other. You should also learn about endianness, word alignment, and how to deal with alignment issues through packing structs correctly.

    Footnote

    1 The types that C 2011 6.5 7 allows an lvalue to access are:

    • 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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How about wxPython together with py2exe? There is a nice… May 11, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer I took a close look at the CFML Reference and… May 11, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer Both are really good options. It really depends on your… May 11, 2026 at 6:13 pm

Related Questions

This question is similar to this one , but not a duplicate because I'm
I'm a recent semi-convert to Eclipse after 20 years of using vi and gvim.
Most of my recent programming has been on 32-bit Windows using C/C++/C#/VB6 . Lately,
After asking about what Visual Studio does to register a COM Library , it

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.