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

Source: Internet
Author: User

[Reprinted from] http://www.zeali.net/entry/454

[Original]

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

After the code without any warning information is compiled under GCC 2.x is changed to GCC 3.x, a similar warning occurs. The reason is that GCC 3 introduces the strict aliasing architecture and uses the-fstrict-aliasing parameter during compilation (this parameter uses-O2
,-O3,-OS optimization parameters take effect by default), and if the source code violates the strict-aliasing rules, the compiler will raise the warning for this part of the code.

GCC 3 manual describes the-fstrict-aliasing parameter: allows the compiler to assume the strictest aliasing rules applicable to the language being
Compiled. for C (and C ++), this activates optimizations based on the type of expressions. in particle, an object of one type is assumed never to reside at the same address as an object of a different type, unless the types are almost the same. for example,
An "unsigned int" Can alias an "int", but not a "Void *" or a "double". A character type may alias any other type.

In short, when this parameter is activated, the compiler expects that different types of objects do not point to the same address. For example:

Int retlen;
Somesetfunc (unsigned long *) & retlen );
Printf ("RET Len = % d \ n", retlen );

The input parameter type of somesetfunc is defined
Unsigned long, so this type of pointer needs to be forced cast. But
-For fstrict-aliasing optimization parameters, such conversions have potential problems (but they may not actually cause any problems ). So if there are too many such type forced conversions in the existing source code, it may be a nightmare to modify the code. The simplest method is to use the-fno-strict-aliasing parameter to disable the GCC optimization option, at the cost of giving up strict-aliasing.
Compilation optimization may improve the performance of executable code. Of course, you can also use-wno-strict-aliasing to shield related warning information. However, no matter how insignificant these warning information is, it is always "suspected to be dangerous ", if possible, we 'd better eliminate all the warnings.

The elimination method is not complex. As shown in GCC manual, different member variables of union can be used to complete type conversion. The above code can be changed:

Union u_retlen
{
Int retlen;
Unsigned long PTR;
};
Somesetfunc (& u_retlen.ptr );
Printf ("RET Len = % d \ n", u_retlen.retlen );

Although it will make the source code ugly, for most of the existing source code, this may be the solution with the smallest change. For newly written code, how to better design function entry parameters (such as using void *) may be a problem that needs to be considered.

[The above is the original article]

 

First, forcibly convert an address to the void * type, and then to another type to eliminate this warning. It may be because the compiler does not have any Optimization for strong conversion of void.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.