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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:00:27+00:00 2026-05-17T02:00:27+00:00

I have the following code: struct A { short b; }; struct B {

  • 0

I have the following code:

struct A
{
    short b;
};

struct B
{
    double a;
};


void foo (struct B* src)
{
    struct B* b = src;
    struct A* a = (struct A*)src;

    b->a = sin(rand());

    if(a->b == rand())
    {
        printf("Where are you strict aliasing warnings?\n");
    }
}

I’m compiling the code with the following command line:

gcc -c -std=c99 -Wstrict-aliasing=2 -Wall -fstrict-aliasing -O3 foo.c

I’m using GCC 4.5.0. I expected the compiler to print out the warning:

 warning: dereferencing type-punned pointer will break strict-aliasing rules

But it never is. I can get the warning to be printed out for other cases, but I’m wondering why, in this case, it isn’t. Is this not an obvious example of breaking the strict aliasing rules?

  • 1 1 Answer
  • 1 View
  • 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-17T02:00:27+00:00Added an answer on May 17, 2026 at 2:00 am

    GCC’s docs for -Wstrict-aliasing=2 says (emphasis mine):

    Level 2: Aggressive, quick, not too
    precise. May still have many false
    positives (not as many as level 1
    though), and few false negatives (but
    possibly more than level 1)
    . Unlike
    level 1, it only warns when an address
    is taken. Warns about incomplete
    types. Runs in the frontend only.

    It seems like your code isn’t too tricky, so I’m not sure why there’d be a false negative, but maybe it’s because you don’t use the & address-of operator to perform the aliasing (that might be what’s meant by “only warns when an address is taken”)


    Update:

    It is from not using the address-of operator. If I add the following code to the foo.c file:

    int usefoo(void)
    {
        struct B myB = {0};
    
        foo( &myB);
    
        return 0;
    }
    

    The warning is issued.

    If usefoo() is in a separate compilation unit, no warning is issued.

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

Sidebar

Related Questions

Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
I have the following code: class Foo<T> where T : struct { private T
I have the following code: #include <boost/shared_ptr.hpp> struct Foo { int a; }; static
I have code that boils down to the following: template <typename T> struct Foo
I have a question with the following Code: #!/usr/bin/perl use strict; use warnings; my
Consider the following C code: struct Foo { short a; long b; char c;
Say I have the following code: struct date { int day; int month; int
Let's say I have the following code: struct mytype { ~mytype() { /* do
I have the following code: class outer { struct inner { int var1; int
I have a following code in a most inner loop of my program struct

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.