473,371 Members | 1,744 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,371 software developers and data experts.

clarification on delete []

Which of the following do I use delete[] instead of just delete.

//1.)
// not sure about this one, as char is of size 1
char *str = new char[10];

//2.)
//not sure about this one, as it is a primitive
int *array = new int[100];

//3.)
// I know I need delete [] for this one
Obj *array = new Obj[2];

//4.)
// what about this one?
//I think I have to manually
//call delete on each element,
//then delete ptrArray?
Obj **ptrArray = new Obj*[2];
ptrArray[0] = new Obj();
ptrArray[1] = new Obj();

//5.)
//similar but a 1 byte primitive
char **strArray = new char*[2];
strArray[0] = new char[10];
strArray[1] = new char[10];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");

//I think this is the same as above, but uglier:
char **strArray = (char**)new char[20];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");

//6.)
//similar but a multi byte primitive
int **intArray = new int[2];
intArray[0] = new int[10];
intArray[1] = new int[10];

Thanks,

~Shea M.

Jul 19 '05 #1
4 2862
Shea Martin wrote:

Which of the following do I use delete[] instead of just delete.


If you used new[] you need delete[]. If you didn't use new[] you
shouldn't use delete[].

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #2

"Shea Martin" <sm*****@arcis.com> wrote in message news:Igglb.8933$f7.481052@localhost...
Which of the following do I use delete[] instead of just delete.


Always use delete [] with new []
Size or the type of the object makes no difference.
Jul 19 '05 #3
Shea Martin wrote:
Which of the following do I use delete[] instead of just delete.
If it's an array allocated with new[], you need to delete[] it. There is
nothing more to it.
//1.)
// not sure about this one, as char is of size 1
What does the size of char have to do with it?
char *str = new char[10];
You need delete[] here, since you used new[].

//2.)
//not sure about this one, as it is a primitive
Doesn't matter.
int *array = new int[100];
Here, too. new[] again.
//3.)
// I know I need delete [] for this one
Obj *array = new Obj[2];
You're right. This needs it too, since (you probably guessed it) you
used new[].
//4.)
// what about this one?
//I think I have to manually
//call delete on each element,
//then delete ptrArray?
Obj **ptrArray = new Obj*[2];
ptrArray[0] = new Obj();
ptrArray[1] = new Obj();
Not quite. You need to delete[] ptrArray.

//5.)
//similar but a 1 byte primitive
char **strArray = new char*[2];
strArray[0] = new char[10];
strArray[1] = new char[10];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");
No. This is not similar, since you're allocating an array of pointers,
and for each of them, you're allocating an array again, while above you
were allocating one single object for each pointer. So you need to
delete[] each string and then delete[] the pointer array. Once again
one delete[] for every new[].
//I think this is the same as above, but uglier:
char **strArray = (char**)new char[20];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");
No, absolutely not. It's just plain wrong. You're allocating an array of
20 chars and then tell the compiler that you want it to treat that as
an array of pointers instead. Those pointers are uninitialized, and
then you try to write data to whatever bogus address they point to.
//6.)
//similar but a multi byte primitive
int **intArray = new int[2];
intArray[0] = new int[10];
intArray[1] = new int[10];


Doesn't make a difference (why should it?). It's just the same as
example 5 (well the first one).

Jul 19 '05 #4
Rolf Magnus wrote:
Shea Martin wrote:

Which of the following do I use delete[] instead of just delete.

If it's an array allocated with new[], you need to delete[] it. There is
nothing more to it.

//1.)
// not sure about this one, as char is of size 1

What does the size of char have to do with it?

char *str = new char[10];

You need delete[] here, since you used new[].

//2.)
//not sure about this one, as it is a primitive

Doesn't matter.

int *array = new int[100];

Here, too. new[] again.

//3.)
// I know I need delete [] for this one
Obj *array = new Obj[2];

You're right. This needs it too, since (you probably guessed it) you
used new[].

//4.)
// what about this one?
//I think I have to manually
//call delete on each element,
//then delete ptrArray?
Obj **ptrArray = new Obj*[2];
ptrArray[0] = new Obj();
ptrArray[1] = new Obj();

Not quite. You need to delete[] ptrArray.

//5.)
//similar but a 1 byte primitive
char **strArray = new char*[2];
strArray[0] = new char[10];
strArray[1] = new char[10];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");

No. This is not similar, since you're allocating an array of pointers,
and for each of them, you're allocating an array again, while above you
were allocating one single object for each pointer. So you need to
delete[] each string and then delete[] the pointer array. Once again
one delete[] for every new[].

//I think this is the same as above, but uglier:
char **strArray = (char**)new char[20];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");

No, absolutely not. It's just plain wrong. You're allocating an array of
20 chars and then tell the compiler that you want it to treat that as
an array of pointers instead. Those pointers are uninitialized, and
then you try to write data to whatever bogus address they point to.

//6.)
//similar but a multi byte primitive
int **intArray = new int[2];
intArray[0] = new int[10];
intArray[1] = new int[10];

Doesn't make a difference (why should it?). It's just the same as
example 5 (well the first one).

Thanks, for the clarification.
~S

Jul 19 '05 #5

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

Similar topics

2
by: Elliot Rodriguez | last post by:
As I continue to read more about the benefits of database querying using ADO.NET, I am having a more difficult time distinguishing what the best approach to data retrieval is anymore. When...
7
by: Rakesh | last post by:
Hi, I was writing this C++ program wherein I used an include statement like - #include <iostream.h> I was told by my co-worker that this form of including a file is deprecated and should...
5
by: ma740988 | last post by:
Consider a number of instances of template class vector<a>, vector<b> ..., vector<f> The types are - of course different. Of interest is an efficient way to access them at runtime. One...
3
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
19
by: Rob | last post by:
I've just started writing managed C++ (VS 2005 Beta 2) and I really have not read a whole lot about the differences between managed and unmanaged C++. Using the class wizard, I can create a...
3
by: Daniel Klein | last post by:
A recent answer to a post I just made (and a good answer it was) brought up a question that I found I was not clear on, and I'm sure other .NET'ers would like to know as well. What, exactly, is...
10
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public:...
5
by: Ed Flecko | last post by:
Hi folks, I'm trying to figure out this whole RSS feed thing. I've created my .xml file to use for my feed, and my browsers "recognize" that I have an RSS feed, and you can subscribe, etc., etc....
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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.