I learned C language directly to C. Now I have to turn to C ++. I personally think that C ++ is the key to learning about its memory allocation.
Next I will use a simple class to study the memory allocation of C ++.
// Definition of the test class
Class Test
{
Public:
Test () {cout <"default" <Endl ;};
Test (int );
Int;
~ Test ();
};
Test: Test (int)
{
Cout <"with Parameters" <Endl;
A = 0;
}
Test ::~ Test ()
{
Cout <"destructor" <Endl;
}
// Main function content
Test T1;
Test T2 (3 );
// T1.a = 3;
Cout <t1.a <endl;
Cout <t2.a <endl;
Const int num = 5;
Test t [num];
Cout <t [0]. a <endl;
// Cout <t [0]. a <endl;
For (int I = 0; I <5; I ++)
{
T [I] = test ();
T [I]. a = I;
Cout <(t [I]). a <endl;
}
Notes:
Test t1; call the default constructor to allocate memory. a is junk content, and t [0]. a is also a garbage value. As for the reason why it is a garbage value, it is because it has not been assigned a value, so if it is initialized for the member in the constructor, it will not be a garbage value. The default constructor provided by the system does not do this.
Test t2 (); this is incorrect, so writing is affected by C #.
Test (); this is acceptable.
For example, cout <endl;
Test ();
T [I] = test ();
Running result: endl default endl destructor (endl; indicates line feed)
From the results, the test () statement can also be executed, and, like the following, a copy constructor is called to generate a temporary class object.
There is no doubt about the dynamic memory allocation (that is, the memory allocation with the new mark), which is very direct.
As I am a beginner, please point out the mistakes I have made. Haha