C + + A and a *a=new a ()

Source: Internet
Author: User

New is allocating memory on the heap, which needs to be released with delete, or it can cause a memory leak.

A A will automatically release memory after the program has finished executing.

int main () {

A a;//defines an object

A *p=new a (); An object is defined on the heap, its pointer is stored in P; the object defined on the heap has no name and must be saved with a pointer.

return 0;

When}//A is here, the memory it occupies will be released, and p, unless you call delete p, memory will never be recycled, and the pointer p is discarded.

The memory is not released, can not be used again, resulting in a waste of memory

Dynamically allocating memory New keyword

1 allocating memory dynamically, allocating memory from the heap. Local variables are generally stored on the stack.

2 using new allocates memory for an object from the free store and returns a pointer to the object, which is the address of the object. Features of the new operator:

Objects assigned with the new operator do not have a name, and operations on the object are done indirectly through the pointer.

3 dynamically creating arrays: int *p=new int[11];

4 run out of memory: If the program runs out of all available memory, the new expression may fail. If the new expression does not get the required memory space. The system will throw an exception named Bad_alloc.

5 dangling pointer: After executing delete p, only the content that the P points to is destroyed, it does not remove the pointer p itself, but it can also point the P back to a new block of memory, so p also points to the address of the object to which he pointed, but the content that P points to is released for destruction, so p is no longer valid and becomes meaningless.

6 Common error: If memory is dynamically allocated to one pointer and the address of another variable is paid to the pointer, then the pointer points to a static address instead of the original dynamic address. When you delete this pointer with delete, the error occurs because delete cannot delete the static pointer.

#include <iostream> using namespace std;
	Class Hyong {Public:int A, B, C;
	Hyong () {a = b = c = 0;
	} hyong (int i) {a = b = c = i;
	} ~hyong () {cout << "Xigou" << "n";
}
};
	int main () {Hyong *p = new Hyong;
	Hyong *p1 = new Hyong (); cout << p->a << p1->a <<endl;
	The output of two 0 calls the default constructor initialization pointer.
	int *p2 = new int;
	int *p3 = new int (); int *P4 = new int (1);
	The new way to allocate memory initialization.
	For a class-type, P2 is not initialized to get a random value, P3 is initialized to 0//,p4 initialized to 1.  cout<<*p2<<endl<<*p3<<endl<<*p4<<endl;
	Output a random value, a 0, a 1 int i = 10;
	Delete P4; cout << *p4 << Endl;
	P4 is now a dangling pointer, and delete just releases the contents of the pointer P4 to the address,//But the pointer P4 still points to the original address, but there is no content, and the pointer P4 can still point to another address.
	P4 = &i; cout << *p4 << Endl;
	You can p4 an address to a dangling pointer.
	const int *P5 = new int;
	const int *P6 = new int (4); cout << *p5 << *p6 << Endl;
	Output a random value and a 4,const constant must be initialized at the time of declaration.
	int *p7 = new Int[2];
	P7[0] = 5;
	P7[1] = 6; cout << p7[0] << p7[1] <<endl;  Defines a dynamic array//const int *p8=new int[2];
	Error, because a dynamic array cannot be initialized at declaration time, and the const must require initialization at the time of declaration, a conflict, an error.
	Delete P1;
	Delete p;   Delete P,p1;
	Note that if you use this statement, you will call only one destructor.
	Delete P2, p3, P4, P5, P6;
	Delete[] P7; int *p8=new int (9);  int a=8; p8=&a;  Delete P8; Error, now the pointer P8 a static address,//deletes a static address with delete error}




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.