Newbie: using delete[] 
Author Message
 Newbie: using delete[]

I'm running a dialog-based app and using pointers from an array to
assign to new objects at run time as necessary.  My problem is in
deleting the objects.  I have in appDlg.h:

        void* HptrR[10000];

and in the .cpp I assign some of them in a loop, using:

        HptrR[n] = new Hlin(scr, scr.GetLength(), n);

and recast HptrR as apprpriate.  Everything works fine, but I get a
compiler warning:

C4154: deletion of an array expression; conversion to pointer supplied

when I write:

        delete[] HptrR;

Does Hlin actually get deleted?  How can I get past this warning?
Thanks in advance.

Max



Sun, 13 Nov 2005 03:38:03 GMT  
 Newbie: using delete[]


Fri, 19 Jun 1992 00:00:00 GMT  
 Newbie: using delete[]


Sun, 13 Nov 2005 03:41:24 GMT  
 Newbie: using delete[]


Fri, 19 Jun 1992 00:00:00 GMT  
 Newbie: using delete[]

Quote:

> HptrR[n] = new Hlin(scr, scr.GetLength(), n);

> delete[] HptrR;

> Does Hlin actually get deleted?  How can I get past this warning?
> Thanks in advance.

You have undefined behavior here.   You have several problems.
The primary one is that you must give (with only scant exception)
delete the same type and value you got from new.

HptrR is NOT a pointer with a value that new returned.   It is an ARRAY
of pointers.   You will have to delete them individually.

Further, you can't just cast to void* and delete the void*.   That is, even
the following:
        delete HptrR[n];
is invalid since you allocated an Hlin but you are freeing something of type
void*.

What are you trying to do?  Why is Hptr not an array of Hlin*?



Sun, 13 Nov 2005 03:48:28 GMT  
 Newbie: using delete[]
Should you not use the same logical used for creating the
object? Like this:

for(int a = 0; a < 1000; a++)
  delete(HptrR[a]);

I really don't know what you erase using delete[], but I
don't think you delete all itens in the array. Isn't delete
[] used when you use new to alloc an array? This is not
your case, because you have an array of pointers.

Quote:
>-----Original Message-----
>I'm running a dialog-based app and using pointers from an
array to
>assign to new objects at run time as necessary.  My
problem is in
>deleting the objects.  I have in appDlg.h:

>    void* HptrR[10000];

>and in the .cpp I assign some of them in a loop, using:

>    HptrR[n] = new Hlin(scr, scr.GetLength(), n);

>and recast HptrR as apprpriate.  Everything works fine,
but I get a
>compiler warning:

>C4154: deletion of an array expression; conversion to
pointer supplied

>when I write:

>    delete[] HptrR;

>Does Hlin actually get deleted?  How can I get past this
warning?
>Thanks in advance.

>Max
>.



Sun, 13 Nov 2005 03:59:39 GMT  
 Newbie: using delete[]


Quote:
> Should you not use the same logical used for creating the
> object? Like this:

> for(int a = 0; a < 1000; a++)
>   delete(HptrR[a]);

Closer, but still wrong.  HptrR[a] has type void*.
Not a legal value to pass to delete.

