How to make vim a semi-automated IDE for C + +

Source: Internet
Author: User
Tags manual int size linux

C language in the Linux system is the importance of nature is unmatched, irreplaceable, so I write Linux Lake series can not mention C language. C language is my enlightenment language, thanks to the C language led me into the program world. Although it does not depend on it now, it is still unavoidable to deal with it often, especially under the Linux system.

Gnu-c is commonly used in Linux systems and there is a Gnu-c language manual. pdf. The GNU C Reference Manual's homepage is here: http://www.gnu.org/software/gnu-c-manual/. The C-language kernel is extremely compact, with a total of only 91 pages, and only 70 pages after removing catalogs, appendices, and indexes. I can usually go over it in one hours. I had the idea of translating it into Chinese, and then I gave it up. This kind of carefully worded thing is to let others do it. I only write about my own sentiment.

Comprehend one: C language standard but GNU extension

Recently, in order to study the underlying protocol of X window, I began to attempt to use XCB programming. When I opened the XCB header file, I was stunned by the large number of __restrict__ keywords, but the GNU C language Manual was a good answer for me. __restrict__ is also a GNU extension keyword, and I'll explain the use of that keyword in detail later. In fact, the C language C99 standard has introduced the Restrict keyword, no before and after the underscore, but in a large number of open source code, the most common use is the GNU extension, rather than the C language standard.

and restrict keyword has the same fate and inline, _complex, and so on, they are introduced in the C99 standard keyword, but in fact, before the C99 standard came out, GNU C already has __inline__, __complex__ and other extension keywords. I remember years ago when I was learning Linux version 0.11 source code, see a lot of __inline__ once puzzled, do not know why Linus in 91 can use so advanced language features, later know, this is the GNU extension keyword.

C language Standards have C89 and C99, use GCC when even to display the specified-STD=C99 to fully support the C99 standards, so in the open source community, we still like the preferred GNU extension keyword. Like __inline__, __complex__ and __restrict__. In short, the C language standard but GNU extension.

Let's look at the real meaning of __restrict__. Remember CSDN once contained an article "why some languages will be faster than others", which mentioned "for a long time, the same two programs in Fortran and C (or C + +) running, Fortran will be faster, because the Fortran optimization to do better." This is true, even if the C language and Fortran compilers are the same as the same code generator. This difference is not because of some of the characteristics of Fortran, in fact, is the opposite, because Fortran does not have the characteristics. "This is because the pointers in C language have made it difficult to optimize the compiler," the article continued: "The problem comes." These pointers can be substituted for any memory address. More importantly, they can overlap. The memory address of the output array can also be an input array. You can even partially overlap, and the output array can overwrite half of an input array. This is a big problem for compiler optimizations because the array-based optimizations are no longer applicable. In particular, the order in which elements are added is problematic, and if the output overlaps an array, the result of the calculation becomes indeterminate, depending on whether the action of the output element occurs before or after the element is overwritten. ”

The problem with the __restrict__,c language will no longer exist. After a pointer is decorated with __restrict__, ① the pointer can only be initialized when it is defined, and ② no more pointers to the memory pointed to by the pointer, so the compiler can optimize the algorithm. The following code:

int * __restrict__ p = (int*) malloc (100*sizeof (int));

The pointer p has the __RESTRICT__ keyword modification, so it can only be initialized at the time of the definition, cannot be assigned later, and no __restrict__-modified pointer can be assigned at any time, as follows:

int arr[100];
int* PARR;
PARR = arr;

The pointer Parr is not decorated with the __RESTRICT__ keyword, so you can assign the first address of the array to it.

For example, we define a function to operate on two pieces of data, the result is placed in the 3rd block of memory, as follows:

void Func1 (void* p1, void* p2, void* p3, int size) {for
    (int i=0; i<size; i++) {
        p3[i] = P1[i] + p2[i];
    }
}

Obviously, because the compiler has no way to determine whether the pointer P1, p2, P3 point to the memory overlap, so can not be optimized, plus the __RESTRICT__ keyword after the following:

void Func1 (void* __restrict__ p1, void* __restrict__ p2, void* __restrict__, int size) {for
    (int p3; i=0 i<size; {
        P3[i] = P1[i] + p2[i];
    }
Related Article

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.