473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

delete and delete []

I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?

(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)

Mar 30 '07 #1
5 1501

"SpreadTooThin" <bj********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
>I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;
Correct.
however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?
No, you must still use delete[]. It's still an
array (albeit whose objects are of a different type).
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)
The array's element type has no bearing on this.
If you use 'new', you must deallocate with 'delete'.
If you use 'new[]', you must deallocate with 'delete[]'.
Simple, huh?

-Mike
Mar 30 '07 #2
On Mar 30, 1:11 pm, "SpreadTooThin" <bjobrie...@gmail.comwrote:
however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?
No, not irrelevant. If you allocate with [], delete with [].

Mar 30 '07 #3
SpreadTooThin wrote:
I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?
No, of course not. Why do you think there might be a difference?
It's an array. You use 'new[]' to allocate it, you use 'delete[]'
to free it.
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)
No, you can't change the object type. That's nonsense, sorry.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 30 '07 #4
On 30 Mar, 18:11, "SpreadTooThin" <bjobrie...@gmail.comwrote:
I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?
Who told you that? It certainly is relevant. The behaviour of mixing
new [] with delete is undefined.
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)
As ever with undefined behaviour, whether you have issues, what those
issues are, whether they are different for different types etc. is
outside the scope of the language definition.

Gavin Deane

Mar 30 '07 #5
On Mar 30, 1:11 pm, "SpreadTooThin" <bjobrie...@gmail.comwrote:
however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?
You seem to be misunderstanding what delete & delete[] do.

basically, they functions like:
delete pObject; =

pObject->~object();
free_n_bytes_of_memory(pObject, sizeof(object));
delete[] arrObject; =

for(int i =0; i < N; ++i)
arrObject[i].~object();
free_n_bytes_of_memory(arrObject, sizeof(object)* N );
Nw, you are correct in surmizing that, as a char dtor is empty, the
different in the first part of each is irrelevent. However, the
difference in the second half is still significant.

To further confuse the issue, on most platforms,
free_n_bytes_of_memory could probably be implemented as:

void free_n_bytes_of_memory(void* ptr, int )
{
free(ptr);
}

with the size parameter ignored, but that would be an implementation
detail, which you can't and shouldn't depend on.
Mar 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
1
by: Douglas Peterson | last post by:
class Allocator { public: virtual void * Alloc(size_t) = 0; virtual void * Free(void*) = 0; }; class Object { public:
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.