c++ - Multiple New Objects Have The Same Pointer Addresses -



c++ - Multiple New Objects Have The Same Pointer Addresses -

i'm relatively new c++ apologize right away if stupid question can't life of me seem figure out whats going on. essentially, come in helper function creates objects, , returns pointer each object. works okay, issue popping sometimes, pointer values identical lastly iteration of function call.

for example:

i'll see ...

0x7fff5d1d0f10 0x7fff5d1d0d80 0x7fff5d1d0d80 <- same lastly pointer 0x7fff5d1d0fe0 0x7fff5d1d0fe0 <- same lastly pointer

is undefined behaviour? very, much appreciate , help!

the pointers homecoming values of helper function (sorry bit verbose):

w5::imessage* w5::getmessage(std::ifstream& file, char limiter){ std::string result; int line_no = 0; std::string line; while (getline(file, line)){ if(line[0] == 't' or line[0] == 'e'){ imessage * message; message = nullptr; std::string user = ""; std::string reply = ""; std::string tweet = ""; if (line[0] == 't'){ int length = line.length(); int _user = line.find("t"); int _reply = line.find("@"); if(_reply != std::string::npos){ int first_space = line.find_first_of(" "); int second_space = line.find_first_of(" ", _reply); //user //std::cout << line.substr(_user+1, _reply-2) << std::endl; user = line.substr(_user+1, _reply-2); //reply // std::cout << line.substr(_reply+1, second_space-_reply) << std::endl; reply = line.substr(_reply+1, second_space-_reply); //tweet //std::cout << line.substr(second_space+1) << std::endl; tweet = line.substr(second_space+1); twitter twitter(user, tweet, reply); // std::cout << &twitter << std::endl; message = &twitter; // std::cout << message << std::endl; homecoming message; }else{ int _tweet = line.find(" "); //user //std::cout << line.substr(_user+1, _tweet) << std::endl; std::string user = line.substr(_user+1, _tweet); //tweet if(_tweet != std::string::npos){ // std::cout << line.substr(_tweet+1) << std::endl; std::string tweet = line.substr(_tweet+1); if(tweet != ""){ twitter twitter(user, tweet, ""); imessage * message; // std::cout << &twitter << std::endl; message = &twitter; // std::cout << message << std::endl; homecoming message; } } } } if(line[0] == 'e'){ int length = line.length(); int _from = line.find("from:"); int _to = line.find(" to:"); int _date = line.find(" date:"); int _body = line.find(" body:"); std::string = ""; std::string = ""; std::string date = ""; std::string body = ""; //from //std::cout << line.substr(_from+5, _to-_from-4) << std::endl; = line.substr(_from+5, _to-_from-4); //to // std::cout << line.substr(_to+4, _date-_to-3) << std::endl; = line.substr(_to+4, _date-_to-3); //date // std::cout << line.substr(_date+6, _body-_date-6) << std::endl; date = line.substr(_date+6, _body-_date-6); //body // std::cout << line.substr(_body+6) << std::endl; body = line.substr(_body+6); email email(from, to, body, date); // std::cout << &email << std::endl; message = &email; // std::cout << message << std::endl; homecoming message; } result += line + "\n"; line_no++; } } imessage *message; message = nullptr; homecoming message; }

the problems exist @ these lines:

email email(from, to, body, date); // std::cout << &email << std::endl; message = &email; // std::cout << message << std::endl; homecoming message;

for reason pointer value of &email seems same lastly iteration instead beingness new pointer value. same issue exists in other homecoming points of function.

'imessage message' abstract base of operations class.

you creating objects on stack , returning them. bad thing object on stack a) destroyed when scope in created left , b) memory you're returning isn't yours return.

you need allocate objects homecoming on heap , homecoming them pointer , clean them when done them. may wish consider returning kind of smart pointer manage lifetime of dynamic memory you're allocating.

so, instead of

email email(from, to, body, date); // std::cout << &email << std::endl; message = &email; // std::cout << message << std::endl; homecoming message;

you want this:

message = new email(from, to, body, date); homecoming message;

the reason current implementation displays same memory addresses because objects happen beingness created in same place on stack. bug you're returning pointers these stack based objects rather allocating on heap , returning object can outlive function call.

c++

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -