C ++ Chapter 2 Data Types

Source: Internet
Author: User
Tags bit set bitset float double

C ++ data is nothing more than constants and variables. constants and variables have a storage structure in the computer.

The program we write and the stored program data are stored in the computer memory in binary sequence. bit is a unit containing 0 or 1 values physically. Its value is negative or positive.

At this level, the set of BITs has no structure. it is difficult to explain these bit sequences in a sense. however, by accident (especially when we access the actual hardware of the machine), we will write programs at a separate bit or bit set level for convenience. the C ++ language provides a set of bitoperators to support bitwise operations and a bitset container type, which can be used to declare objects containing a bitset.

In order to consider these places as a whole, we addStructureThis structure is called byte and word. Generally, a byte is composed of eight characters, and a word is composed of 32 characters, or four bytes.

Type abstraction enables us to make a meaningful explanation of a bit sequence with a fixed length. c ++ provides a set of predefined data types, such as string, vector, and plural. it also provides a set of operators or called operators such as addition, subtraction, equals, and less than operators to manipulate these types.

C ++ also provides a few sets of statements for program flow control, such as the while loop and if statements. These elements constitute

A symbolic system has been used to write many large and complex practical systems. The first step to grasp C ++ is to understand these basic components.

 

Text constant:

L character char is usually used to represent a single character and a small integer. It can be expressed in a machine byte.

L integer int short integer short and long integer long, which represent the integer values of different lengths. in typical cases, short is represented in half a word, int is represented in one machine word, and long is one or two machine words. in 32-bit machines, int and long are generally the same length.

L float double and long double indicate Single-precision floating point numbers, double-precision floating point numbers, and Extended Floating Point Numbers respectively. in typical cases, float is a word, and double is a word. long double is a string of three or four characters.

Char short int and long are called integer types ). the integer type can be signed or unsigned. In the symbolic type, the leftmost bit indicates the symbol bit, and the remaining bit indicates the value. in the unsigned type, all bits represent numerical values. if the symbol bit is set to 1, the value is interpreted as a negative number. If it is 0, it is a positive number. an 8-bit signed Char can represent a value from-128 to 127, while an unsigned char represents a range from 0 to 255.

When a value, such as 1, appears in a program, it is called a literal constant (literal constant): it is called a text because we can only refer to it as its value, it is called a constant because its value cannot be changed.

Each text has a corresponding type. The literal constant is unaddressable. Although its value is stored somewhere in the machine memory, we cannot access their addresses.

 

Variable:

Variables provide us with a memory storage area with a name, which can be read, written, and processed by programs. The variable corresponds to a specific data type, and the content of this data type is to be determined.

A variable is an object.

Storage of variables in memory:

A variable can be considered in two aspects: Space: Scope

Time: lifetime

From the perspective of space:

Local (internal) variables: Local: the variables defined in the function or composite statements (function parameters are also), which are valid in this range.

Global (external) variables: variables defined outside the function. Valid range: Starting from the definition variable to the end of the source file.

Dynamic Storage in terms of time:

Dynamic Storage: The units temporarily allocated when the function is called (auito, register, and form parameters ).

Static storage: the whole time the program exists. (Static global ).

Variable Allocation in memory:

