Let me start by saying that I have, in fact, searched for this question in stackoverflow, but I have not found an answer to this exact question, so if this is a duplicate, feel free to just link me the question.
Also, I apologize for my novice, I'm just starting out with the c++ language.
I'm studying pointers, and in order to illustrate them, we can use this common example...
int main()
{
// Assign value to string variable
string foo = "hello";
// Assign a pointer which is the address of foo
string* bar = &foo;
// Modify the value of the pointer
*bar = "world";
// Print the value stored at bar's address,
// which is the same address of foo
cout << *bar << endl; // world
// Print the value of foo, which should be the same
// as bar, because we modified the value at the same address
cout << foo << endl; // world
}
And this completely makes sense. However, let's consider this (based on the number of questions that ask about it, seemingly counterintuitive) example.
int main()
{
// Init a string pointer
string* bar;
// Do the same type of assignment we did in the previous example
*bar = "world"; // BEHAVIOR IS UNDEFINED
// Novice intuition tells us that the output would be "world"
// based on the previous example.
cout << *bar << endl;
}
Based on the other answers I've read, this is actually undefined behavior. Reason being because... and I'll just quote this answer... "The value of that storage location is a pointer. Since you did not initialize it to a valid pointer, its value is either a valid pointer or an invalid pointer; which is it? That's up to your compiler to decide. It could always be invalid. It could always be valid. Or it could be left up to chance." by Eric Lippert
And, ok, fair enough, that seems like a reasonable explanation as to why this is the way it is...
But to me this second example, as evidenced by my - and many others - intuition to gravitate towards it, seems like a very basic and common practice. I can see many situations where, to make use of pointers, I would want to initialize a pointer to some sort of "blank" or "null" value, and then make use of it later by giving it a value.
Or... am I completely misunderstanding the purpose of pointers? Does my second example not make any sense whatsoever? Is there a different way of approaching what I'm trying to achieve that I'm missing? Or is the first example the only way that we deal with pointers in this way; we must start with a variable and then init our pointer.
Please correct me if my terminology is incorrect. Again, I'm a beginner.
But ok so, I looked into nullptr
and initializing my pointer like string* foo = nullptr
but that seemed to lead to a segmentation fault
error, pptaszni's comment seems to work appropriately.
int main()
{
std::string* bar = nullptr;
bar = new std::string("world");
cout << *bar << endl;
}
This question is different, and should be reopened, because I am looking for the common practice in dealing with a seemingly common situation. All of these questions just describe "that there is undefined behavior" and I have already acknowledged that I understand that that is the case. My question is: "How do I do the thing that I'm trying to do, given that I can't do it the way I illustrated it in the second example"