Like the following c++ program, I define a pointer of char type in the class named "test". In the function init1(), str="hello1" , what is the existing period of "hello1". Whether the string "hello1" will be invalid if the function init1() exits ? then "str" point to the invalid field in memory. When will "hello1" and "hello2" be released by OS ?
Where is "hello1" stored in memory?
Code:
#include <iostream>
using namespace std;
class test{
char *str;
public:
void init1(){
str = "hello1"; //Is "hello1" stored in stack?
}
void init2(){
str = "hello2";
}
void display(){
cout<< str << endl;
}
};
int main(){
test t1;
t1.init1();
t1.display();
t1.init2();
t1.display();
return 0;
}
Code:
cout<< "hello everyone"
When will "hello everyone" be released?
Thanks !!