String Management char Array, get rid of new delete

Source: Internet
Author: User

New company, maintain an old code:

A memory out-of-bounds issue:

c = * (stext+nleng+1//  Read whatever you * (stext+nleng+10//  cross two characters, write bad memory 


This will cause, under normal conditions without pressure to run without error, not core dump.

At the time of the high stress test (fork architecture), some processes will error malloc (): Memory corruption, and no core file generation.

This can only be done by commenting on the code.

And by the way, a memory leak problem was found:

Char *newchar[len+1//  no delete behind

These styles are clearly the C + + code written by the developer.

Try using STL to manage allocation release issues.

1. Std::array<char, Num>, there is data () operation, but array is a template, num compiler resolution, I need to support runtime resolution.

2. Vector and string, this two all meet the requirements, vector changed memory, the inside of the count can not be added, it is not used, pollution code

The C_STR () string has to be const_cast, but there is no other choice.

String Management char Array, get rid of new 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.