Solved

Freeing Char Array memory

Posted on 2004-09-20
6
236 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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