Some C syntax reviews

Source: Internet
Author: User

The loan is cumulative, so it is reprinted.

1. Constant struct, error prompt: incomplete type is not allowed

Q:The declaration in IIC. C is as follows:
Const struct AA
{
Uchar I;
} BB = {3 };

The statement in IIC. H is as follows:
Extern const struct aa bb;

In Main (), the application is as follows:
Uchar cc;

Cc = BB. I;
That is, the error message "incomplete type is not allowed" appears. Why?

A:Struct declaration cannot be extern. If multiple files use the same struct, only one declaration is required.
The struct needs to be defined in the header file, and the initialization can be placed in. C. The details are as follows:

// IIC. h
Typedef const struct
{
Uchar I;
} Type_aa;
Extern type_aa BB;

// IIC. c
Type_aa BB = {3 };

// Main
Uchar cc;

Void main ()
{
Cc = BB. I;
}

2. GCC warnings about strict-aliasing

warning: dereferencing type-punned pointer will break strict-aliasing rules
compile the code without any warning information in GCC 2.x to change to GCC 3.x. A similar warning is displayed after the version. 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), while Source Code contains some violations of 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, like this code:

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

Since the input parameter type of somesetfunc is defined as unsigned long, such a pointer type must be forced cast. However, 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 the performance improvement of executable code that may result from strict-aliasing compilation optimization. You can also use
-Wno-strict-aliasing is used to shield the related warning information. However, no matter how insignificant the 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.

3.


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.