Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Beginner
Apr 14, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Memory Deallocation

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 ?

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
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date