Memory Deallocation

Memory Deallocation

Question:
I have a byte pointer pointing to a message buffer. I want to know how can I delete just the first two bytes of this message and pass on the rest of the message to the application.

If I simply pass on a shifted pointer pointing to the third byte, the application gets an error, when deleting the shifted message pointer, which says that there is a memory leak (which must be the first two bytes).

My trouble is that I am also not allowed to copy the message from third bytes on to another buffer. How should I get rid of first two bytes ?

Answer:
You have two options: either avoid dynamic allocation altogether and use stack or static memory instead, or?in case you must use dynamic memory?make sure that the function that deletes the buffer decrements the pointer by two bytes before actually deleting it. For example:

void reader(char * pmessage){ //.. process message buffer char *p = pmessage -2; delete [] p; // now fine}
Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular