Newbie Question: using delete with dynamic 2-d arrays 
Author Message
 Newbie Question: using delete with dynamic 2-d arrays

On Thu, 22 Apr 1999 02:13:13 GMT, Chirag Mehta

Quote:
> Hello,
>      I have been looking for an answer to this one, and have yet to find it.
> Perhaps someone out there can help.  I am trying to figure out how to
> use "delete" to free up a dynamically created two dimensional array.

> Here's the code I use to create the array:

> //Dynamically create array of length variable
> int **array = new int *[variable];
> for (int s = 0; s < variable; s++) {
>      array[s] = new int[variable];
> }

> This I understand, no problem, an array of pointers each pointing to an array.

> Is this the correct way to delete array?

> //Delete the dynamically created memory
> for(s = 0; s < variable; s++) {
>      delete [] array[s];
> }
> delete array;

> Thanks for the help.

> ----------
> Chirag Mehta
> (909) 237-3008


<Jack>

The correct way is to ask in comp.lang.c++, not in comp.lang.c.  But
the correct way is to have one delete [] for each new [], and in
reverse order.

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.



Mon, 08 Oct 2001 03:00:00 GMT  
 Newbie Question: using delete with dynamic 2-d arrays

Quote:

> Hello,
>      I have been looking for an answer to this one, and have yet to find it.
> Perhaps someone out there can help.  I am trying to figure out how to
> use "delete" to free up a dynamically created two dimensional array.

You don't.  You use free() to delete dynamically allocated memory.
delete (and delete[] and new) are C++, not C.  This different language is

either.  Lisp and Scheme are OK, but only as part of jokes.

Quote:

> Here's the code I use to create the array:

> //Dynamically create array of length variable
> int **array = new int *[variable];
> for (int s = 0; s < variable; s++) {
>      array[s] = new int[variable];
> }

> This I understand, no problem, an array of pointers each pointing to an array.

Plenty of problem.  'new' is an undefined identifier.  

Quote:

> Is this the correct way to delete array?

No.

Quote:

> //Delete the dynamically created memory
> for(s = 0; s < variable; s++) {
>      delete [] array[s];
> }
> delete array;

--




Mon, 08 Oct 2001 03:00:00 GMT  
 Newbie Question: using delete with dynamic 2-d arrays
(off topic)

Quote:

>On Thu, 22 Apr 1999 02:13:13 GMT, Chirag Mehta

[snip]
>> Is this the correct way to delete array?
[snip]
>The correct way is to ask in comp.lang.c++, not in comp.lang.c.  But
>the correct way is to have one delete [] for each new [], and in
>reverse order.

"in reverse order"?  Am I missing something here?

-dave

--
------------------------------------------------------------------------
* Remember to remove the spam blocker when replying.
* Return address is: dstarr AT xnet DOT com
------------------------------------------------------------------------



Tue, 09 Oct 2001 03:00:00 GMT  
 Newbie Question: using delete with dynamic 2-d arrays

Quote:

> (off topic)


> >On Thu, 22 Apr 1999 02:13:13 GMT, Chirag Mehta

> [snip]
> >> Is this the correct way to delete array?
> [snip]
> >The correct way is to ask in comp.lang.c++, not in comp.lang.c.  But
> >the correct way is to have one delete [] for each new [], and in
> >reverse order.

> "in reverse order"?  Am I missing something here?

<further off-topic>

If you use delete[] on an object it must have been allocated with new[],
not with new. For example:

        char *p = new char(10);
        delete [] p;    // error

        char *p = new char[10];
        delete p;       // error

        char *p = new char(10);
        delete p;       // ok

        char *p = new char[10];
        delete [] p;    // ok

(What 'new char(10)' means is left as an excercise for the reader :)

</further off-topic>

        AriL
--
Humans may send email (if absolutely necessary) to the
obvious non-spam address.



Tue, 09 Oct 2001 03:00:00 GMT  
 Newbie Question: using delete with dynamic 2-d arrays
<yet more off-topic>
Quote:


>> (off topic)




Quote:
>> >On Thu, 22 Apr 1999 02:13:13 GMT, Chirag Mehta

>> [snip]
>> >> Is this the correct way to delete array?
>> [snip]
>> >The correct way is to ask in comp.lang.c++, not in comp.lang.c.  But
>> >the correct way is to have one delete [] for each new [], and in
>> >reverse order.

>> "in reverse order"?  Am I missing something here?

><further off-topic>

>If you use delete[] on an object it must have been allocated with new[],
>not with new. For example:

[snip]

What my question actually was meant to address was that
JK's post could easily be interpreted to mean that new[] and
delete[] must be matched in some sort of LIFO way, i.e.:
  char *p = new char[x];
  char *q = new char[y];
  delete [] q;
  delete [] p;
where q /has/ to be deleted before p.

While the C++ committee has created such a monstrosity that I
might have missed this little nugget about "in reverse order" re
new and delete, I think the sentence only inadvertently gives this
impression.  My question was actually rhetorical, to give JK an
opportunity to clarify the sentence.
Sorry about the further off-topic posting, but enough people read
this newsgroup that the issue needed to be clarified.  I believe this
is especially important when it arises from a post by someone
like JK, who is normally quite clear in his posts.

In retrospect, an ambiguous rhetorical question is not really the
best way to address a (perceived) lack of clarity in a post  :)

   -dave

--
------------------------------------------------------------------------
* Remember to remove the spam blocker when replying.
* Return address is: dstarr AT xnet DOT com
------------------------------------------------------------------------



Tue, 09 Oct 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Deleting dynamic arrays!!

2. how to write a function to delete dynamic array of object

3. Deleting Dynamic Multidimensional Array

4. dynamic memory overrun when using new and delete

5. dynamic array - newbie help

6. Newbie - Dynamic Arrays??

7. Newbie Q: Dynamic Array Sizing

8. Newbie: using delete[]

9. Multi-dimensional array using dynamic memory allocation

10. Using pointers for equivalent of dynamic array of strings

11. Dynamic Array in C, using Linked Lists.

12. Multi-dimensional array using dynamic memory allocation

 

 
Powered by phpBB® Forum Software