Array Memory Structure

Source: Internet
Author: User
17.1 array and memory

The variable needs to occupy the memory space, and the memory space has an address.Variables of different data types may occupy different memory sizes and have different memory structures.

What we have learned previously is called "simple data type", for example:Int, Char, float, double, bool. ImageChar, bool,Only occupies one byte, so we do not care about its "structure ".Int, float, doubleIt takes up multiple bytes, but it is relatively simple. When appropriate, we will explore how the four bytes form an integer.

Then we learned about arrays. The size of memory occupied by array variables is not fixed, because different array variables can have different types and different numbers of elements, which affect the size of these two variables.

Therefore, array is the first data type to focus on its structure. Compared with the more data types we will learn later, the array structure is quite simple. Simply put, the elements are arranged neatly in the same size.

Memory Structure of the 17.1.1 Array

Variables need to occupy the memory space. The memory space has an address.

Declare an integer variable

Int;

The system will apply for a space of the corresponding size for this variable. When an int type variable is used, it will take up four bytes of space, for example:

That is to say, the memory structure of an int type variable is "4 consecutive bytes ".

When we declare an array:Int arr [100];

We can imagine,ArrThe array occupies 100 of the memory.* Sizeof (INT)Bytes.

OpenWindowsPaint BrushProgramHome draws an array of memory structures.

Memory Address of the 17.1.2 Array

OneIntType variable, which occupies 4 bytes of memory. The first byte is called the memory address of the variable.

Similarly, an array variable occupies a continuous memory. The first byte is called the memory address of this array variable.

Remember&Is this symbol? You can obtain the memory address of the specified variable.

Int;

Cout <& A <Endl;

&It is called the "access operator ". If you cannot remember the course, you can view the previous course.

The first part of this chapter requires special attention:

View the address of the array variable, no need to use&. The following is also a conclusion. You must remember it.

In C, C ++, the operation on the array variable is equivalent to the operation on the address of the array variable directly.

Therefore, to view the address of an array variable,CodeIs:

Int arr [10];

Cout <arr <Endl ;//Note,ArrNot Required&.

OpenCB,Then, write the code above into a complete console program and check the output result.

17.1.3 memory address of the array element

An array variable contains multiple consecutive elements. Each element is a common variable. Therefore, you can use&To obtain the address:

//View the address of the first element in the array:

Int arr [10];

Cout <& arr [0] <Endl;

Example 1:

Now, please continue with the code in the previous section in CB. Requirement: UseForLoop, output ArrayArrThe address of each element in.

If you have finished, let's look at my answer.

# Include <iostream. h>

...

Int arr [10];

For (INT I = 0; I <10; I ++)

Cout <& arr [I] <Endl;

...

Cin. Get ();

We can combine it with the previous example of the output array address, and then observe the output result.

...

Int arr [10];

//Output array address:

Cout <"ArrayArrAddress:"<Arr <Endl;

//Output the address of each element:

For (INT I = 0; I <10; I ++)

Cout <"ElementArr ["<I <"]Address:"<& Arr [I] <Endl;

...

Output result:

The first line tells us that the entire array variableArrAnd the first element.Arr [0], The addresses are the same.

In fact, arrays and elements are two different expressions for the same memory segment. Consider the memory as an overall variable, that is, an array. Divide the memory into multiple segments of the same size, that is, an array element.

See:

(Separated segments are elements, which are called an array as a whole.,However, the two correspond to the same memory segment.)

 

 

 

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.