Use a program to analyze heap and stack Problems

Source: Internet
Author: User

Case 1: (error)
# Include <vector>
Using STD: vector;
# Include <string>
Using STD: string;
# Include <iostream>
Using STD: cout;
Using STD: CIN;
Using STD: Endl;
Int main ()
{
Vector <string *> spvec;
String STR;
While (CIN> Str)
{
String * pstr;
Pstr = & STR; // The result is that every time pstr points to Str.
// In addition, pstr is a temporary variable. in case of "}", it is destroyed.
// Rewrite. The final result is the last STR;
Spvec. push_back (pstr );
}
For (vector <string * >:: iterator iter = spvec. Begin (); iter! = Spvec. End (); ++ ITER)
{
Cout <** ITER <"";
}
Getchar ();
Return 0;
}
After running the above program, assume that the input adfdsfds fdsfds dfdf fd df dffd df d sfd Fas
Running result: Fas FAS Fas

Analyze the while loop:
1. create a temporary variable * pstr pointing to the STR string ("adfdsfds") and place the pointer to spvec [0]. When "}" is encountered, pstr survival ends, that is, release. spvec [0] --> STR, STR survival ends until "}" of main.
2. create a new temporary variable * pstr pointing to the STR string ("fdsfds") and place the pointer to spvec [1]. When "}" is encountered, pstr survival ends, that is, release, spvec [1] --> STR ("fdsfds"), and the STR value pointed to by spvec [0] has been changed, so * spvec [1] = "fdsfds ";
......
As a result, * spvec [N] means that ** ITER is the last Str.
The above pstr temporary variable is created on the stack, and his lifetime is his statement block (that is, a pair closest to him {}).

 

Case 2: (correct)
Write a model first
Spvec string Str
ITER --> spvec [0] (pstr [0]) --> Str
Spvec [1] (pstr [1]) --> Str
Spvec [2] (pstr [2]) --> Str
Spvec [3] (pstr [3]) --> Str
Spvec [4] (pstr [4]) --> Str
Spvec [5] (pstr [5]) --> Str
Spvec [6] (pstr [6]) --> Str
Next
String * pstr = & STR; change to string * pstr = new string;
* Pstr = STR;
Spvec. push_back (pstr );
Running result: adfdsfds fdsfds dfdf fd df dffd df d sfd fas // normal
Analyze the while loop:
1. create the variable * pstr on the stack, and then * pstr = Str. Copy the content of STR directly to the space pointed to by pstr. When this space Encounters "}", the lifetime is not over, the result is spvec [0] = pstr [0].
2. create another variable * pstr on the stack, and then * pstr = Str. Copy the STR content directly to the space pointed to by pstr. When this space Encounters "}", the lifetime is not over, the result is spvec [1] = pstr [1].
.....
As a result, each spvec saves a pointer.
The above pstr temporary variables are created on the stack, and the stack is managed by the operating system. We must release the stack by ourselves, otherwise it will cause memory garbage.

 

Case 3: (incorrect)
String * pstr; // new cannot be used. If not, this pointer points to an unknown space.

// (It cannot be equal to or greater than 0. It just initializes and does not point to any object)
* Pstr = STR; // use STR to assign a value to the content of the pointer space. An exception occurs.
Spvec. push_back (pstr );

 

Case 4: (incorrect)
String * pstr = new string; // similar to the first case. Although there is space allocated on the heap
Pstr = & STR; // every time pstr is taken as the STR address, resulting in every * spvec [N]

// The string is the last one.
Spvec. push_back (pstr );

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.