Safely deleting a type-casted pointer

Discussion in 'C++' started by Pat, Feb 22, 2007.

  1. Pat

    Pat Guest

    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
     
    Pat, Feb 22, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Feb 22, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. praveen

    problem with a casted pointer

    praveen, Jun 29, 2007, in forum: C Programming
    Replies:
    15
    Views:
    490
    David Thompson
    Jul 22, 2007
  2. kkirtac

    Problem on using a casted void pointer

    kkirtac, Sep 23, 2007, in forum: C Programming
    Replies:
    10
    Views:
    658
    kkirtac
    Sep 24, 2007
  3. Ravi
    Replies:
    11
    Views:
    1,437
  4. Raj Pashwar

    problem with a casted pointer

    Raj Pashwar, Oct 8, 2011, in forum: C Programming
    Replies:
    4
    Views:
    347
    Morris Keesan
    Oct 10, 2011
  5. Replies:
    2
    Views:
    99
    Barry Schwarz
    Feb 14, 2014
Loading...

Share This Page