(The parens around delete's operand are spurious by the way).



Sun, 13 Nov 2005 04:25:52 GMT  
 Newbie: using delete[]
You're right.
This code can be compiled, but crashes at runtime.
In my opinion, the parens in the delete operand are not wrong - there's
no rule defined about this, or there is? When I develop a program, I
costume to use parens in new, delete and return's operators. Is this
wrong (is not ANSI compliant)?
Thanks,
   Ravi

Quote:


>>Should you not use the same logical used for creating the
>>object? Like this:

>>for(int a = 0; a < 1000; a++)
>>  delete(HptrR[a]);

> Closer, but still wrong.  HptrR[a] has type void*.
> Not a legal value to pass to delete.

> (The parens around delete's operand are spurious by the way).

--
Ravi Ambros Wallau


Remove the "nospam." from the last line and you'll have my E-Mail.



Sun, 13 Nov 2005 10:21:12 GMT  
 Newbie: using delete[]
Not wrong, simply unnecessary.

It disguises the fact that new, delete, return (and throw) are operators,
not functions, but from a correctness standpoint, it's equivalent to
writing:

int i = (5);

The parens add nothing.

-cd

Quote:

> You're right.
> This code can be compiled, but crashes at runtime.
> In my opinion, the parens in the delete operand are not wrong -
> there's
> no rule defined about this, or there is? When I develop a program, I
> costume to use parens in new, delete and return's operators. Is this
> wrong (is not ANSI compliant)?
> Thanks,
>    Ravi




>>> Should you not use the same logical used for creating the
>>> object? Like this:

>>> for(int a = 0; a < 1000; a++)
>>>  delete(HptrR[a]);

>> Closer, but still wrong.  HptrR[a] has type void*.
>> Not a legal value to pass to delete.

>> (The parens around delete's operand are spurious by the way).



Sun, 13 Nov 2005 10:38:46 GMT  
 Newbie: using delete[]
This is a good reason for not using the parens.
Thanks...

Quote:
>-----Original Message-----
>Not wrong, simply unnecessary.

>It disguises the fact that new, delete, return (and

throw) are operators,
Quote:
>not functions, but from a correctness standpoint, it's
equivalent to
>writing:

>int i = (5);

>The parens add nothing.

>-cd


>> You're right.
>> This code can be compiled, but crashes at runtime.
>> In my opinion, the parens in the delete operand are not
wrong -
>> there's
>> no rule defined about this, or there is? When I develop
a program, I
>> costume to use parens in new, delete and return's
operators. Is this
>> wrong (is not ANSI compliant)?
>> Thanks,
>>    Ravi



message

>>>> Should you not use the same logical used for creating
the
>>>> object? Like this:

>>>> for(int a = 0; a < 1000; a++)
>>>>  delete(HptrR[a]);

>>> Closer, but still wrong.  HptrR[a] has type void*.
>>> Not a legal value to pass to delete.

>>> (The parens around delete's operand are spurious by
the way).

>.



Sun, 13 Nov 2005 19:39:55 GMT  
 Newbie: using delete[]


Quote:
> In my opinion, the parens in the delete operand are not wrong - there's
> no rule defined about this, or there is? When I develop a program, I
> costume to use parens in new, delete and return's operators. Is this
> wrong (is not ANSI compliant)?

Nope, it's not wrong, just superfluous.


Mon, 14 Nov 2005 00:22:50 GMT  
 Newbie: using delete[]


Quote:
> It disguises the fact that new, delete, return (and throw) are
operators,
> not functions

return is a statement, not an operator. It does not form expressions.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Mon, 14 Nov 2005 03:14:01 GMT  
 Newbie: using delete[]

Quote:



>> It disguises the fact that new, delete, return (and throw) are
>> operators, not functions

> return is a statement, not an operator. It does not form expressions.

ya got me :)

-cd



Mon, 14 Nov 2005 04:00:50 GMT  
 Newbie: using delete[]
My fault. I told it was an operator first
;-)

Quote:



>>It disguises the fact that new, delete, return (and throw) are

> operators,

>>not functions

> return is a statement, not an operator. It does not form expressions.

--
Ravi Ambros Wallau


Remove the "nospam." from the last line and you'll have my E-Mail.



Tue, 15 Nov 2005 07:26:20 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Newbie Question: using delete with dynamic 2-d arrays

2. newbie question on deleting a record

3. ASP.NET using C# - Delete all files with same extension with one call

4. How to delete a picture in Excel using C#

5. deleting a file using c

6. How to delete a file by using c command

7. HELP : Deleting bytes from binary file, using FILE *

8. How to delete a file using Visual C?

9. deleting files using wildcards?

10. Add/Delete while using Bulk fetch

11. Insert, Update, Delete of rows using OLE DB ATL Consumer Templates

12. ODBC problems using Delete()....

 

 
Powered by phpBB® Forum Software