String literals inside functions: automatic variables or heap allocated?

Are string literals automatic variables used inside functions? Or are they allocated to a heap that we must free manually?

I have a situation like the code below, in which I assign a string literal to a private field of the class (marked as ONE in the code) and extract it much later in my program and use it (marked as TWO). Am I assigning a variable on the stack to a field in ONE? Can the code refer to a dangling pointer that worked in this case because the program was small enough?

I compiled and ran it, it worked fine, but I have a strange crash in my real program, where I assign string literals for class fields like this, and I suspect I mentioned above.

#include <iostream>

using namespace std;

class MemoryLeak
{
private:
    char *s;
public:
    MemoryLeak() {}

    void store()
    {
        s = "Storing a string"; // ONE
    }

    char *retrieve()
    {
        return s;
    }
};

int main()
{
    MemoryLeak *obj = new MemoryLeak();
    obj->store();
    cout << obj->retrieve() << endl; // TWO
    delete obj;
    return 0;
}

Should I declare the variable "s" as a char array instead of a pointer? I plan on using std :: string, but I'm just curious.

Any pointers or help, as always, are greatly appreciated :) Thank you.

+3
source share
5 answers

() , () . , , . , ( ) .

+8

undefined , , (ISO ++: 2.13.4/2). char* C, , .

, const char *.

, s , std::string.

+6

.

. , , delete'd. :

if (obj != NULL) delete obj;

:

if (obj != NULL)
{
    delete obj;
    obj = NULL;
}

++ :)

+1

, , 0-?

0

.
:

    /*
     * Should initialize s to NULL or a valid string in constructor */
        MemoryLeak()
        {
            store();
        }

        void store()
        {
            // This does not need to be freed because it is a string literal
            // generated by the compiler.
            s = "Storing a string"; // ONE

            // Note this is allowed for backward compatibility but the string is
            // really stored as a const char* and thus unmodifiable. If somebody
            // retrieves this C-String and tries to change any of the contents the
            // code could potentially crash as this is UNDEFINED Behavior.

            // The following does need to be free'd.
            // But given the type of s is char* this is more correct.
            s = strdup("Storing a string");

            // This makes a copy of the string on the heap.
            // Because you allocated the memory it is modifiable by anybody
            // retrieving it but you also need to explicitly de-allocate it
            // with free()
        }

, C-Strings. ++ std::string. ++ std::string . . , . ( i.e. ). C-String, .. ( ).

, .
. -.
.

std::auto_ptr<MemoryLeak> obj(new MemoryLeak());

obj->store();
std::cout << obj->retrieve() << std::endl; // TWO

// No need to delete When object goes out of scope it auto deletes the memory.
0

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


All Articles