Memory allocations, function calls, and return value problems in C + + (1/2)

Source: Internet
Author: User
Tags constant

Before talking about function call and return value problem, let's look at the problem of memory allocation in C + +.

The C + + compiler divides computer memory into code areas and data areas, and it is clear that the code area is where the program code is stored, and the data area is the variables and constants that appear in the process of compiling and executing the program. The data area is divided into static data area, Dynamic Data area and constant area, and dynamic Data area includes heap area and stack area.
is the role of each district:
(1) Code area: Store program code;
(2) Data area
A. Static data area: the memory that is allocated for the variable when the compiler compiles, that is, global variables and static variables (variables declared by static), the system is automatically released after all the data programs stored in this zone are executed, and the declaration cycle runs through the whole process of execution.
B. Heap area: This part of the storage space is entirely managed by the programmer itself, and its allocation and release are the responsibility of the programmer. This area is the only one that can be determined by the programmer to determine the lifetime of the variable. You can use Malloc,new to apply for memory, and free space by using the release and delete. If the programmer had applied for space in the heap area and had forgotten to release the memory, it would have caused a memory leak that would have prevented access to the storage area later.
C. Stack area: Store the form parameters and local variables of the function, which are allocated and automatically released by the compiler, and the space occupied by local variables and parameters will be released automatically when the function is finished. Efficiency is high, but the allocated capacity is limited.
D. Constant area: a constant interval, such as a string constant, to note that data stored in a constant area cannot be modified once it has been initialized.
After you understand the problem of memory allocation, look at the procedure of the function call:
When a function is executed, if there is a parameter, the space is allocated on the stack for the form parameter (outside of the argument of the reference type) and continues into the body of the function, and if the variable is encountered, the variable is allocated space in the different storage area (if it is a variable of the static type). is in the process of compiling has already allocated space), the function within the execution of the statement, if the function does not return a value, it returns directly to the place where the function is called (that is, the execution is far away), and if there is a return value, the return value is copied back, then the execution distance is returned, and after the function has been completed, the stack operation is performed. Release the memory space requested on the stack just inside the function.
Here are a few examples to talk about memory allocation and function return values:
Problem with memory allocation:
int a=1; A in the stack area
Char s[]= "123"; s in the stack area, "123" in the stack area, and its value can be modified
Char *s= "123"; s in the stack area, "123" in the constant area, its value cannot be modified
int *p=new int; P in the Stack area, application space in the heap area (P-pointing area)
int *p= (int *) malloc (sizeof (int)); P in the Stack area, p-pointing space in the heap area
static int b=0; b in the static area

1.test1

  
   
   
#include <iostream>
using namespace Std;

void Test (int *p)
{
int b=2;
p=&b;
cout<<p<<endl;
}

int main (void)
{
int a=10;
int *p=&a;
cout<<p<<endl;
Test (P);
cout<<p<<endl;
return 0;
}
The first and third rows of output are the same, while the first and third rows are different from the second row output. It can be seen from here that when a pointer is passed as a parameter it is only a value, except that the value is only one address, so changes to the formal parameter do not affect the argument.

2.test2

The output may be Hello world!, or it may be a tangle.
This situation occurs because: the STR array declared inside the test function and its value "Hello World" is stored on the stack, and when the value of STR is returned with return, a copy of the STR value is sent back, and the space on the stack is automatically released when the test function finishes. That is, the unit that holds the Hello world may be rewritten to the data, so although the pointer p in the main function points to a unit that holds Hello World, there is no guarantee that the test function will be stored in Hello World after it is executed. So the printed results sometimes come from Hello World and in some cases it's a tangle.

3.test3 execution result is Hello world!

  
   
   
#include <iostream>
using namespace Std;

int test (void)
{
int a=1;
return A;
}

int main (void)
{
int b;
B=test ();
cout<<b<<endl;
return 0;
}
Output is 1
Some people will ask why the value returned here can be printed correctly, is not the stack will be refreshed content? Yes, indeed, after the test function is done, a cell that holds a value is likely to be overridden, but when the function executes return, an int type zero is created, the value of a is copied to the zero variable, so the correct value is returned, even if the cell that holds a value is overwritten by the data, But it will not be affected.

4.test4

   
    
    
#include <iostream>
using namespace Std;

char* Test (void)
{
Char *p= "Hello world!";
return p;
}

int main (void)
{
Char *str;
Str=test ();
cout<<str<<endl;
return 0;
}

Home 1 2 last page
Related Article

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.