C ++ common memory errors

Source: Internet
Author: User
In C and C ++, s t a t I c has two basic meanings, and these two meanings often conflict with each other:
1) allocated on a fixed address, that is to say, the object is created in a special static data zone, instead of being generated on the stack for each function call. This is also the concept of static storage.
2) It is local to a specific compilation unit (as we will see later, this includes the class range in C ++ ). S t a t I c controls the visibility of the name, so the name is invisible outside of the unit or class. This also describes the concept of connection, which determines what names the connector will see.
1. Static components that do not belong to the class

1.1. Static variables

1.1.1. modify local variables
This variable scope is confined to the function body. However, unlike non-static local variables, the value of this variable is retained, instead of being initialized every time this function is called.

Void fun (void ){
Int Var = 0;
Static int svar = 0;

VaR ++;
Svar ++
}

After function fun is called for the first time, var and svar values are changed to 1. After the second call, VaR is still equal to 1, while svar value is changed to 2.

1.1.2. Static global variables
Global variables are static by default (without static modification)
File1.cpp
Int gi;

Gi is a global variable, which is automatically stored statically.

1.1.2.1. modify global variables with static
Restrict this global variable to this file (by default, global variables can be used by any file)
File1.cpp
Int gi;
File2.cpp
Extern int gi;

If you use static to modify GI (static int gi;) in file1.cpp, you cannot use GI in file2.cpp.

1.2. Static Functions
The static keyword allows this function to be called only by other functions in this file.

 

 

2. Static components of the class
2.1. Static class members (variables)

The State data member is treated as a global object of this type. For non-static data members, each class object has its own copy, while the static data member has only one copy of the static data member for each class type. shared access by all objects of this type.
Static data members are initialized outside the definition of this class. Like a global object, static data members can only provide one definition in the program. This means that static data member initialization should not be placed in the header file, but should be placed in a non-inline function definition file containing the class..
Static data members can be of various data types (pre-negotiated data types and user data types ).
Static data members can be const or non-const members.
2.1.1. Non-const static class members

Header file
Class CB {
Public:
CB () {mi = 2 ;}
CB (int I) {mi = I ;}
Int Mi;
};

Class ca {
Public:
CA () {mi = 3 ;}
Int Mi;
Static int SMI;
Static int smi2;
Static CB ob;
Static CB ob2;
};

Implementation File

Int CA: SMI; // cannot be written as int CA: SMI ();
Int CA: smi2 (5 );
Cb ca: OB; // cannot be written as Cb CA: OB ();
Cb ca: ob2 (5 );

Int main (INT argc, char * argv [])
{
Int I = 0;
Int J = 0;

I = Ca: SMI;
I = Ca: smi2;
J = Ca: OB. Mi;
J = Ca: ob2.mi;

Return 0;
}

2.1.2. Const static class member

Header file
# Include <string>
Using namespace STD;

Class CB {
Public:
CB () {mi = 2 ;}
CB (int I) {mi = I ;}
Int Mi;
};

Class ca {
Public:
CA () {mi = 3 ;}
Int Mi;

Static const CB ob;
Static const CB ob2;
};

Implementation File

Const cb ca: OB;
Const cb ca: ob2 (5 );

Int main (INT argc, char * argv [])
{
Int J = 0;
String name;

Name = Ca: Name;
Name = Ca: name2;
J = Ca: OB. Mi;
J = Ca: ob2.mi;

Return 0;
}

2.1.3. Access static class members
Two Methods: direct access and access through class objects.

Direct Access
That is, you can directly access it with a name qualified by the class name. Example CA: Name;
Class Object Access
Ca oa;
Oa. Name;
Or
Ca * PoA;
PoA-> name;

2.1.4. A special purpose of static data members
The type of a static data member can be the class to which it belongs, rather than the pointer or reference of an object declared as this class.

// ----------- Header file
Class ca {
Public:
Static Ca * instance () {return & SOA ;}
PRIVATE:
CA (int I): MI (I ){};
PRIVATE:
Int Mi;
Static ca SOA;
};

// ----------- CPP File
CA: SOA (5 );

This is because static data members do not occupy the storage space of class objects. Therefore, the return value of sizeof CA is 4.
The preceding example uses this feature to implement the singleton mode.

2.1.5. Note
1. Local classes cannot have static member variables (but Nested classes can)
// -------- Local class
Void fun (){
Class Foo {
Static int Mi; // Error

}
}

// -------- Nested class
Class outer {
Class inner {
Static int Mi; // correct

}
}

2. When using static const int class members to define the array of class members, it depends on whether the current compiler supports this feature.

Class ca {
Public:
Int Mi;
Static const int size;
Char SZ [size];
};

 

2.2. static member functions
Adding the static keyword before a member function makes this member function a static member function of the class.
Static member functions have several special features:
Cannot be declared as const or volatile
No this pointer. Therefore, implicit or explicit reference to this pointer in a static member function will cause compilation errors.
Cannot access non-static data members

2.2.1. Access static class member functions
Three methods: direct access and access through class objects.
Class ca {
Public:
CA () {mi = 3 ;}
Int Mi;
Static int fun ();
};

Int CA: Fun (){
Return 9;
}

Direct Access

Int I = Ca: Fun ();

Class Object Access
Ca oa;
Int I = Oa. Fun ();
Or
Ca * PoA;
PoA-> fun ();

Access through function pointers
INT (* PFn )();

PFN = Ca: fun;
I = PFN ();
Or
PFN = Oa. fun;
I = PFN ();

3. Static Object Memory Allocation

3.1. Static variables in the function
Both C and C ++ allow the creation of an s t a t I c object in the function. This object will be stored in the static data area of the program, rather than in the stack. This object is initialized only once when the function is called for the first time. In the future, it will keep its value between the two functions.

3.2. Global static object
In C ++, the constructor of the Global static object is called before m a I n (), so we now have () A simple and portable Method for executing a piece of code before, and you can use the destructor to execute the code after exiting m a I n. To do this in C, we have to be familiar with the starting code of the compilation language of compiler developers.

3.3. destructor of static objects
When the program exits from the main () block, or the Standard C library function e x I t () is called. In most cases, the end of the m a I n () function is to call e x I t () to end the program. This means that using e x I t () inside the Destructor is very dangerous, because it may fall into an endless loop. However, if the standard C library function a B o r t () is used to exit the program, the destructor of the static object will not be called.
We can use the standard C library function a t e x I t () to specify when the program jumps out of m a I n () (or calls e x I t ()) the operation to be performed.
In this case, before jumping out of m a I n () or calling e x I t (), use a t e x I t () the registered function can be called before the destructor of all objects.
Static objects are destroyed in reverse order during initialization. Of course, only those created objects will be destroyed. Fortunately, the programming system records the object initialization sequence and created objects. The global object is always created before m a I n () is executed. Therefore, the last statement takes effect only for the static object in the local part of the function. If a function that contains a static object has never been called, the constructor of this object will not be executed, and the Destructor will naturally not be executed.

 

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.