Solved

Freeing Char Array memory

Posted on 2004-09-20
6
232 Views
Last Modified: 2010-04-01
I have the following code:

char *bodybuf=0; unsigned int bodysize=0;
IStream *istream;
hr = imsg->OpenProperty(PR_BODY, &IID_IStream, STGM_READ, 0, (IUnknown**)&istream);
if (hr==S_OK)
{
                  STATSTG stg = {0};
                  hr = istream->Stat(&stg,STATFLAG_NONAME);
                            if (hr==S_OK)
                              {
                        bodysize = stg.cbSize.LowPart; // won't bother checking for >2gb messages!
                        bodybuf = new char[bodysize+1];
                        ULONG red;
                        hr = istream->Read(bodybuf, bodysize, &red);
                        if (hr!=S_OK)
                                  bodysize=0;
                        else if (red<bodysize)
                                  bodysize=red;
                                  bodybuf[bodysize]=0;
                        }
                        istream->Release();
                  }

in the event that the amount red from the IStream (red) is less than the amount allocated (bodysize + 1), the string will be null terminated by the call to bodysize = red; and then bodybuf[bodysize] = 0;  

however, when I call delete [] bodybuf, will this leak memory?  If so, what is the proper way to handle this?
0
Comment
Question by:jjacksn
  • 3
  • 2
6 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 300 total points
ID: 12107399
delete operator will not evaluate the lenght of your string, or where is a terminating null character located, it will free the same memory amount as allocated in new char[bodysize+1], so don't worry about this.
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 200 total points
ID: 12107662
Though there wont be any problem deleting, as jaime said, delete just deletes the memory allocated by new

It does not look out for lengt og string or NULL charater etc etc

Hwever, why dont u allocate only that much memory as needed

THus, after u come to know that I need red aamount of mmoery, allocate that much.......that wuld not waste your memory

Amit
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12107760
Since IStream don't has a method to know how many bytes are pending, I think jjacksn implementation is ok. There is no (little) memory waste because once processed, buffer will be deleted.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12107824
I think once its kknow that REad has succeded, then red wuld contain the size. Hence that can be used to allocaate

amit
0
 
LVL 5

Author Comment

by:jjacksn
ID: 12108792
but the read needs the allocated buffer.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12110940
ohh...yes....i am sorry...didnt notice that
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now