Solved

Freeing Char Array memory

Posted on 2004-09-20
6
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  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 …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 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