For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > Need Help on free up dynamically allocated memory









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Need Help on free up dynamically allocated memory
alee

2005-11-26, 7:01 pm

Please help!
In the main(), I declared CBook * ptrBookArray[15] = {NULL};
Then, I pass it down to a function where I read in the book details and
hanging these book objects to the ptrBookArray.
in the function, I have a for loop reading in the book details and hanging
to the pointer:

for (......)
{
ptrBookArray[i] = new CBook() //here I created a book object and hang it
to a ptr
ptrBookArray[i]-> setInfo // here I assign the details of the object
}

Then back to the end of the main(), I put

delete [] ptrBookArray // to free up the memory

But I got error message:

Debug Assertion Failed
File dbgdel.cpp
Line: 52

expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

Please help me figure out what's wrong with the delete. If I take away the
delete code, I have no error message.

Thanks.




John Carson

2005-11-26, 7:01 pm

"alee" <alee@discussions.microsoft.com> wrote in message
news:24BA87A1-F9B0-4723-87E2-848223E8C3EC@microsoft.com
> Please help!
> In the main(), I declared CBook * ptrBookArray[15] = {NULL};
> Then, I pass it down to a function where I read in the book details
> and hanging these book objects to the ptrBookArray.
> in the function, I have a for loop reading in the book details and
> hanging to the pointer:
>
> for (......)
> {
> ptrBookArray[i] = new CBook() //here I created a book object and
> hang it to a ptr
> ptrBookArray[i]-> setInfo // here I assign the details of the object
> }
>
> Then back to the end of the main(), I put
>
> delete [] ptrBookArray // to free up the memory
>
> But I got error message:
>
> Debug Assertion Failed
> File dbgdel.cpp
> Line: 52
>
> expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
>
> Please help me figure out what's wrong with the delete. If I take
> away the delete code, I have no error message.
>
> Thanks.


Your delete calls have to match your new calls. You call

ptrBookArray[i] = new CBook();

for each i so you must call

delete ptrBookArray[i];

for each i.


--
John Carson


alee

2005-11-26, 7:01 pm

Thanks John!
I change the delete [] using a for loop as follows, it works.
for (.... )
{
delete ptrBookArray[i];
}

Can I delete it as an array? just like delete [] ptrBookArray. Do you mind
giving me an example of how in my case can use the delete [] ptrBookArray?



"John Carson" wrote:

> "alee" <alee@discussions.microsoft.com> wrote in message
> news:24BA87A1-F9B0-4723-87E2-848223E8C3EC@microsoft.com
>
> Your delete calls have to match your new calls. You call
>
> ptrBookArray[i] = new CBook();
>
> for each i so you must call
>
> delete ptrBookArray[i];
>
> for each i.
>
>
> --
> John Carson
>
>
>

Igor Tandetnik

2005-11-26, 7:01 pm

"alee" <alee@discussions.microsoft.com> wrote in message
news:D0B2538F-34E6-4400-9923-24C60EE79127@microsoft.com
> Thanks John!
> I change the delete [] using a for loop as follows, it works.
> for (.... )
> {
> delete ptrBookArray[i];
> }
>
> Can I delete it as an array? just like delete [] ptrBookArray.


You have not allocated it as an array, why do you feel you can delete it
as one?

> Do you
> mind giving me an example of how in my case can use the delete []
> ptrBookArray?


You can't. You could have done

CBook* ptrBookArray = new CBook[15];
....
ptrBookArray[i].setInfo()
....
delete[] ptrBookArray;


For that matter, since you want a fixed size array, why not just

CBook bookArray[15];

? No delete necessary.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


alee

2005-11-27, 3:58 am

Thanks Igor!

"Igor Tandetnik" wrote:

> "alee" <alee@discussions.microsoft.com> wrote in message
> news:D0B2538F-34E6-4400-9923-24C60EE79127@microsoft.com
>
> You have not allocated it as an array, why do you feel you can delete it
> as one?
>
>
> You can't. You could have done
>
> CBook* ptrBookArray = new CBook[15];
> ....
> ptrBookArray[i].setInfo()
> ....
> delete[] ptrBookArray;
>
>
> For that matter, since you want a fixed size array, why not just
>
> CBook bookArray[15];
>
> ? No delete necessary.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com