Memory analysis of "Learning notes" and "C language" variables

Source: Internet
Author: User

The memory in the computer is the storage space in bytes . Each byte of memory has a unique number, which is called an address . As if the hotel is a room unit, each room has a unique room number, we can find the corresponding room according to room number.

1. Storage of variables

The number of bytes consumed by 1> is related to the type, and also to the compiler environment

2> Variable Instance

int B = 10;

int a = 134;

* Memory from large to small addressing

* Only binary forms are stored

* Each variable has an address: the address of the first byte is the address of the variable.

3> two ways to view memory addresses:%x and%p

4> viewing the binary form of integers

voidPutbinary (intN) {    intBITS =sizeof(N) *8;  while(bits-->0) {printf ("%d", n>>bits&1); if(bits%4==0) printf (" "); } printf ("\ n");}

2. Negative numbers in-memory storage

1 int Main () 2 {3     int B =-ten;   4     return 0 ; 5  

In line 3rd, an integer variable is defined, and its value is-10. How is 10 stored in memory? In fact, any value is stored in the form of complement in memory.

    • The complement of positive numbers is the same as the original code. Like 9, the original code and the complement are 1001.
    • The complement of a negative number equals its positive number and the original code is reversed after +1. (The inverse means 0 change 1, 1 to 0)

Then-the 10 complement calculation process is as follows:

1> first calculates the binary form of 10:0000 0000 0000 0000 0000 0000 0000 1010

2> Reverse 10 binary: 1111 1111 1111 1111 1111 1111 1111 0101

3> results of reverse +1:1111 1111 1111 1111 1111 1111 1111 0110

Therefore, the integer-10 binary form in memory is: 1111 1111 1111 1111 1111 1111 1111 0110

3. Range of values

Memory analysis of "Learning notes" and "C language" variables

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.