C ++'s valuable work experience. Let me go ....

Source: Internet
Author: User
2. Common runtime error 2.1 terminate called after throwing an instance of 'std: bad_alloc'

The following error occurs: MyProgramThis occurs only when the data volume is too large. Analyzed to confirm the previous issuesCodeFrequently use new to generate an array, and then use Delete [] to delete it. Finally, modify it to declare an array (my program only needs one read/write buffer zone ):

Terminate called after throwing an instance of 'std: bad_alloc 'What (): STD: bad_allocaborted
2.2 free (): Invalid next size (FAST)

Improper malloc generation:

* ** Glibc detected **./p_main: Free (): Invalid next size (FAST): 0x08cec800 ***

In my case, declaring a char pointer to store data does not take '0' at the end into account, so the free release error occurs:

Tmpdir = (char *) malloc (strlen (DIR) + 1 + 1); strcpy (tmpdir, DIR); If (tmpdir [strlen (tmpdir)-1]! = '/') Strcat (tmpdir ,"/");
 my summary: If malloc () is frequently used in a thread, even if it is free (), this will also lead to errors, or segment errors, otherwise, terminate called after throwing an instance of 'std: bad_alloc 'and other strange errors. I will not be able to use malloc () frequently. Stay. Frequent: malloc () is frequently called in programs. 2. When writing files, do not write strings to the files. Otherwise, it will lead you up. Oh. I will go. Regulatory 
Related Article

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.