The new operator returns a pointer to the memory allocated to an object, or returns NULL if the memory cannot be allocated. This isn't an error as such, it simply means there isn't a large enough block of free memory available to meet the allocation. There may well be more than enough unused memory available, but the memory must be allocated as a single, contiguous block. Modern operating systems use hard-disk space to provide more memory than physically exists in the system (virtual memory), however with a 32-bit system there is an upper limit of 4GB (which includes video memory), of which only 2GB is available to applications. Thus if the user has a lot of programs consuming a lot of memory then it may not be possible to free up enough physical RAM to meet an allocation. Hence the need to check if a returned pointer is non-NULL before attempting to access the memory it points to. On a 64-bit system this is less of a problem because there's effectively unlimited memory (the only limit being free hard-drive space), but even then you must still check a pointer is non-NULL before indirectly accessing the memory it points to.
Chat with our AI personalities
one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.
1. It is the only way to create object. 2. New is a keyword. 3. New operator allocates memory for an object. 4. It is a bit faster and clever way of creating objects or instances.
A new operater is used to allocating a memory space for a particular object.
No, you have to use the operator delete to objects created by new.
The new operator instantiates a named object of a given type while the delete operator destroys an object. The new operator invokes the object's default constructor unless directed to invoke a specific constructor. The delete operator always invokes the object's destructor.