An interesting little thing in Csapp.

Source: Internet
Author: User

The efficiency of home is obviously down, but the third chapter is the rhythm of the end. See the fixed-length array here today and see a fun thing. In the bottom of the computer, all the operations of the array are done using pointers. The array is also a very simple data structure, which is to combine some of the simplest data types on a contiguous area of memory, which is a composite type---array.



When we learn C, we all know that there are two ways to access array elements, one is to use the array name to index the form, and the other is to use the pointer plus the offset. At the bottom, no matter how you access the element, the last thing you do is turn to the pointer.

When writing code, the compiler will optimize the code during compilation.

Today, one of the problems is to follow an example, to a 16*16 two-dimensional array of diagonal elements are set to the Val value of the C language code, let you write a copy of the compiler's optimization rules of the optimized C language code, the specific example or for everyone to paste a bar, or look at the front and back.




The compiler is pretty clever, isn't it? Look at the differences between the two codes to know what the compiler did.

My understanding is that the optimization is still a pointer or offset operation here.


So, according to the above task, I wrote a version as follows.

What I would like to say before pasting the code is that the diagonal elements of each layer are n+1 to the diagonal elements that are adjacent to them, and that N is the number of columns in a two-dimensional array.

int *p = &a[0][0]for (int i = 0; I < n n + 1); i = i + n+1)          P[i] = val

This code is easy to imitate, but after you think about it, it's pretty interesting. The compiler is to manipulate the original two-dimensional array into one-dimensional arrays, because I started by pointing the P pointer to an element in a one-dimensional array. In other words, the entire two-dimensional array is now considered a one-dimensional array, except that this one-dimensional array appears to us to be curved. In memory, each element in a two-dimensional array is an address that is the first address of a one-dimensional array within a two-dimensional array. Everyone is physically adjacent.

So this code is a good explanation. Using pointers and inexpensive operations, all elements in the entire two-dimensional array are considered to be a one-dimensional array, so that as long as you find the law of the increment can be done.

Another thing to say is this loop condition, because I'm looping from 0, so the dimension of the array is n, so it can be up to (n-1) (n+1)

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.