By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

Safely deleting a type-casted pointer

P: n/a
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
Feb 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Pat wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.