473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Safely deleting a type-casted pointer

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
1 3090
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rishi | last post by:
Looking for tips on how to improve performance on deleting records. In our database we do dataloads daily that require us to purge millions of records a day so any improvement in speed would be...
3
by: kris.dorey | last post by:
Hi, Ive got the following code which seems ok but when the user runs the function for a second time I get an error message stating that the mdb is in use by another process. There is still an...
6
by: Neo The One | last post by:
On my development machine, I often delete all folders/files under C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files. That has not caused me any trouble til now. So now we have...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
46
by: DP | last post by:
hi, i've got a form, with a subform in it. i've got a delete button in the subform. the code i;ve got is; Private Sub cmdDeleteRecord_Click() msg = "Are you sure you want to delete this...
19
by: MaXX | last post by:
Hi, I hope I'm not OT. I have the following issue: I want to delete a record from my db with a php script. Let's say I'm auth'd and I want to delete the record id 440. With a simple form (get...
14
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
2
by: azmiza | last post by:
Hi everybody, I need your help. I want to view my sql database and its work very well which is display in my web browser but once I want to press button yes, its not working, I check the...
3
Odisey
by: Odisey | last post by:
I am not sure if this is a MySQL syntax error or a DB issue yet. I am getting the error from the code: Page Error! This page has been accessed in error. The GET is populated in the browser...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.