C language: memory address Analysis & amp; sizeof and strlen usage Summary

Source: Internet
Author: User

C language: memory address Analysis & sizeof and strlen usage Summary

It was also the C language that I came into contact with in the university era. When I learned arrays, pointers, and other concepts, I got the word "Dizzy. In addition to my recent study, I am crazy about supplementing the relevant knowledge. In conclusion, if you have any mistakes, please give me more advice.

I. Memory Address Analysis

1) Let's look at a basic example:

 

int a[4];
Q: What do & a [0], a, & a, a + 1, & (a + 1), and & a + 1 represent?

 

At first glance, we are really at a loss; we can analyze it in Diagrams (assuming that the following operations are on the 32 system ).

 

Let's briefly explain the work:

1. the purple area is the specific value stored by array a in the memory. Because it is defined as a [4], there are four "specific values" in the memory storage area.

2. Assume that the address of the first element in the array in the memory is 0x8000, so the addresses of each element are marked as, that is, they represent the memory address of each element in array.

3. & a points to the address of the entire array in the memory.

Next, let's answer the questions we just raised one by one.

1. & a [0]: corresponding, its value is 0x8000, indicating the address of the first element of array.

2. a: It is equivalent to & a [0]. Note that a is a pointer constant, that is, its value cannot be changed. Therefore, the array name can be understood as a pointer constant, and its value is the address of the first element in the array.

3. & a: It indicates the address of the entire array. Although its value is the same as that of a and & a [0], its meaning is different;

4. a + 1: because a represents the address of the first element in the array, a + 1 represents the address of the second element, that is, 0x8004.

5. & (a + 1): this expression is incorrect. Only the address of the entire array can be written as & a, and a + 1 indicates the address of the second element. In this case, obtaining the address (&) is incorrect.

6. & a + 1: & a indicates the address of the entire array, and 1 indicates the address with the length of the entire array, so its value is 0x8010 marked in.

 

2) Let's look at an address distribution instance:

 

Int a = 3; int B; int main (int argc, const char * argv []) {// insert code here... int c = 4; int d; char * str = hello world; char str2 [] = good idea; printf (% p, & a); printf (% p, & B); printf (% p, & c); printf (% p, & d); printf (% p, str); printf (% p, & str ); printf (% p, * (& str); // you can see that the content in the & str address is the str address printf (% p, str2 ); printf (% p, & str2); return 0 ;}

The printed result on my computer is:

 

 

Zero x 100001028

0x0000102c

0x7fff5fbff744

0x7fff5fbff740

0x100000f78

0x7fff5fbff738

0x100000f78

0x7fff5fbff75e

0x7fff5fbff75e


Looking at these results, there is no rule for Buddha. However, you can refer to the section "C language: link property and storage type" for analysis. In this section, I finally provide a memory distribution chart, as shown below:

 

 

1. a is an initialized global variable, so its address is small.

2. B is an uninitialized global variable, so it allocates a larger memory address than a. The output results are 4 bytes different, so the memory allocation is continuous.

3. c and d are all allocated in the stack storage zone, so their memory address looks much larger than that of a and B.

4. str stores a String constant in the read-only data zone. Therefore, the print value is smaller than that of.

5. & str is a pointer to a String constant, which is allocated on the stack, so its value is close to that of c and d.

6. * (& str) the output is consistent with that of str. That is, the content in the & str address is the str address.

7. str2 and & str2 have been analyzed in the first example, and we will not repeat it here.

 

Ii. sizeof and strlen

Sizeof is widely used in C language.

1. Pass the array name directly with real parameters

 

int a[10];printf(%ld,sizeof(a));
Sizeof calculates the size of array bytes and the output result is 40.

 

2. The address is passed.

void foo(int a[]) {    printf(%ld,sizeof(a));}

Then, when calling:

 

 

foo(a);
Note: The a passed here is a pointer, that is, the address of array. Only when defining the function foo, deliberately write the form of the form parameter into the int a [] array. In this way, the Buddha is operating the array. But in fact this is a misunderstanding, and it actually transmits pointers. Therefore, the foo function can be defined in this form.

 

 

void foo(int *a) {    printf(%ld,sizeof(a));}
Therefore, the above two definitions of the foo function are equivalent. Now we can analyze the sizeof (a) printed in function foo. Because a is an address, it occupies 4 bytes in the 32-bit system, so the print result is 4.

 

3. Use sizeof to analyze character Arrays

 

char str[5] = hello;printf(%s,str);
Analysis print result?

 

 

There are three possible print results: 1. hello; 2. garbled characters are added to hello; 3. The program crashes directly.

Analysis: Because the defined str array has a length of 5, the array does not end.

1. If the byte after the str array in memory is exactly 00, it indicates that the str array ends and the output is hello.

2. If the byte behind the str array is not 00, it will continue to search for the back of the memory. After a lot of bytes are passed, it will finally find 00, so it will output hello with some garbled characters.

3. If the byte behind the str array is not 00, it will continue to search for the back of the memory and find the memory area that the system prohibits access to, then the program will crash directly.

 

Conclusion:

If the array is defined as: char str [5] = hello, the memory is allocated without '', so sizeof (str) is 5.

If the array is defined as char str2 [] = hello, ''is added to the memory allocation, so sizeof (str) is 6.

If the array is defined as char str3 [7] = hello, ''is added to the memory allocation, so sizeof (str) is 7.

The preceding str, str2, and str3 results after strlen calculation are 5. The value of sizeof remains unchanged.

It indicates that strlen is the actual length of the calculated string, and sizeof is the length of the calculated array during initialization.


 

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.