C + + Written test-2, Basic theory

Source: Internet
Author: User

The test paper is to learn from other blog content, recorded in the back to facilitate their own reading review

Resources:

Http://www.cnblogs.com/TonyEwsn/archive/2010/01/29/1659496.html

1. Contents of the Stack:

Heap, queue first, FIFO first. Stack, advanced back out

? A program compiled by C + + has a memory that is divided into the following sections

1. Stack (stack)-Automatically allocated by the compiler to release, store the function parameter value, local variable value and so on. It operates in a manner similar to a stack in a data structure.

2. Heap area-allocated by the programmer to release, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is not the same as the heap in the data structure, but the distribution is similar to the linked list.

3. Global zone (static zone)-the storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another contiguous area. Released by the system after the program is finished.

4, literal constant area-the constant string is placed here, the program is released after the end of the system.

5. Program code area-binary code that holds the function body.

int a = 0; Global initialization Zone

Char *p1; Global uninitialized Zone

Main ()

{

int b; Stack

Char s[] = "ABC"; Stack

Char *p2; Stack

Char *p3 = "123456"; 123456\0 in the constant area, p3 on the stack.

static int c = 0; global (static) initialization zone

P1 = (char *) malloc (10);

P2 = (char *) malloc (20);

}

The allocated area of 10 and 20 bytes is in the heap area.

But note that P1, p2 itself is in the stack. P1, p2 is a pointer, placed in the stack

strcpy (P1, "123456"); 123456\0 is placed in a constant area, the compiler may optimize it to a place with the "123456" that P3 points to.

Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.

Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.

The stack is automatically assigned by the system and is faster. But programmers can't control it.

Heap is the memory allocated by new, the general speed is relatively slow, and prone to memory fragmentation, but the most convenient to use.

Stack: When the function is called, the first stack is the address of the next instruction (the next executable statement of the function call statement) after the function call in the main function, and then the parameters of the function, in most C compilers, the arguments are in the right-to-left stack, followed by the local variables in the function. Note that static variables are not in the stack.

When the function call is finished, the local variable is first out of the stack, then the parameter, and the last stack pointer points to the first saved address, which is the next instruction in the main function, and the program continues to run from that point.

Heap: The size of a heap is typically stored in a heap at the head of a pile. The concrete contents of the heap are arranged by programmers.

In the actual work of the application summary:

If the data is to be read frequently, such as the contact data of the telephone book, first define a struct in the module layer,

typedef struct CONTACTS_ITEM
{
Intnid;
Stringstrname;
stringstrspelling;
vector<string>vnumber;
vector<int>vnumbertype;

Contacts_item ()
{
NId =-1;
}

BOOL operator = = (Const Contacts_item & tdata) const
{
return (NId = = Tdata.nid);
}

BOOL operator! = (const Contacts_item & tdata) const
{
Return! (*this = = Tdata);
}

Contacts_item & operator = (const Contacts_item & Tdata)
{
if (This! = &tdata)
{
NId = Tdata.nid;
StrName = Tdata.strname;
strspelling = tdata.strspelling;
Vnumber = Tdata.vnumber;
Vnumbertype = Tdata.vnumbertype;
}
return *this;
}

}t_contacts_item;

typedef vector<t_contacts_item> Contacts_vector;

then Create the variable for this struct directly on the view layer:

Contacts_vector m_searchlist;

The resulting m_searchlist is in the stack, and the benefit is that it can be read quickly and frequently.

But if the amount of data is large, then you need to put the data into the class instead of the struct, and then the object of the class new. This can avoid memory overflow, data overflow will cause the app to crash, in Linux under the program crashes will produce core files, which can be located in the cause of the crash, there will be an array offside, memory overflow these problems. Doing a good job of pre-management of the stack can effectively avoid the pain of looking for core files later.

C + + Written test-2, Basic theory

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.