Solved

Freeing Char Array memory

Posted on 2004-09-20
6
233 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  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 shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

910 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

20 Experts available now in Live!

Get 1:1 Help Now