开发者

Heap corruption issues

开发者 https://www.devze.com 2022-12-22 06:31 出处:网络
Inside my template function I have the following code: TypeName myFunction() { TypeName result; void * storage = malloc( sizeof( TypeName ) );

Inside my template function I have the following code:

TypeName myFunction()
{

    TypeName result;
    void * storage = malloc( sizeof( TypeName ) );

    /*Magic code that stores a value in the space pointed to by storage*/

    result = *(TypeName *)storage;

    free( storage );
    return result;
}

This causes an "HEAP CORRUPTION DETECTED" error.If I don't call the free() function, the error doesn'开发者_如何学运维t occur, but I am afraid that I am creating a memory leak.what would be the proper way to return the value of "storage" and then deallocate the memory?


You don't need to allocate storage, You could probably pass your result variable into a function that does your magic stuff. Something like this.

void magic(void *buffer)
{
  // magic stuff 
}

TypeName foo()
{
   TypeName result;
   magic(&result);
   return result;
}

Or of course you could have your TypeName structure set up as bit-fields or whatever your magic code manipulate...


don't call it like this:

TypeName result;
void * storage = malloc( 4 );

you should call it

TypeName result;
void * storage = malloc( sizeof(TypeName) );

anyway code looks strange :)


What about:

TypeName myFunction() {
    TypeName result;
    void* storage = &result;

    /*Magic code that stores a value in the space pointed to by storage*/

    return result;
}

Here, all your variables will be stored on the stack so you shouldn't encounter heap-related problems (depending on what exactly your "magic" code does).

Is there a reason why you have your storage array separate from result? If the results will simply be copied into result, it would make more sense (IMHO) to only use one object (and either keep a void* pointer to it or type-cast &result as needed).

If there is a reason to use a separate storage and result, you will probably get better milage using TypeName storage = new TypeName and delete instead of malloc(4) and free.


I think your confusion lies in this line:

 void * storage = malloc( 4 );

It looks like you're trying to allocate space for a 4-byte pointer, but that's not what you need to do. Let's break the line into two steps:

void * storage;          // This allocates 4 bytes for a variable of type "pointer to void"
storage = malloc( 4 );   // This allocates 4 _more_ bytes and sets "storage" to their address.

I'm assuming that the "magic" code copies data from a variable of type Typename to the memory that was allocated for storage with this effect:

memcpy(storage, data_from_a_Typename_variable, sizeof(Typename));

So if sizeof(Typename) is larger than the 4 bytes that were allocated to storage you'll see the heap-corruption error.

As the other answers indicate, what you need to do is allocate enough space for a Typename variable, like this:

void * storage = malloc(sizeof(Typename));

But, as Liz Albin suggested, you've already allocated space for a Typename in result so it's simpler to pass &result or (void *) &result to the magic function.


Why did you malloc with 4 bytes and yet casted to the type name of TypeName? That definitely looks odd!

The other answers are hinting you what it is...!

Hope this helps, Best regards, Tom.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号