2

Assuming we have some class defined which has a pointer to a char*, something like this

class MyClass
{
public:
   static MyClass* create(const char* str = "Some str")
   {
       MyClass* ptr = new MyClass();
       ptr->mStr = str; // the string is not deep copied.
       return ptr; //EDIT
   }

   const char* getStr()
   {
       return mStr;
   }

private:
   const char* mStr;
}

Somewere in the code the object is created:

MyClass* cls = MyClass::create("Some str2");

Somewere else in the code after other functions have executed i have this:

const char* str = cls->getStr();
// str == "Some str2"

At this point the str has a valid pointer, either the default "Some str", or whatever the caller passed in function create.

Can anyone explain me why the memory of str was not altered? From what I know, the string was created on the stack and the memory was freed when the execution left the function, although the function is static, could this be the reason?

2
  • 2
    Btw that string is a const literal. All you're doing is throwing its address around. Commented Nov 13, 2014 at 16:10
  • 6
    "the string was created on the stack" No, string literals are part of the constant memory of your executable, and you are only receiving pointers to it. So nothing is actually allocated here besides your MyClass class object, but this one holds a pointer to a constant memory. (And thus you should not delete mStr, by the way, if you wanted to do that...) Commented Nov 13, 2014 at 16:12

2 Answers 2

2

Your code fails to return the MyClass dynamic instance:

 static MyClass* create(const char* str = "Some str")
 {
   MyClass* ptr = new MyClass();
   ptr->mStr = str;
   // return is missing here <------------
 }

So your code right now exhibits undefined behavior, since not returning anything from a function declared to return something results in UB.

So the obvious fix is just to state:

return ptr;

at the end of the function.

Also, why are you resorting to char*? Why not use std::string?

Sign up to request clarification or add additional context in comments.

3 Comments

I usually use std::string for storing variables like this, i tried to explain to my colleague why is wrong to store a string only by it's pointer which was not allocated explicitly.
Your colleague is an "overthinker". Let's say your colleague is right -- would you want to maintain such a program with pointers all over the place instead of simple string variables? Is he willing to take the heat if and when the program crashes due to pointer mismanagement?
i wanted to understand why the address of pointer was not altered after some time. And from what i learned now, the strings are stored in the binary of the program loaded in memory. Knowing that no one will call the function other then with "string literal" variable, the program is pretty safe.
1

String literals such as "Some str2" aren't stored on the stack, but mostly in some kind of read-only memory. Pointers to them are stored on the stack, though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.