Save pointer? Or a copy?

Source: Internet
Author: User
Test a new version today Code A bug is found because a class written by a colleague needs to use pushparam () before calling the execute () method on its instance () the method is to "Press" the parameter of the type const char * to this object. Class Foo {
Public :
Void Pushparam ( Const Char * PARAM );
Bool Execute ();
}; In terms of class behavior, I naturally think that pushparam () will save a copy of the string, because the common STL Algorithm Or containers do this, and it is safer and more important, no annotations explicitly require that "the specified string pointer must be valid for a long time (not before the foo object is destroyed)". In fact, for efficiency, the implementation of the foo class only saves the string pointer, not a copy. It assumes that this parameter is valid for a long time, the new Code pushes a temporary String object here, and the problem arises ...... in fact, there is no such problem. The key to the problem is:
    1. Sound comments are necessary
    2. Code security is superior to (so-called) Efficiency in most cases. The so-called "so-called" is used because of the overhead of saving a copy of a string in the speed and space of today's computers, it is really minimal.
    3. According to Delphi, if C ++ has a string with reference count, the efficiency and security are both considered. C ++ is quite convenient to implement string reference counting. The difference is that Delphi hides all this behind the compiler.

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.