C Language Memory and address

Source: Internet
Author: User

Memory and Address

Before introducing pointers, let me tell you about computer memory and addresses. Computer memory can be thought of as a train, we know the train has a lot of cars, each car has a car number (people who have been on the train know), each car can be likened to a piece of computer memory, the compartment has a seat number, through the seat number can only determine a seat, The seat number is good. The offset of a memory block, through which it can uniquely determine the location of the data store.

Note that each location in memory is identified by a unique address, and each location in memory contains a value.

Now an example of an in-memory data store, such as the following table, the bold border portion of the table represents the actual stored data in memory, and the borderless portion represents the address of the memory unit.

10004

10000

10006

10008

10012

10016

+

-1

16 /strong>

1234567890

10000

10008

The careful person might see that the 1 or 16 only accounted for two bytes, can be roughly seen as the short int type data, 110 in memory accounted for 4 bytes, can be roughly regarded as the int type, and 1234567890 can be roughly regarded as floating point data, so you should understand that although the same address, Different types of data may also be stored.

If you can remember the storage address of a value, you might get this value based on this address. But it's too clumsy to remember all these addresses (because the memory address is not as simple as the one shown in the table), so a feature of the high-level language is to access the memory location by name rather than by address. The following table is the same as the previous one, but this time use the name instead of the address.

+

1234567

TD valign= "Top" width= ">

A

B

C

D

E

F

-1

+

10000

10008

As can be seen from the table above, when an address is abstracted into a name that is easier for us to accept, it becomes simple for programming, which is the legendary variable. However, it is important to remember that the association between the name and the memory location is not provided by the hardware, it is implemented by the compiler for us. All of these variables give us a more convenient way to remember addresses-the hardware still accesses the memory location through the address .

Well, let's take a look at the two tables above, and I have a question about whether the contents stored in the variable e represent a value or an address?

The problem is simple, not sure.

Example 1:

int a = 110;

int *e = &a;

In Example 1, E represents an address that points to an integer variable A.

Example 2

int e = 10000;

Example 2, E represents a value.

We already know that the type is required to define the variable, and the compiler implements the association between the variable and the memory location, what is the type of the variable? The variable name is equivalent to helping us find that memory, and the variable type tells the compiler to take a few bytes from the memory location that the variable points to. such as int a = 110; is 4 bytes from 10000 (a corresponds to the memory address), so the address from 10000 to 10003 is the space stored in variable A, and variable a has 232 values.

while short int b =-1, the delegate takes sizeof (short int) bytes from 10004, that is, 10004, 10005 of these 2 bytes. Others can be and so on.

For type we can understand this, or take the train as an example, when a trip, buy a ticket can, this situation is equivalent (char type); one day just go out with your girlfriend, buy a ticket is not enough, you have to buy 2 tickets (short int type) If 4 people in a dorm are ready to travel, the show will be 4 tickets (int,float type); Trains have different needs for different people to allocate seats, and memory allocates different memory space according to different variables. Understanding these, the definition of memory allocations for complex types such as arrays, structs, and union variables is clear.

C Language Memory and address

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.