Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Freeing Char Array memory

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
jjacksn
Asked:
jjacksn
  • 3
  • 2
2 Solutions
 
Jaime OlivaresCommented:
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
 
Sys_ProgCommented:
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
 
Jaime OlivaresCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

amit
0
 
jjacksnAuthor Commented:
but the read needs the allocated buffer.
0
 
Sys_ProgCommented:
ohh...yes....i am sorry...didnt notice that
0

Featured Post

Technology Partners: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now