7 Matching Annotations
  1. Dec 2022
    1. std::string_view can handle both NTBS and std::string, e.g.

      As we have both NTBS and C++ Strings, writing portable code to handle both can be challenging. For example, a C++ string cannot be passed to const char * (the .c_str() member function must be used).

    2. to significantly improve performance, when the literal string has fewer characters than an implementation-defined threshold, the runtime implementation stores the literal string within the stack space allocated for the handle, rather than allocation memory from the hea

      called SSO: actually small strings are not allocated dynamically (i.e. on the heap), instead the literal string is stored at runtime in the stack space allocated for the handle

    3. std::string implements “deep-copy” semantics; when the copies are created, new memory is allocated, and the contents are copied (e.g. strcpy)

      a "deep copy" is when the whole object in the heap is also copied

    4. std::string cannot be used directly where a const char* is required. Many embedded libraries favour C-based APIs, requiring support for converting a std::string to const char*.

      this is why it is better to get used to const char*'s first

    5. Unlike C, C++ does not allow the dangerous code of having a non-const pointer pointing at constant memory,

      which is why string literals must be const char* instead of char* - because the string literal memory is stored in flash - which is non-mutable memory (unlike the stack)

    6. The first output message will display 17, the number of characters in the string (including the null character). The second output will display the sizeof a pointer

      In C: char message[] ="abc"; and const char *msg_ptr = "abc"; are represented differently in memory; in the first case the literal string is held on the stack (when it is copied in at runtime) and for (2) only the address of the string is held on the sstack