Safely deleting a type-casted pointer

P

Pat

Hi all,

I have a really awkward situation that is causing memory leak problems.
I'm passing data to a driver, and unfortunately, the driver code is not
something I can change, and it is written in C, so it deals with the data
as a big BYTE array. Basically, the driver expects a struct, followed
immediately in memory by a big chunk of raw BYTE data.

The size of the array of BYTEs is determined by certain members of the
struct. So here is how I allocate the data:

typedef struct
{
WORD data_size;
// (other fields here)
} data_header;

...

data_header *allocate_data_block(WORD data_size)
{
BYTE *p = new BYTE[sizeof(data_header) + data_size];

if (p)
{
data_header *dh = (data_header *)p;
dh->data_size = data_size;

return dh;
}

return NULL;
}

...

void some_function_in_my_code()
{
// Allocate a data block with 20 extra bytes
data_header *my_header = allocate_data_block(20);

// Fill in the other fields in the header
data_header->x = 1;
data_header->y = 2;
data_header->z = 3;

// Fill in the BYTE data
BYTE *data = (BYTE *)(data_header) + sizeof(data_header);

data[0] = 100;
data[1] = 101;
data[2] = 102;
// ...

// Send the data to the driver
some_driver_function(data_header, sizeof(data_header) + 20);

// Delete the data
// ???
}

Everything up to the call to the driver is working fine. But how do I go
about safely deleting the allocated memory? I guess the real question
is, how does "delete" know how much to delete?

For example, if I just say "delete my_header;" will that delete only
sizeof(data_header) and leave the remaining 20 bytes as a memory leak?

Or, is it safe to cast it back to a byte array, like this:

BYTE *delete_it = (BYTE *)my_header;
delete[] delete_it;

?

Thanks,
Pat
 
V

Victor Bazarov

Pat said:
I have a really awkward situation that is causing memory leak
problems. I'm passing data to a driver, and unfortunately, the driver
code is not something I can change, and it is written in C, so it
deals with the data as a big BYTE array. Basically, the driver
expects a struct, followed immediately in memory by a big chunk of
raw BYTE data.

The size of the array of BYTEs is determined by certain members of the
struct. So here is how I allocate the data:

typedef struct
{
WORD data_size;
// (other fields here)
} data_header;

...

data_header *allocate_data_block(WORD data_size)
{
BYTE *p = new BYTE[sizeof(data_header) + data_size];

if (p)
{
data_header *dh = (data_header *)p;
dh->data_size = data_size;

return dh;
}

return NULL;
}

...

void some_function_in_my_code()
{
// Allocate a data block with 20 extra bytes
data_header *my_header = allocate_data_block(20);

// Fill in the other fields in the header
data_header->x = 1;
data_header->y = 2;
data_header->z = 3;

// Fill in the BYTE data
BYTE *data = (BYTE *)(data_header) + sizeof(data_header);

data[0] = 100;
data[1] = 101;
data[2] = 102;
// ...

// Send the data to the driver
some_driver_function(data_header, sizeof(data_header) + 20);

// Delete the data
// ???
}

Everything up to the call to the driver is working fine. But how do
I go about safely deleting the allocated memory? I guess the real
question is, how does "delete" know how much to delete?

For example, if I just say "delete my_header;" will that delete only
sizeof(data_header) and leave the remaining 20 bytes as a memory leak?

You should "delete" whatever you allocated *the same way* you allocated
it. Deleting a wrong pointer using the wrong 'detele' form has undefined
behaviour.
Or, is it safe to cast it back to a byte array, like this:

BYTE *delete_it = (BYTE *)my_header;
delete[] delete_it;

?

Most likely that's what you need to do. But since you wrapped the
allocation into a function, I'd probably wrap deallocation into
a function as well.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top