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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:38:50+00:00 2026-06-15T08:38:50+00:00

I have a problem with strict aliasing in C. I’m using GCC 4.7.1. Example

  • 0

I have a problem with strict aliasing in C. I’m using GCC 4.7.1.

Example 1:
When compiling this code with -fstrict-aliasing -Wstrict-aliasing=3 I get “warning: dereferencing type-punned pointer will break strict-aliasing rules”

#include <stdio.h>
#include <stdint.h>

int main(void)
{
    uint8_t a[4] = {0x01, 0x23, 0x45, 0x67};
    uint32_t b;

    b = *(uint32_t *)a;

    printf("%x\n", b);

    return(0);
}

Example 2:
This code gives no warning with -fstrict-aliasing and -Wstrict-aliasing=3 or -Wstrict-aliasing=2 or -Wstrict-aliasing=1

#include <stdio.h>
#include <stdint.h>

int main(void)
{
    uint8_t a[4] = {0x01, 0x23, 0x45, 0x67};
    uint32_t b;
    void *p;

    p = a;
    b = *(uint32_t *)p;

    printf("%x\n", b);

    return(0);
}

Both examples work correctly.

Using union is also undefined behavior and using memcpy() is too slow in my case.
So, is the first example safe (false positive) or second example is also unsafe (false negative) or …?

Thanks.

  • 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-15T08:38:51+00:00Added an answer on June 15, 2026 at 8:38 am

    If you want to manufacture a uint32_t from 4 uint8_t, then do just that: manufacture it. Don’t try to pull one from something that isn’t one by pointer casts. The code you presented will vary in results depending on whether you’re platform is little or big endian, not to mention its just plain wrong.

    They’re both bad. Both samples provided are unsafe no matter what. data alignment requirements are bypassed with such casts. If anything you’re casting ‘to’ requires potentially more restrictive alignment than anything you’re casting ‘from’, you inviting a bus-error. Heed the initial warning. intermediate pointer-to-void simply masks the problem (as it does for most problems).

    You want to know what byte is going “where” when you build that uin32_t.

    uint8_t a[4] = {0x01, 0x23, 0x45, 0x67};
    uint32_t b = ((uint32_t)a[0] << 24) |
                 ((uint32_t)a[1] << 16) |
                 ((uint32_t)a[2] << 8) |
                 (uint32_t)a[3];
    

    This will always put the a[0] byte in the high-byte of the target 32bit unsigned, a[1] in the next byte, etc, regardless of endian’ness. b will always be 0x01234567.

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

Sidebar

Related Questions

I have problem with my query on C, I’m using the oci8 driver. This
I have a problem caused by breaking strict pointer aliasing rule. I have a
Hello i have a problem with this line of code: Last Visted On :
I'm trying to get element by name, and I have a problem This is
i have this problem... i have a my struct: typedef struct Mystruct{ float a;
Having a problem embedding a flash chatroom under a doctype equaling strict using the
I'm having a real strange problem using GCC for ARM with the optimizations turned
I have problem with getting username form windows system. I tried using getlogin function
Sage is supposed to be able to create compiled code using Cython. I have
I have a strange problem in matching a pattern. Consider the Perl code below

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.