C ++ memory allocation problems _ 1

Source: Internet
Author: User

I. Basic Concepts

1. "stack" and "stack"

"Heap", that is, the FIFO queue, is allocated from the low address to the high address in the system, while "stack" isProgramThe runtime is allocated by the "runtime environment" Manager. Specifically, in C ++, heap allocation is implemented by the program's own operations such as "new, malloc". What is in the heap is entirely determined by the programmer; the "stack" has nothing to do with programmers, and its distribution is determined by the system, because the basic data structure of the computer is "stack" (in fact, it is the computer's command execution sequence, see the computer organization structure). For example: int * P = new (a); P is a compiler arranged in the stack, the pointer to the heap address is stored.

There are some differences between "heap" and "stack": (1) Generally, heap allocation is much slower, mainly because it involves memory allocation.Algorithm. Stack allocation is much simpler, generally as long as the remaining space is large enough. (2) The stack size is pre-defined. For example, vc6.0 seems to have only 1 m (2 m), while the stack size is theoretically addressable space with a high degree of freedom, in general, the heap space is allocated when the program runs.

2. Global (static) Zone: "initialized" global or static variables are stored in one region, while "uninitialized global or static variables" are stored in another adjacent region.

3. Text constant area: it is determined by the compiler at the beginning. You can refer to the basic assembly knowledge. For example, the constant string is here.

5. ProgramCodeZONE: the binary code is here.

 

Ii. Static members of the class

In actual programming, it is sometimes necessary that all objects of the class have a public information. Such as the number of objects, this requires a static class member variable. You must know that the class declaration only aims to tell the compiler the size of the space occupied by the class, and Cannot initialize members. Therefore, static members cannot be initialized at this time. It can only be assigned values outside the class.

Class static functions: this is a bit special. First, it can only access static data members and function members (because its function pointer type is nonmember and there is no this pointer, in fact, this is for specific objects of the class ). This has a benefit, such as setting the callback function. In this way, we can combine C ++ and C-based xwiindow, and at the same time, it can be successfully applied to the thread function.

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.