How do you play C !, Playing C Language

Source: Internet
Author: User

How do you play C !, Playing C Language

I saw an article on cool shell. The member arrays and pointers in the C-language structure make me really admire each other. Although Mr. Hao said that he is not a master, he wrote such an article, which makes me feel that my level is actually scum! I feel a little excited after reading it. I also want to talk about it myself and try to see if I can clearly understand the problems described in the microblog. There is absolutely no meaning of plagiarism. Due to my limited level, writing may be terrible. Please forgive me!

Okay. Let's talk about the problem first. There is such a piece of code,

 1 #include<stdio.h> 2 struct str{ 3     int len; 4     char s[0]; 5 }; 6  7 struct foo { 8     struct str *a; 9 };10 11 int main(int argc, char** argv) {12     struct foo f={0};13     if (f.a->s) {14         printf( f.a->s);15     }16     return 0;17 }

Which line of the program will be suspended? I am using gcc, And I am suspended in line 14th. Why? Why is the if statement not suspended, but in the printf statement?

What are the first variables to analyze? What is a variable? It is actually the alias of the memory region! The variable of the struct type is actually a large memory area, and the struct variable name points to the starting address of the memory area, and other variables are pushed back Based on the variable type. For example, the str struct above. In fact, a 4-byte memory is used to store the int variable len, and the subsequent bytes are used to store the character array s. We can print out their addresses through gdb, as shown in:

  

Here, the struct variable t and the address of its first member len are both 0xbffff184, and the address of its s member is the address of t + 4, that is, 0xbffff188. OK. I don't know if you can understand this. In fact, the address of the variable of each member in the struct is actually the address of the struct variable plus the relative offset. For example, in the above example, the s address is the t address plus four bytes (representing the int variable ).

Now that we know this concept, let's analyze this program.

  

First, an f variable is created from the main function. a member is pointer a, and the address stored in pointer a is initialized to 0. F. a-> s, it is to find an address, that is, the address of the s variable. The search method is to first find the address stored in Structure Variable pointer a, and then add 4 to this address, after finding this address, I did not access the memory space represented by this address, so the program did not report an error. In the print statement, find the address first, then access the memory space represented by the address, and try to find a string in the space and output it. As we have initialized 0 IN a, the accessed memory space is 0x4, so the program will be suspended! ^ O ^

  

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.