c++ - Deleteing from memory after reference has returned -
i have question deleting things memory in c++, , , not exist longer. if create queue in c++ (standard library), add 2 elements it, call front(), store in regular variable (no pointer). call pop() on queue. variable stored still exist right? happens if edits memory address pointer in queue used live? mean local variable stored earlier no longer valid since created reference?
thanks clarifications.
i'll try answer in code:
#include <queue> #include <iostream> int main() { // "q: if create queue in c++..." std::queue<int> q; // "...add 2 elements it..." q.push(42); q.push(1729); // "...then call front(), store in regular variable (no pointer)..." int = q.front(); // "...then call pop() on queue..." q.pop(); // ...the variable stored still exist right?" // a: "yes, of course! , retained value:" std::cout << << std::endl; }
moreover:
"what happens if edits memory address pointer in queue used live?"
we did not store pointer in queue (see above)! anyway, if meant "what happens if region of memory queue had popped element stored gets released or overwritten", answer stored copy of object in i
, , i
lives in different region of memory.
"does mean local variable
i
stored earlier no longer valid since created reference?"
no, still valid, because though assigned object referenced through return value of front()
, made copy of object!
Comments
Post a Comment