S
subramanian100in
The following discussion is for learning purpose only.
1)
Suppose for a type T, I have
T *ptr = new T[100];
To free ptr, suppose I use ( I deliberately omit [] in delete.)
delete ptr;
Learner's Question:
Will this always free the first element in the allocated array and
remaining allocated memory is leaked ? Or does it invoke undefined
behaviour ?
2)
Suppose I have
size_t size = get_size();
Suppose get_size() returns zero. That is size = 0.
T *ptr = new T[size]; // zero only - this is allowed
But the question is: is it necessary to free ptr and if so what is the
correct syntax to free it ?
Is it : delete [] ptr; or just: delete ptr; ?
Kindly explain.
Thanks
V.Subramanian
1)
Suppose for a type T, I have
T *ptr = new T[100];
To free ptr, suppose I use ( I deliberately omit [] in delete.)
delete ptr;
Learner's Question:
Will this always free the first element in the allocated array and
remaining allocated memory is leaked ? Or does it invoke undefined
behaviour ?
2)
Suppose I have
size_t size = get_size();
Suppose get_size() returns zero. That is size = 0.
T *ptr = new T[size]; // zero only - this is allowed
But the question is: is it necessary to free ptr and if so what is the
correct syntax to free it ?
Is it : delete [] ptr; or just: delete ptr; ?
Kindly explain.
Thanks
V.Subramanian