user12
user12

Reputation: 55

How to safely delete a struct

I have defined this struct :

typedef struct ethernet_header
{
    UCHAR m_dest[6];
    UCHAR m_source[6];
    USHORT m_type;
}   ETHER_HDR;

Then I used it in a function like this :

void SniffPacket(u_char* p_buffer, int p_size)
{
    //Ethernet header
    ETHER_HDR *l_ethHeader = (ETHER_HDR *)p_uffer;

    //Do Something 
    //How to safely delete l_ethHeader;
}

Now I want to delete l_ethHeader before the function leaves. How to safely do it.

Upvotes: 1

Views: 234

Answers (5)

Soren
Soren

Reputation: 14688

Most coding style guide suggest that whoever allocate the memory is also responsible for deallocating it.

Your dealloocation depends on how it was allocated -- your code does not allocate anything, so I would not expect any deallocations either, but the function which called your function may need to deallocate.

If it was allocated using malloc -- then you simply free.

If your struct was allocated using "new" then you must use "delete" and make sure that your pointer is of the same type or a derived type (inheritance) as deconstructors may not fire correctly otherwise.

In your case the struct is a plain-old-data-type so it is impossible to say if it was allocated using malloc, new or if it is a stack variable -- the latter needs no deallocation at all as the return will take care of that.

Upvotes: 3

Grimm The Opiner
Grimm The Opiner

Reputation: 1806

What you have essentially done is told the compiler to treat the p_buffer pointer value as though it was a pointer to your struct, but in doing so no extra memory was allocated. The same memory is being looked at - just with a different interpretation.

If you wish to keep the header you could do either this:

ETHER_HDR l_ethHeader = *(ETHER_HDR *)p_uffer;

which would allocate a header struct on the stack that would automatically be destroyed as it went out of scope.

Or this:

ETHER_HDR* l_ethHeader = new ETHER_HDR( *(ETHER_HDR *)p_uffer );

Which would allocate a struct on the heap which would need deleting later.

All else being equal you should opt for option 1, 'always favour the stack'.

Upvotes: 4

Zac Howland
Zac Howland

Reputation: 15872

Assuming your p_buffer is already allocated when you enter SniffPacket, you do not deallocate it in the function - as your function appears to be designed to "sniff", not to manage memory.

If it is not allocated (e.g. p_buffer is NULL or uninitialized) when you enter SniffPacket, then you are invoking undefined behavior in your cast, so anything you do after that is irrelevant.

Upvotes: 2

Pedro Gandola
Pedro Gandola

Reputation: 196

It depends,

If you have allocated the memory using the new then you must use delete or delete[] (if it is an array), if you have allocated the memory using the malloc function then you must use free.

Regards

Upvotes: 0

John Dibling
John Dibling

Reputation: 101456

You don't!

You only delete what you new -- and there is no new here.

Upvotes: 7

Related Questions