I remember a programming error from one afternoon to the middle of the night! Free (): Invalid next size (FAST): 0xb73004e8 ***

Source: Internet
Author: User

After debugging for half a day, I found the crash caused by the delete operation. I suspect that after applying for memory for a long time, the pointer operation went wrong, causing something to be written to the memory area that was not applied, an error occurred while releasing.

However, this delete operation sometimes has problems and sometimes does not.

Later, I posted a post on the School Forum and got a reminder. It may be because of a problem with the statement for applying for memory and releasing memory, which I did not expect before.


Yes:

My original code is:

char *msg2 = new char(2+(sp->first).size()+sizeof(in_addr_t)+sizeof(in_port_t));

Release code:

Delete msg2; // The program crashes during the delete operation. Why?

Because I used the msg2 pointer to operate on memory that is not applied for by me.


The correct code should be

char *msg2 = new char[2+(sp->first).size()+sizeof(in_addr_t)+sizeof(in_port_t)];

The correct release code should be:

delete []msg2;


Note the difference between () and [] After new.

The Applied memory should be

Char * p = new char [10]; // assume that the request is for 10 bytes.
Char * p = new char (10); // apply for a byte memory and assign the value of this byte memory to 10
The difference is too big.

The release should be
Combination of new and delete
Combination of new [] and delete []


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.