Avoid memory leaks during mutation of c-lines

For training purposes, I use cstrings in some test programs. I would like to shorten the strings with a placeholder such as "...".

That is, it "Quite a long string"will become "Quite a lo..."if my maximum length is set to 13. In addition, I do not want to destroy the original line - therefore, the shortened line should be a copy.

Below is the (static) method that I am encountering. My question is: If the class allocating memory for my shortened string is also responsible for freeing it? Now I have to save the returned string in a separate “user class” and defer the freeing of memory for this user class.

const char* TextHelper::shortenWithPlaceholder(const char* text, size_t newSize) {
    char* shortened = new char[newSize+1];

    if (newSize <= 3) {
        strncpy_s(shortened, newSize+1, ".", newSize);
    }
    else {
        strncpy_s(shortened, newSize+1, text, newSize-3);
        strncat_s(shortened, newSize+1, "...", 3);  
    }
    return shortened;
}
+3
8

char []. , sprintf(), , . , .

+6

, ++, std::string .

- , char*. . :

std::string TextHelper::shortenWithPlaceholder(const std::string& text,
                                               size_t newSize) {
    return text.substr(0, newSize-3) + "...";
}

C cstr():

some_c_function(shortenWithPlaceholder("abcde", 4).c_str());

!

, ++ , C. ++ .

+5

, . , .

, , ?

.

const char* TextHelper::shortenWithPlaceholder(const char* text, 
                                               size_t textSize, 
                                               char* short_text, 
                                               size_t shortSize)

short_text = buffer shortSize = . const char*, short_text, ( NULL, shortSize ).

+2

, std::string, , .

C , , ,

char * strncpy ( char * destination, const char * source, size_t num );

, :

const char* TextHelper::shortenWithPlaceholder(
    char * destination, 
    const char * source, 
    size_t newSize);

- , , . , new[] , , delete[], free delete, . .

- , :

char buffer[13];
printf("%s", TextHelper::shortenWithPlaceholder(buffer, source, 12));
+2

, , . , .

class string_wrapper
{
    char *p;

public:
    string_wrapper(char *_p) : p(_p) { }
    ~string_wrapper() { delete[] p; }

    const char *c_str() { return p; }

    // also copy ctor, assignment
};

// function declaration
string_wrapper TextHelper::shortenWithPlaceholder(const char* text, size_t newSize)
{
    // allocate string buffer 'p' somehow...

    return string_wrapper(p);
}

// caller
string_wrapper shortened = TextHelper::shortenWithPlaceholder("Something too long", 5);

std::cout << shortened.c_str();

std::string.

+1

, .

, , , , .

, , - shortened , .

0

: , . , . .

++ , 0/NULL ( , ), , . : , . - . , , , / ( ).

, ? , - , , ( ), .

std::auto_ptr Boost::shared_ptr. char *.

, , , TextHelper. ctor:

TextHelper(const char* input) : input_(input), copy(0) { copy = new char[sizeof(input)/sizeof(char)]; //mess with later }
~TextHelper() { delete copy; }
0

, : a) TextHelper c . . b) TextHelper .

. b) : TextHelper , , . , , , TextHelper , . , . b), , , std::string:: c_str(). TextHelper , , , , a). , , , TextHelper.

0
source

Source: https://habr.com/ru/post/1713594/


All Articles