new allocates memory and also invokes constructor to initialize the object.
Some more -
'malloc()' is a function inherited in C++ from C. It is defined in '
Its main purpose is to request the OS (free store) for a bunch of memory bytes for you.
You would make a call to 'malloc()' and the request goes to the operating system that allocates the requested amount of contiguous raw bytes if possible.
On success, you would get a pointer to the first byte of allocated memory.On failure, 'NULL' would be returned.
'free()' is a function inherited in C++ from C. It is defined in '
Every 'malloc()' call must be followed by a call to 'free()' else you get into a situation called a memory leak.
That piece of malloc-ed memory remains unusable to your program as well as the OS, until your program ends.
void free(void* memblock);
Here, 'memblock' is the same pointer that you would get from a call to 'malloc()'. It is also a good practice to set that pointer to 'NULL' after the 'free()' call and always have the 'free()' call surrounded by a check on the validness of the pointer.
Calling 'free()' on the same pointer twice or a 'NULL ' pointer or any other pointer (not acquired by a 'malloc()' call) leads to an undefined behaviour of the program (including but not limited to a crash).
'new' is a C++ operator. The task that you will use this for is still request for dynamic allocation of one object or multiple objects. On success, it returns a suitably typed pointer and on failure it would throw an exception ('std::bad_alloc').
There is a nothrow version of 'new' as well. C++ objects are different. They are constructed in basically two steps:
* first, allocation of requried amount of bytes and
* second, a successful call of the constructor.
class A
{
/*some members and a public constructor taking no arguments*/
public:
A() { /* ... */ }
};
int main()
{
A* ptr = new A(); // (1)
A* ptrToArrayOfAObjects = new A[10]; // (2)
}
At statement labeled (1), we use 'new' to allocate memory for a single 'A' object and then a call to the constructor of 'A' happens. To verify, put a 'cout' statement in the constructor and see for yourself. At statement labeled (2), we use array form of new to construct 10 'A' objects. That is, allocate for 10 objects and call the constructor for each. That is what 'new' or 'new[]' does. This array version of 'new' requires a default constructor (i.e. a constructor without parameters) or a contructor with default values for all the parameters (that evaluates to a default constructor).
'delete' It is the counter-part of 'new' and also a C++ operator. The task that you will use this for is to free up memory acquired by a call to 'new'. For the allocations shown in the above code you will need to do the following.
class A
{
/*some members and a public constructor taking no arguments*/
public:
A() { /* ... */ }
~A() { /* ... */ }
};
int main()
{
A* ptr = new A(); // (1)
A* ptrToArrayOfAObjects = new A[10]; // (2)
delete ptr;
delete [] ptrToArrayOfAObjects;
}
'delete' does two things as well:
* it first calls the destructor of the object and then
* makes the deallocations.
Calling 'delete' on the same pointer twice or some different pointer (not acquired by a 'new' call) leads to undefined behaviour. But calling 'delete' on 'NULL' pointers is safe and hence it is a good practice to set the pointers to 'NULL' after a call to 'delete', but unlike 'free()' no checks here for validness is required.
You have to make sure you use the correct match of 'new' and 'delete'. According to the C++ standard:
* if you allocate with 'new' you release with 'delete'
* if you allocate with 'new[]' you release with 'delete[]'
By now, you should be able to understand the differences between 'malloc()'/'free()' and 'new'/'delete' but still I will put up some points to make it really explicit so as to kill any scope of confusions.
-malloc()' allocates a bunch of bytes while 'new' does the allocation as well as has the responsibility of calling the constructor.
-malloc' returns 'NULL' on failure while 'new' throws an exception object of type 'std::bad_alloc'. (There exists a nothrow version of 'new', though)
-'malloc' is the C way of dealing with the free store while 'new' is the C++ way.
-Every call to 'malloc()' should be followed by a call to 'free()' while every call to 'new' should be followed by a call to 'delete'.
-'free()' deallocates all bytes allocated by a prior 'malloc()' call (using the same pointer) while 'delete' makes a call to the destructor and then goes on with the deallocation
-There are array forms of 'new' and 'delete' while 'malloc()' and 'free()' do not (as the request is for a specific size of raw memory in bytes).
-'free()' on 'NULL' pointers is not safe while 'delete' is.
No comments:
Post a Comment