0

I'm studying C++ and in all my "experiments" i'm trying to understand temporary objects (rvalues) lifetime.

My question is:

Having an object which contains a const char* pointer, what happens when i want to use a constructor which takes "const char*" as argument? Usually temporary objects get destructed automatically but what happens with pointers created in this way?

I'm not using std::string or other c++11 classes for now because I'm just focusing on understanding rvalueness.

An example below:

class MyAwesomeClass {

private:
    const char* data;

public:
    MyAwesomeClass(const char* ptr) {

        this->data = ptr;
    }

    MyAwesomeClass(MyAwesomeClass&& myAwesomeClassRVALUE) {

        this->data = myAwesomeClassRVALUE.data;
        myAwesomeClassRVALUE.data = nullptr;
    }

    ~MyAwesomeClass() {

        delete data;
    }
};

int main() {

    MyAwesomeClass s = "My Awesome Class' string data.";

    return 0;
}
12
  • 3
    you have two unrelated question. Please pick one and create a minimal reproducible example for it. Commented Feb 14, 2021 at 22:23
  • 2
    You still need to create a minimal reproducible example. You ask about something that happens in the constructor. Keep the constructor and maybe 1 more method to show how you intend to use the class. The rest of the code must go. Commented Feb 14, 2021 at 22:25
  • 1
    std::string* stringData; I'd reject a CL with that in a struct/class without very persaussive justification, none of which I can invent would apply here. Commented Feb 14, 2021 at 22:27
  • 1
    unrelated to your question: : public Object it looks like you are trying to emulate Java where all class inherit from one Object. What's up with that? Commented Feb 14, 2021 at 22:28
  • 1
    Why do you think you can implement a copy constructor as noexcept, if the std::string constructor you're using in it makes no such guarantees? Also why make stringData a pointer? This just complicates the memory management. (If you want copies to refer to the same data, use std::shared_ptr<std::string>().) Commented Feb 14, 2021 at 22:32

3 Answers 3

1

Usually temporary objects get destructed automatically but what happens with pointers created in this way?

Pointers are objects themselves. Same thing happens to temporary pointer objects as happens to all other temporary objects.

Your example deletes a pointer pointing to a string literal, so the behaviour of the program is undefined. The class would also have a broken assignement operators even if it was used correctly by passing pointer to an array allocated with new[].

As for rvalueness, the only rvalue expression in the example that I could find is nullptr.

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

Comments

0

Note this answer is based on a previous edit of the question where MyAwesomeClass was a String class.

Your String class is not really a string class as it doesn't own the underlying string data. It's more akin to std::string_view.

You have two avenues I can see you can pursue:

  1. Your class owns the underlying data. In this case the class is a wrapper around std::string and has std::stringdata member. Look up composition. No need to worry about lifetime asstd::string` is well behaved.

  2. Your class is like a "pointer/reference/view" to another string. In this case you have a const char* and maybe std::size_t size data member. You basically have a std::string_view (except for the wisdom, expertise and experience that went into designing std::string_view). Since you don't own the underlying data you can't do anything about the lifetime of the underlying data. The user of the class must make sure it doesn't end with a "YourStringView" to an expired object, just as he/she needs to make sure it doesn't end up with a reference/pointer to an expired object.

The semantics of these two scenarios are wildly different (as the difference between an object and a pointer to an object).


Anyway I wouldn't recommend you do any of this except for maybe learning reasons. std::string_view already exists so just use that. If you want the printing capabilities use the fmt library or the C++ format library (that is based on the mentioned fmt library).

Even if you decide to do this for learning purposes I highly encourage you look into these alternatives and learn from how they are doing things.


MyAwesomeClass(const char* ptr)
{
    this->data = ptr;
}

~MyAwesomeClass()
{
    delete data;
}

Oh no, no, no! No!!

Please look into RAII and rule of 0/3/5. Your class either owns the pointed object or it doesn't. If it owns it then it is responsible for creating it and deleting it. If it doesn't then it can't do either. You can't have "half of responsibilities" where you are responsible for deleting it but not for creating it.

In user code you should never need to manually manage memory. Use the rule of 0.

2 Comments

"You can't have "half of responsibilities" where you are responsible for deleting it but not for creating it." You very much can do that, and it's fine in many cases. What's not fine is the particular case of 1) implicit conversion from a pointer of unknown origin and ownership to owning that pointer (that is, when you adopt an object, there should be no question that this is what you meant to do), and 2) the specific usage of char const*, which is very likely to be a literal string and thus not owned by you.
Thank you @bolov for your references, i'm reding it. Furthermore, according to this question stackoverflow.com/questions/2001286/… one should not delete const char pointers since they are allocated in READ-ONLY area (so it's not the case of the "new" operator or malloc). This means that my destructor has a problem (even if no crashes occurred so far) and it's not using the "delete[]" statement but only "delete". Would you accept this code if this->data was created with new/malloc operators?
0

Quick answer is that your class does not own the data, but just the raw pointer. Under certain conditions you will see a problem with delete operator. Raw pointer are not great tool to ensure correct object ownership.

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.