transferred from: http://blog.csdn.net/shihui512/article/details/9092439;
1.new, delete, malloc, free relationship
Delete calls the destructor of the object, and the new free only frees up memory and new calls the constructor. malloc and free are standard library functions for c++/c languages, and new/delete are the operators of C + +. Both can be used to request dynamic memory and free memory. For objects that are not internal data types, Maloc/free cannot meet the requirements of dynamic objects. Objects are created with the constructor automatically executed, and the destructor is automatically executed before the object dies. Because Malloc/free is a library function and not an operator, it is not possible to impose the task of executing constructors and destructors on Malloc/free without the compiler controlling permissions. The C + + language therefore requires a new operator to complete dynamic memory allocation and initialization, as well as an operator delete that cleans and frees up memory work. Note that New/delete is not a library function. 2.delete differs from delete []
Delete Calls only a destructor, and delete[] invokes the destructor of each member. In more effective C + + There is a more detailed explanation: "When the delete operator is used for an array, it calls the destructor for each array element, and then calls the operator delete to free the memory." "Delete is matched with new, delete [] with new [] matching
Memtest *mtest1=new memtest[10];
Memtest *mtest2=new memtest;
int *pint1=new int [10];
int *pint2=new int;
Delete[]pint1; -
Delete[]pint2; -
delete[]mtest1;//-3-
delete[]mtest2;//-4-
The error is in the division.
This means that the delete and delete[] features are the same for built-in simple data types. For custom complex data types, delete and delete[] are not interoperable. Delete[] Deletes an array, delete deletes a pointer. In simple terms, the memory allocated with new is deleted with delete, and the memory allocated with new[] is deleted with delete[. Delete[] invokes the destructor of the array element. The internal data type does not have a destructor, so the problem is small. If you use delete without parentheses, delete will think that it points to a single object, otherwise it will think that it points to an array. What are the properties of 3.c++ (object-oriented features)
Encapsulation, inheritance, and polymorphism. 4. To invoke the destructor of the parent class when the subclass is destructor.
The order of the destructor calls is to derive the destructor of the base class after the destructor of the class, that is to say, the information of the derived class has been completely destroyed when the destructor of the base class is invoked. The constructor of the base class is called first when an object is defined, and then the constructor of the derived class is called, and the destructor is reversed: The destructor of the derived class is called first, and then the destructor of the base class is called. 5. Polymorphic, virtual function, pure virtual function
Polymorphism: A different action occurs when a different object receives the same message. The polymorphism of C + + is embodied in two aspects of running and compiling: The polymorphism of the program runtime is embodied by inheritance and virtual function;
The polymorphism is embodied in the overload of functions and operators when the program compiles;
virtual function: A member function that is called the keyword virtual in a base class. It provides an interface interface. Allows you to redefine the virtual functions of a base class in a derived class.
function of a pure virtual function: Preserve the name of a function for its derived class in the base class so that the derived class defines it as needed. As an interface, there is no function of pure virtual function, which can not be called directly.
A pure virtual function inherited from a base class that is still a virtual function in a derived class. If there is at least one pure virtual function in a class, then this class is called an abstract class.
Abstract classes include not only pure virtual functions, but also virtual functions. An abstract class must be used as a base class for deriving other classes, not for creating object instances directly. However, you can still support run-time polymorphism with pointers to abstract classes. 6. Find the return value of the following function (Microsoft)
int func (x)
{
int countx = 0;
while (x)
{
Countx + +;
x = x& (x-1);
}
return countx;
}
Suppose x = 9999. Answer: 8
Train of thought: Convert X to 2 to see the number of 1 contained. 7. What is a "reference". The issues to be noted and the use of references.
A: A reference is the alias of a target variable, and the action applied is exactly the same as the direct manipulation of the variable. When you declare a reference, remember to initialize it. After the reference declaration is complete, it is equivalent to the target variable name with two names, that is, the target's original name and reference name, and the reference name cannot be used as an alias for another variable name. Declaring a reference, not a newly defined variable, only indicates that the reference name is an alias of the target variable name, it is not a data type itself, so the reference itself does not occupy the storage unit and the system does not allocate the storage unit to the reference. Cannot establish a reference to an array. 8. What are the characteristics of a "reference" as a function parameter?
(1) The effect of passing a reference to a function is the same as passing a pointer. At this time, the parameter of the function is called the real parametric or an alias of the object in the original keynote function, so the operation of the parameter variable in the modulated function is the operation of its corresponding target object (in the keynote function).
(2) using the parameter of the reference pass function, there is no copy of the argument in memory, it is a direct operation of the argument, while using a generic variable to pass the function's arguments, when a function call is made, the parameter is required to allocate the storage unit, the parameter variable is a copy of the argument variable, and the copy constructor is called if the object is passed. Therefore, when the parameter passes the data is big, uses the reference to pass the parameter with the general variable the efficiency and occupies the space to be good.
(3) The use of pointers as a function of the parameters, although also can achieve the effect of using references, however, it is very easy to make errors and poor reading of the program in the same way that the parameters are allocated to the parameters, and the "* pointer variable name" needs to be used repeatedly. On the other hand, at the call point of the calling function, You must use the address of the variable as an argument. And references are easier to use and clearer. 9. When you need to use the "constant reference".
If you want to use the reference to improve the efficiency of the program, but also protect the data passed to the function is not changed in the function, you should use the constant reference. Constant reference declaration method: Const type identifier & reference name = target variable name;
Example 1
int A;
const INT &ra=a;
Ra=1; Error
A=1; That's right
Example 2
string foo ();
void Bar (string & S);
Then the following expression will be illegal:
Bar (foo ());
Bar ("Hello World");
The reason is that Foo () and the "Hello World" string all produce a temporary object, whereas in C + + These temporary objects are const types. So the expression above is an illegal attempt to convert a const-type object to a non-const type. Reference parameters should be defined as const if they can be defined as const.
10. To