How many kinds of memory are allocated? The delivery of dynamic memory considerations!

Source: Internet
Author: User

1, memory allocation method is divided into static storage, stack, heap.

Static storage: Within the time the program is compiled, it has been allocated, and the entire runtime of the program exists, such as global variables.

Stack: Created on the stack, during the execution of a function (main function or other sub-function), the storage unit of the local variable within the function can be created on the stack, which is freed automatically at the end of the function execution. So we cannot return variables defined inside the function (the pointers defined within the function can be returned) because they have been destroyed at the end of the function execution. The processor's instruction set has an allocation operation within the stack, so it is highly efficient, but the allocated memory resources are limited.

Heap: Allocated from the heap, also known as dynamic memory allocation, using new or malloc to request any amount of memory, the programmer himself decide when to release memory, use flexibility, the problem is often here.

2. Dynamic Memory Transfer:

First, a variable other than the pointer cannot be returned inside a child function, http://www.cnblogs.com/audi-car/p/4753292.html

Second, when a child function is called, the compiler automatically makes temporary variables for each parameter of the parameter list by default, and these temporary variables are not actually passed back to the main function, and these temporary variables are automatically destroyed at the end of the function call (except for dynamically requested memory, which is not destroyed). So relying on them to implement parameters or memory transfer is purely nonsense.

The most typical example is the more basic swap () function, and the following example:

void getmemory (char *p) {    p=newchar[ten];} int Main (intChar * *argv)    {char *str=NULL;    GetMemory (str);     // Char *str=new char[10];    cout<<str<<Endl;     return 0 ;}

When you execute cout<<str<<endl;, the program crashes. Because this pointer is not initialized, and can not be applied from within the child function to the memory space, is pointing to the unknown location, so it will collapse.

3, how to solve the problem of the 2nd step?

It's simple, it can be solved by reference.

void getmemory (char *&p)    // using reference {    p=newChar [ten];} int Main (intChar * *argv)    {char *str=NULL;    GetMemory (str);     // Char *str=new char[10];    cout<<str<<Endl;     return 0 ;}

4, why use the reference to solve the problem?

Because of this time, the compiler will not make a temporary variable to the parameter P, we clearly point out that this is a copy (reference, is actually a copy of the referenced object), he will not be released at the end of the execution of the child function.

The above content, personal views, welcome to correct.

How many kinds of memory are allocated? The delivery of dynamic memory considerations!

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.