1. STACK: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation method is similar to the stack in the data structure. Advanced, backward, and downward growth.
2. Heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.
3. Global (static)-the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The system is released after the program ends.
4. Text Constant Area-constant strings are placed here. The program is released by the System
5. program code area-stores the binary code of the function body.

  • Theoretical knowledge of heap and stack
    1. Application Method
    STACK:
    Automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.
    Heap:
    The programmer needs to apply and specify the size. In C, the malloc Function
    For example, P1 = (char *) malloc (10 );
    Use the new operator in C ++
    For example, P2 = (char *) malloc (10 );
    But note that P1 and P2 are in the stack.
    2
    System Response after application
    STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
    Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application,
    The linked list is traversed to find the heap node with the first space greater than the requested space. Then, the node is deleted from the idle node linked list and allocated to the program, for most systems, the size of the allocation will be recorded at the first address in the memory space, so that the delete statement in the code can correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.
    3. Application size limit
    STACK: in windows, a stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In Windows, the stack size is 2 MB (OR 1 MB, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, the space available from the stack is small.
    Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
    4 Comparison of application efficiency:
    The stack is automatically allocated by the system, which is faster. But programmers cannot control it.
    The heap is the memory allocated by new, which is generally slow and prone to memory fragments. However, it is most convenient to use.
    In addition, in windows, the best way is to use virtualalloc to allocate memory. Instead of heap or stack, it is to reserve a fast memory in the address space of the process, although it is the most inconvenient to use. However, it is fast and flexible.
    5 Storage content in heap and stack
    STACK: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by local variables in the function. Note that static variables are not included in the stack.
    When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.
    Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.
    6 Comparison of access efficiency

    Char S1 [] = "aaaaaaaaaaaaa ";
    Char * S2 = "bbbbbbbbbbbbbbbbb ";
    Aaaaaaaaaaa is assigned a value at the runtime;
    Bbbbbbbbbbbbb is determined during compilation;
    However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap.
    For example:
    # Include <stdio. h>
    Void main ()
    {
    Char A = 1;
    Char C [] = "1234567890 ";
    Char * P = "1234567890 ";
    A = C [1];
    A = P [1];
    Return;
    }
    Corresponding assembly code
    10: A = C [1];
    00401067 8A 4D F1 mov Cl, byte PTR [ebp-0Fh]
    0040106a 88 4D FC mov byte PTR [ebp-4], Cl
    11: A = P [1];
    0040106d 8B 55 EC mov edX, dword ptr [ebp-14h]
    00401070 8A 42 01 mov Al, byte PTR [edX + 1]
    00401073 88 45 FC mov byte PTR [ebp-4], Al
    The first type reads the elements in the string directly into the CL register, while the second type reads the pointer value into EDX. Reading the characters based on edX is obviously slow.
    ?

    7. Summary:
    The difference between stack and stack can be seen in the following metaphor:
    Using Stacks is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it). If you are full, you can leave, without having to worry about the preparation work, such as cutting and washing dishes, as well as the cleaning and tail work such as washing dishes and flushing pots, his advantage is fast, but his degree of freedom is small.
    Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom.

    The differences between stack and stack are as follows:
    The heap and stack of the operating system, as mentioned above, will not be mentioned much.
    There is also the heap and stack in the data structure. These are different concepts. Here, the heap actually refers to a Data Structure (meeting the heap nature) of the priority queue. The 1st elements have the highest priority; stack is actually a mathematical or data structure that meets the needs of the advanced and later stages.
    Although the stack is called a connection, they still have a lot of difference. The connection is only due to historical reasons.

String:

C ++ provides two types of String Representation: C-style string and string introduced by standard C ++. We recommend that you use the string class.

  • C-style string

A string is stored in a character array and is usually manipulated by a char * pointer. The standard C Library provides a set of functions to manipulate C-style strings:

Int strlen (const char *); int strcmp (const char *, const char *);

We need to include the corresponding header file: inlcude <cstring>. the character pointer to a C-style string always points to an associated character array, even when we write a String constant, such:
Const char * ST = "the expense of spirit/N ";
The system also stores string constants in a string array internally and points st to the first element of the array. How can we manipulate ST in the form of a string?
Generally, we use the arithmetic operation of the pointer to traverse the C-style string. Each time the pointer is increased by 1 until it reaches the end of the null character. For example:
While (* st ++ ){...}
Char * type pointer is unreferenced, and test whether the character to be pointed to is true or false. the value true is any character except empty characters. generally speaking, it is necessary to test whether the pointer points to an object before removing the pointer reference. Otherwise, the program may fail, which is also required before the pointer operation.

String type:

The program may be prone to errors due to the Formation Characteristics of character pointers. To solve this problem, C ++ provides support for character initialization, copying, comparison, character search, and whether it is null.

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.