C Language: Memory address analysis & sizeof and Strlen usage Summary

Source: Internet
Author: User

Or in the university era of contact with the C language, when learning arrays, pointers and other concepts, how a "halo" word. Recently in the study, crazy to fill the relevant knowledge, so summed up, if there are errors, please give us a lot of advice.

First, memory address analysis

1) First look at one of the most basic examples:

int a[4];
question: &a[0], A, &a, A+1, & (a+1), what does &a+1 say?

At first glance, I'm really overwhelmed; we can analyze it graphically (assuming that the following operations are on the system above 32).



Let's start with a simple description:

1. The purple area is the specific value that array a stores in memory because it is defined as a[4], so the memory storage area has four "concrete value" content .

2. Assume that the first element in the array has an address of 0x8000 in memory, so the addresses of each element are labeled, that is, they represent the memory address of each element in array a.

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

Next, we will answer the questions we have just raised.

1. &a[0]: Corresponding, its value is 0x8000, which represents the address of the first element of array A.

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

3. &a: It represents the address of the entire array, although its values are consistent with a and &a[0], but the meaning of the expression is inconsistent;

4. A+1: Because a represents the address of the first element in the array, a+1 represents the address of the second element, which is 0x8004.

5. & (a+1): This expression is wrong. Only the address of the entire array can be written as &a, and a+1 represents the address of the second element, at which point the address (&) is incorrect.

6. &a+1: &a represents the entire array address, plus 1 for the entire array byte length address, so its value is the 0x8010 in the callout.


2) Let's look at one more address distribution example:

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\n", &a);    printf ("%p\n", &b);    printf ("%p\n", &c);    printf ("%p\n", &d);    printf ("%p\n", str);    printf ("%p\n", &str);    printf ("%p\n", * (&STR)); It can be seen that the contents of the &STR address is the address of Str    ("%p\n", str2);    printf ("%p\n", &str2);        return 0;}

The results printed on my computer are:

0x100001028

0x10000102c

0x7fff5fbff744

0x7fff5fbff740

0x100000f78

0x7fff5fbff738

0x100000f78

0x7fff5fbff75e

0x7fff5fbff75e


looking directly at these results, there are no rules for the Buddha to be released. However, you can refer to the "C language: Link attributes and Storage type " section of the content of the analysis. In this section, I end up with a memory allocation diagram, as follows:



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

2. B is an uninitialized global variable, so it allocates memory addresses larger than a, prints out the results, they differ by 4 bytes, so the memory allocation is continuous.

3. C,d are allocated in the stack store, so their memory address looks a lot larger than A/b.

4. Str holds a string constant, which is stored in the read-only data area. So it has a smaller print value than a.

5. &str is a pointer to a string constant that is allocated on top of the stack, so its value is closer to the c,d.

6. * (&STR) The printed result is consistent with Str. That is, the contents of the &STR address are the address of Str.

7. STR2 and &STR2 have been analysed in the first example and are not repeated here.


Ii. sizeof and Strlen

sizeof is widely used in C language.

1. Direct argument passing array name

int a[10];p rintf ("%ld\n", sizeof (a));
sizeof calculates the byte size of the array and outputs the result as 40.

2. The address is passed

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

then call the time:

Foo (a);
Note: The A passed here is a pointer to the address where the array A is located. It is only when the function foo is defined that the formal parameter is deliberately written as an array of int a[]. This way, when used, the Buddha is manipulating an array. But actually this is a misunderstanding, it actually passes the pointer. So the Foo function can be completely defined in this form

void foo (int *a) {    printf ("%ld\n", sizeof (a));}
so the above two definitions of the Foo function are equivalent. Now it's much easier to parse the sizeof (a) printed in the function foo. Because A is an address, it accounts for 4 bytes in a 32-bit system, so the print result is 4.

3. Use sizeof to parse the character array

Char str[5] = "Hello";p rintf ("%s\n", str);
Analyze print results?

There are three possible printing results: 1. Hello 2. Hello plus some garbled; 3. The program rushes directly to collapse

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

1. If the byte after the str array in memory is exactly 00, the end of the STR array and the output hello.

2. If the byte after the STR array is not 00, it will continue to look behind the memory, after a lot of bytes, finally found 00, so will output hello plus some garbled.

3. If the byte behind the str array is not 00, it will continue to look behind the memory, and just find the area of memory that the system is forbidden to access, then the program rushes directly to collapse.

The mechanism of allocating memory for character arrays conclusion:

If the array is defined as: char str[5] = "Hello", then the memory allocation does not add '% ', so the sizeof (STR) result is 5

If the array is defined as: char str2[] = "Hello", the memory allocation will be added '% ', so the sizeof (STR) result is 6

If the array is defined as: char str3[7] = "Hello", then ' \0\0 ' is added to the memory allocation, so the sizeof (STR) result is 7

The above STR,STR2,STR3 after the strlen calculation results are 5. and the value of sizeof is always changing.

Description Strlen is the actual length of the computed string, and sizeof is the length of the calculation when the array is initialized


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C Language: Memory address analysis & sizeof and Strlen usage Summary

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.