[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

cleanup after dynamic memory allocation

I have the following function in a class.

      uint8_t* EncryptRSA(uint8_t* pkgbuf, int& size/*in, out*/);
      
Memory for pkgbuf is allocated outside the function, before the function call.
The second parameter has the size of pkgbuf.

Inside the function I allocate fresh memory for the return value of the function as:

uint8_t* retbuf = new BYTE[NEWSIZE];

I leave the responsibility of cleaning up the memory allocated for pkgbuf to the caller of the function EncryptRSA.

Is it better to reallocate memory to the pointer pkgbuf inside the function with a return type of void.

I am avoiding memory reallocation as there seems NO c++ way to reallocate memory. Also, if for some reason the caller would still like to use pkgbuf outside the function it will become unavailable.

Is there any better way of doing this and why? Tnx
0
Mydeen Yussouf
Asked:
Mydeen Yussouf
4 Solutions
 
HooKooDooKuCommented:
I can't see where using a return type of void is going to gain you anything.  The type is just a label.  The memory getting allocated/reallocated is the same.

While sometimes necessary, I personally do not like functions that allocate memory that require the calling function to delete the memory.  I like to follow the principle of what ever allocates the memory is responsible for cleaning it up.

So a cleaner method would be to have the calling function be responsible for allocating the memory for both the input buffer AND the output buffer.  That way, the caller can deside if they want to use stack space, heap space, or reuse a memory block over and over.

You could have the return value indicate the number of nSizeOut bytes used, and if enough memory has not been provided, return a value indicating how much memory is required.

int EncryptRSA( uint8_t* pIn, int nSizeIn, uint8_t* pOut, int nSizeOut )
0
 
Infinity08Commented:
>> Is it better to reallocate memory to the pointer pkgbuf inside the function with a return type of void.

I wouldn't do it like that, no.
And btw, you'd still have to return the resulting pointer, because the re-allocation might have changed it.


>> Also, if for some reason the caller would still like to use pkgbuf outside the function it will become unavailable.

That is indeed one of the reasons I wouldn't do it (and if you do, make sure to clearly document it, so there's no confusion).


>> Is there any better way of doing this and why?

First of all, make appropriate use of const : ie. make pkgbuf a const uint8_t*, to indicate that the function won't modify the buffer contents.

Second, it's probably better to group the buffer and its size in a struct or class, so they can be easily manipulated together. Or better yet, use a std::vector to make your life easier.

Third, you might want to output the resulting encrypted data via a parameter rather than a return value. You can then use the return value to indicate succes or failure (in some way or other).

So, something like this eg. :

        EncryptResult encryptRSA(const std::vector<uint8_t>& in, std::vector<uint8_t>& out);
0
 
sarabandeCommented:
though you couldn't/shouldn't call realloc in c++ it is wrong that you couldn't do a reallocation. though i strongly support the solution with std::vector suggested by infinity08, you also could pass the pointer pkgbuf by reference in case you don't need the input buffer anymore. then the new pointer could be returned like that:

bool encryptRSA(uint8_t *& pkgbuf, size_t & sz)
{
    ...
    uint8_t * pout = new uint8_t[outsz];
    // do encrypting
    ...
    delete [] pkgbuf;
    pkgbuf = pout;
    sz = outsz;

Open in new window


with that the caller would be responsible for pointer pkgbuf as it was before though the pointer has changed.

Sara
   
0
 
trinitrotolueneCommented:
the converse is you would clean up the memory only if there is most definitely no intended use for it outside the encryptRSA() function.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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