I ran this program but I didn't get what this auto_ptr does and on which basics it shows the values?
int main(int argc,char **argv)
{
int *i= new int;
auto_ptr<int> x(i);
auto_ptr<int>y;
y=x;
count <<x.get()<<endl;
count <<y.get()<<endl;
}
This code will print a NULL address for the first auto_ptr object and some non-NULL address for the second, showing that the source object lost the reference during the assignment (=).
auto_ptr automatically releases the memory allocated by new at the end of its scope (in this case when main exits). However this example is rather stupid because the integer never receives an actual value, so I have to imagine you're seeing garbage.
This may be a bit more revealing:
int main(int argc,char **argv)
{
int *i= new int;
*i = 5;
auto_ptr<int> x(i);
auto_ptr<int>y;
y=x;
count << *(x.get()) <<endl;
count << *(y.get()) <<endl;
*x.get() = 7;
count << *(x.get()) <<endl;
count << *(y.get()) <<endl;
}
In a nutshell, auto_ptr is a templated object and when its variable goes out of scope, its destructor is called and it releases the memory acquired by it. It's a one of the simpler version of smart pointers.
auto_ptr assumes ownership over the pointer you construct it with.
ie. it will automagically destroy the object pointed to once the auto_ptr itself is destroyed.
auto_ptr<int> x(i) will make x own i
auto_ptr<int> y will make y not own anything
your y=x operation will transfer ownership of i* from x to y
so now y owns i and x owns nothing, this means that when x is destroyed nothing happens, and when y is destroyed i is deleted
For this program, auto_ptr is like a normal pointer except:
it deletes any still-pointed-too object when it goes out of scope
if you copy from one auto_ptr object to another the copied-from object will "forget" about the object it has been tracking
So, x initially takes on the value of i. Then y=x effectively asks x to forget about i (by storing a NULL sentinel value instead), and y to remember i. Then printing the pointer values returned by get will show NULL/0 for x and a non-NULL value (matching i) for y. Then as main() returns, y and x leave scope, and as y is holding the non-NULL pointer value matching i, it will delete the int object, releasing the heap memory.
auto_ptr takes over ownership of the object. The important points to note are that
It will delete the object held internally
Copying/assigning auto_ptrs transfers ownership
It will be deprecated in c++0x and replaced with unique_ptr
For these reasons it tends to be used in conjuction with RAII (Resource Acquisition is Initializtion) paradigm. For example say you allocated a variable without storing it in an auto_ptr, then you would have to manage the deletion of the memory yourself. The example below shows that this has to be done twice, once for the good path and the once for the failed path.
A* p(new A);
try {
....
}
catch (std::exception& e) {
delete p;
return;
}
delete p;
If we used auto_ptr instead it saves us having to remember to delete the allocated memory.
auto_ptr<A> p(new A);
try {
....
}
catch (std::exception& e) {
....
}
Related
I'm a bit curious about a pointer's behaviour in C++. So I have this little program to test out and unfortunately it run well.
#include <iostream>
class Test{
public:
Test(){
std::cout<<"Test Created!\n";
}
~Test(){
std::cout<<"Test Destroyed!\n";
}
};
void destroyer(Test* T){
Test* temp = T;
delete temp;
}
int main(){
Test* ptr = new Test();
destroyer(ptr);
}
And it gives in return
Test Created!
Test Destroyed!
And I draw the conclusion that when we delete a pointer, actually it just deletes the object that the pointer refers to, not the variable itself (variable pointer will automatically deleted at the end of program, same as other primitive data type). Is my thought about this true?
Yes, the delete operator only calls the destructor and frees the memory occupied by the object that the pointer points to. The pointer itself is a local variable like any other and cannot be (and does not need to be) deleted.
It may be worth noting that calling delete ptr; does not change the value of ptr, so after deleting the pointer ptr would point to memory that has been deallocated. It would be undefined behaviour to access that memory after deleting it.
delete is an operator that looks at the object pointed to by the pointer object. The pointer variable is still a local value whose lifetime is tied to the scope it is declared in.
You have 3 pointer objects, with differing lifetimes. Only ptr in main lasts to the end of the program. T and temp only exist during destroyer.
Colloquially we say "delete the pointer" when we write
delete x;
However, this isnt quite accurate. What is deleted is the object pointed to by x.
And i have conclusion that when we delete pointer, actually it just delete the object that pointer refer to not the variable itself (variable pointer will automatically deleted at the end of program, same as other primitive data type). Is my thought about this true?
Yes.
In practice the inaccuracy mentioned above is just fine because after deleting the object the pointer value cannot be used without invoking either implementation defined or undefined behavior and that also affects other pointers to the same object:
int* x = new int(42);
int* y = x;
detele x;
std::cout << x << y; // implementation defined
std::cout << *x << *y; // undefined
x = new int(42); // OK
For details see here.
int main(int argc, char *argv[])
{
auto sp = std::make_shared<int>();
auto p = sp.get();
delete p; //here
std::cout << *sp << std::endl;
return 0;
}
I was hoping that the object managed by the shared_ptr sp will be deleted by the statement commented "here" but the object remains intact and is printed in the output statement.
Is this not working because I didn't create the object managed by the shared_ptr explicitly with the new keyword (because every new statement must have a corresponding delete statement and therefore every delete statement, an earlier corresponding new keyword?)?
And does this mean that pointed objects managed by shared_ptrs, created from the make_shared function are destroyed only by the shared_ptrs themselves?
The shared_ptr doesn't know you delete the pointer you got from .get(). So when the shared_ptr itself is destroyed it will also try to delete the object - leading to a double delete which is undefined behaviour. Additionally, dereferencing a pointer to a deleted object is also UB.
delete marks the memory occupied by the object as "free to be re-used" and calls the objects destructor. After delete you are not allowed (under pain of invoking UB) to ever access the object again.
Your program is simply invalid and anything the compiler may generate is allowed.
The way to "manage" the memory associated with a shared_ptr is to use C++ scoping, often referred to RAII (Resource Acquisition Is Initialization).
For your code:
int main(int argc, char *argv[])
{
{
auto sp = std::make_shared<int>();
std::cout << *sp << std::endl;
}
// memory is now delete'd because "sp" is out-of-scope
return 0;
}
With shared_ptr things can actually be (quite a bit) more complicated than that as multiple shared_ptrs can be pointing to the same memory; only when the last one is gone is the memory deleted.
Your preference should be to use std::unique_ptr until you really need shared_ptr.
Firstly, if you do this your object will be deleted twice, and you'll crash if you're lucky.
Second, just because something has been deleted doesn't mean it's underlying representation is guaranteed to be invalid. You can have a situation where the memory is freed, but nothing has yet come around to overwrite what was there before.
On C++ primer 5 edition. chapter 12. Dynamic memory.
shared_ptr<int> p(new int(42)); // reference count is 1
int *q = p.get(); // ok: but don't use q in any way that might delete its pointer
{ // new block
// undefined: two independent shared_ptrs point to the same memory
shared_ptr<int>(q);
} // block ends, q is destroyed, and the memory to which q points is freed
int foo = *p; // undefined; the memory to which p points was freed
In this case, both p and q point to the same memory. Because they were created
independently from each other, each has a reference count of 1. When the block in
which q was defined ends, q is destroyed. Destroying q frees the memory to which q
points. That makes p into a dangling pointer, meaning that what happens when we
attempt to use p is undefined. Moreover, when p is destroyed, the pointer to that
memory will be deleted a second time.
As I see q is not a shared_ptr but a built-in pointer thus it doesn't has a reference count. Normally he should say the temporary shared_ptr object and p instead. isn't it?
Also an I think this code wouldn't cause a Undefined Behavior! Because I've tried it and runs fine. Because as I guess an expression like:
{ // new block
// undefined: two independent shared_ptrs point to the same memory
shared_ptr<int>(q); // I think this will be removed by the compiler as an optimization.
}
Thus I think there is only one shared_ptr manages that memory. So it is safe I think. But if he wrote:
{ // new block
// undefined: two independent shared_ptrs point to the same memory
shared_ptr<int> q2(q);
}
Then this is what he might mean.
Thank you!
The presented code is indeed problematic and not showing what it was intended to show, but not because of some presumed compiler optimization...
The line:
shared_ptr<int>(q);
doesn't create a new temporary shared_ptr with q being a parameter sent to the constructor of the shared_ptr! It creates a shared_ptr named q.
So it is indeed a bad code example and the fix that you propose is correct.
However as mentioned in the comments, the philosophy of: it runs thus it is not undefined behavior is wrong.
Another fix for the code in the example could be to use curly brackets {} for sending q as a parameter for the temporary shared_ptr:
int main() {
shared_ptr<int> p(new int(42)); // reference count is 1
int *q = p.get(); // ok: but don't use q in any way that might delete its pointer
{ // new block
// undefined: two independent shared_ptrs point to the same memory
shared_ptr<int> {q}; // q is now a parameter to a temporary shared_ptr
} // block ends, q is destroyed, and the memory to which q points is freed
int foo = *p; // undefined; the memory to which p points was freed
}
One last note - the suggestion of giving a name to the shared_ptr inside the block doesn't really waive compiler optimization, as long as the actual object is not used the compiler may drop its creation. So if you want to make sure compiler optimization wouldn't happen you should go for:
int main() {
shared_ptr<int> p(new int(42)); // reference count is 1
int *q = p.get(); // ok: but don't use q in any way that might delete its pointer
{ // new block
// undefined: two independent shared_ptrs point to the same memory
shared_ptr<int> ptr {q}; // q is now a parameter to a local shared_ptr
*ptr = 42; // do something with ptr
} // block ends, q is destroyed, and the memory to which q points is freed
int foo = *p; // undefined; the memory to which p points was freed
return foo; // do something with foo
}
I have some questions about pointers, scopes and pointers inside vector encapsulated in class
i have this supposed cases and examples with questions:
Example 1
the variable int y scope is inside the function and when the function finish and go away that returning reference will be died and that reference be referenced to nothing?
int& createInt() {
int y = 5;
return y;
}
case1: if i do this in main or other function:
int x = createInt();
std::cout << "x value \n";
std::cout << x << "\n";
// std::cout return 5
this mean that im saving my own copy of the value of createInt()
function that is 5 in the variable x, so is safe because int x contain their own value?
but what happen with the reference returning from the createInt() function, is there a memory leak or not because is not a pointer a will die with the scope of the function.
case 2: if i do this in main or other function:
int &x = createInt();
std::cout << "x value \n";
std::cout << x << "\n";
// std::cout return 32767
int &x is equals to the reference returning from createInt() function, that reference die when the function finish/go away so for that reason
int &x is returning a wrong value 32767 and not 5 or what are that 32767 value?
so int &x = createInt(); is evil and very bad practice because is reference to nothing.
example 2
what about this? i'm requesting allocation memory for int and initialized memory to the pointer variable...
int& createInt() {
int* y = new int(5);
return *y;
}
that pointer variable is in the stack but store a reference to the new int that is in the heap, so that new int will be alive when the scope of the function go away because is in the heap right?
so when i return the reference i'm returning the reference to that new int not the pointer variable, right? so is bad to returning the reference instead of the pointer? for what?
case1: if i do this in main or other function:
int x = createInt();
std::cout << "x value \n";
std::cout << x << "\n";
// std::cout return 5
i'm creating a copy of the new int value from createInt() in my local int x variable, so is this a memory leak because i'm creating a copy and not getting the pointer, so i can't do a delete of int x variable because is not a pointer, and also i can't delete the int *y pointer created inside createInt() function because the pointer is lost, i don't have it outside the createInt()
but whats happen if i do:
delete &x;
i will get a error:
malloc: *** error for object 0x7ffee204b8c8: pointer being freed was not allocated
because i'm deleting my int x that is not in the heap? or is trying to delete the int *y inside the createInt() function ?
case2: if i do this with the same function:
int &x = createInt2();
std::cout << "x value \n";
std::cout << x << "\n";
// std::cout return 5
my int &x is a reference of the returning by createInt()
so i can do:
delete &x;
is a memory leak here? but its so bad delete &x reference instead of the pointer int *y? maybe doing delete & i'm not have form to be sure if that is allocated memory or is stack memory, so good practice is never try to delete using &?
vectors parts:
i have a class A that contain a vector of pointers of class B, also i have a method that return a element of the vector but as reference (because i want to have it in memory to reutilize it and control when is deleted like a connection pool also i move it from used vector to notInUsevector but this is other history), and in the destructor of class A i delete all the vector elements:
Class A {
//this is singleton
public:
static A& getInstance()
{
std::call_once(m_once, []() {
instance.reset(new Database());
});
return *instance;
}
B& getFirstElement() {
auto element = connections.front();
return *element;
}
~A() {
for(auto element : myVector){
delete num;
}
}
A(A const &) = delete;
void operator=(A const &) = delete;
private:
A();
static std::unique_ptr<A> instance;
static std::once_flag m_once;
std::vector<B*> myVector;
}
so in other place/function/class etc i do:
auto element = &A::getInstance().getFirstElement();
or maybe is best or the same:
auto &element = A::getInstance().getFirstElement();
so when the Class A instance is deleted the destructor will delete all the pointers inside myVector
is this safe, there is a memory leak? it's a very bad return the reference in the getInstance() function instead of the pointer?
thanks
First of all let's make it clear, scope is "layer" between { and }. It might be a function body, statement (if, switch, for, etc.) or standalone scope. The most important consequence is that life time of objects created on stack is limited to that scope.
Take your function createInt() as example, internal int y, exists only inside of that function. The moment you reach } that memory is free.
Second thing is term memory leak. Memory leak is a situation where you have a chunk of memory (might be a single byte, might be a couple of pages) and no way to point at it. It's like a locked box without a key. It is there, it is yours memory, it won't be free unless you tell, problem is you don't know where that memory is located, you have no pointer for it.
That's being said, let's talk about your cases:
Example 1 Case 1:
You are lucky. Your function return its internal y by reference, but this returned variable is freed before you return from your function. Effectively you have a value written down on sticky note, then you throw it to the trash and then return saying that your value is on that note that is in trash. The only reason you can still read it is because no one assigned that bit of memory in the mean time and it was not overwritten. Remember, the moment your function reached } all stack alocated variables (y was allocated on stack) are destroyed. That also answer your second question about memory leak, there is no memory leak. As long as you work with stack, allocation and deallocation is done "automagically" as destructor calls are inserted at the end of the scope (usually) by compiler.
Example 1 Case 2:
The very same thing as in case 1. Your returned value is compromised because you returned stack allocated variable that is no longer valid as you return from function. Now you can observe this because between assigning and reading that value you make a function call. Depending on where original y was allocated that bit of memory might be reused during std::cout call. This become obvious as you work on the very same bit of memory cause of reference usage. Remember when you reached } of createInt(), you free that bit of memory. As an additional excersise, put a break point on lines int &x = createInt(); and int x = createInt(); then step into function and watch memory state as you leave it.
Example 2 Case 1:
Now you create (potential) memory leak. You allocate a memory on heap so it won't be destroyed as you leave function body. You also pass that exact memory address, so you also should take responsibility for freeing it and you don't call delete, so that's your memory leak. And one more problem occurs as you assign returned value to completely new stack allocated variable. Your original x from createInt() is allocated, then returned by reference but feed to assign operator to other independent variable (int x have different address than int* y). Again you can check this with help of breakpoints and debugger.
Example 2 Case 2:
Now this is almost proper use of variables returned by reference and assignment of them. You create variable x on heap, then return it's address and assign it to y. Problem being as x was created on stack you are responsible for destroying it so call to delete is neccessary to avoid memory leak. Put a break point and track memory addresses of both x and y. They are the same.
General rule of thumb is that return by reference should be used only with variables that exist outside of function, for example you can use it for class memebrs as they live inside the object or for global or static variables. For other purposes use return by value, unless you really need that very object, then return it by pointer. And while you work with pointers always be aware that someone at some point have to delete underlying variable.
I skip the part about vectors and instead point you a good tool for tracking memory leaks. It's not the best solution by any means, but for starters it will do the trick (assuming you are using Visual Studio).
https://learn.microsoft.com/pl-pl/visualstudio/debugger/finding-memory-leaks-using-the-crt-library?view=vs-2019
Remember to use breakpoints and debugger instead of printing everything to output/console. This will help you a lot. In both tracking memory and variable state, and understanding what your code really does.
I don't have the impression you have a real question at all, because you seem yo have found out all tricks, traps and good practices on your own. You seem to know about memory leaks and dangling references. Static and dynamic allocation. Most of your assumptions seem correct. Some hints though :
* references '&' and raw pointers result in exactly the same machine instructions, but they are hints to the programmers. Indeed, a method returning a reference likely means you don't have to take ownership for that reference, but without documentation there is no guarantee on the lifetime of that object. A raw pointer may represent a heap instance, or an optional argument, but it could also represent just a reference. This lack of ownership rules in C/C++ is indeed tricky.
* When dereferencing a dangling reference, the behavior is undefined. It may be working perfectly on your machine and it could burn my harddisk. Anything goes.
* When dealing with heap allocations, we nowadays prefer yo use smart pointers whenever possible. There is no mistaking in it's usage. Search for unique_ptr and shared_ptr
* Finally I recomy learning a bit of Rust. This is a programming language that was specifically designed to resolve such object lifetime/ownership issues. It is quite easy to install and run the most basic examples. Just try returning a dangling reference with Rust, you'll learn a lot, believe me.
I have been pondering an issue today and it's difficult to find the answer on google.
I'm trying to understand the STL container behaviour when dealing with pointers to both objects allocated on the heap, and on the stack.
So, start with objects, no pointers ... imagine I have ...
std::vector<int> myVec;
while(true)
{
int myInt = 5;
myVec.push_back(myInt);
myVec.pop_back();
}
My understanding is that the pop_back() method will ensure the integers contained in the vector are deleted, not just removed from the container. So if this ran and did a billion iterations, I should not expect to leak memory. Everything I insert, will be deleted. A memory check shows this behaviour.
Now consider I use a vector of pointers (to objects on the heap) ...
std::vector<int*> myVec;
while(true)
{
int * myIntP = new int(5);
myVec.push_back(myIntP);
myVec.pop_back();
}
In this case, only the pointer itself ought to be removed each time pop_back() is called, and the underlying object remains un-deleted, causing a memory leak. So after a billion iterations I have quite some significant memory being used, even though I have no entries in my vector.
Now what if I have a vector of pointers (to objects on the stack) ...
std::vector<int*> myVec;
while(true)
{
int myInt = 5;
int * myIntP = &myInt;
myVec.push_back(myIntP);
myVec.pop_back();
}
Here the pointers point to stack objects. Is their memory freed in the call to pop_back(), or not? A memory check showed me that this behaviour was no memory leaked. The small amount of memory used, indicated that this behaved like objects on the stack. However this was not expected to me, because if the pointer had been passed into me from another function, to a stack variable i.e.
void myFunc(int * myIntP)
{
std::vector<int*> myVec;
myVec.push_back(myIntP);
myVec.pop_back();
}
int main()
{
int myInt = 5;
int * myIntP = &myInt;
myFunc(myIntP);
std::cout << (*myIntP) << std::endl;
return 0;
}
Then allowing the vector to free this memory, would render my myIntP pointing to removed data. So surely this can't be correct?
Could anyone help explain?
Also is there a name for "a pointer pointing to a variable on the stack" i.e. not initialised with "new"?
Thanks
Joey
while(true)
{
int myInt = 5;
int * myIntP = &myInt;
myVec.push_back(myIntP);
myVec.pop_back();
}
You only actually have one int here, myInt with a value of 5. The loop will re-use the same one. You push a pointer to that one int into the vector and then remove it. Nothing else is happening. There isn't a memory leak because you are not allocating new ints.
STD containers do nothing different for pointers than they would for a 32/64 bit interger. As far as they care, a pointer is just another number. So, if you insert a pointer into a container, it is your responsibility to delete it.
If you make a pointer to a variable on the stack, the variable will be destructed when it goes out of scope, regardless of the pointer. And destructing the pointer (as long as you don't call delete on it) will have no effect on the variable.
So if you stop using your pointer before, no problem, if you store it longer, problem...
And if you plan on using pointers on dynamically allocated variable, you should look into smart pointers.
Here the pointers point to stack objects. Is their memory freed in the call to pop_back(), or not?
No, they are not. They are freed when they go out of scope, which happens at the }. After the }, the memory is no longer used for this variable (the stack-frame popped off) and will be reused! So if you didn't pop-off the pointer right after pushing it, your vector would contain a dangling pointer when the variable goes out of scope.
So, let's go through each of your examples:
std::vector<int> myVec;
while(true)
{
int myInt = 5;
myVec.push_back(myInt);
myVec.pop_back();
}
The push_back() method makes a copy of the argument and stores the copy internally. So, if you were storing an stack-allocated object instead of a primitive, a copy constructor would have been called. The pop_back() method does not assume anything either. It removes the copy of the item you stored (whether it was a value or a pointer) and removes it from its internal storage. If the copy stored was a stack-allocated object, the class' destructor will be called when the container manages its internal memory because the copy item will no longer be in scope.
Your second example:
std::vector<int*> myVec;
while(true)
{
int * myIntP = new int(5);
myVec.push_back(myIntP);
myVec.pop_back();
}
As you stated, the integer is allocated on the heap. Calling push_back() still stores the argument. In this case, you are not storing the value of the integer "5", the value of the pointer, an address of a memory location that contains the value of "5". Since you allocated the memory that stores the "5", you are responsible for getting that pointer and deallocate the memory. The pop_back() method does not delete the pointer for you nor returns you a copy of the pointer.
Your third example has subtle differences:
std::vector<int*> myVec;
while(true)
{
int myInt = 5;
int * myIntP = &myInt;
myVec.push_back(myIntP);
myVec.pop_back();
}
In this case, you are not allocating any memory on the heap. You assigning the address of myInt, which is a stack-allocated value, to a pointer. Stack memory lives through out the life of a process and does not deallocate on its own. However, once you leave the current scope (the while loop), the memory reused by something else. The memory is still there, but it may no longer have the value you expect.
Your last example:
void myFunc(int * myIntP)
{
std::vector<int*> myVec;
myVec.push_back(myIntP);
myVec.pop_back();
}
int main()
{
int myInt = 5;
int * myIntP = &myInt;
myFunc(myIntP);
std::cout << (*myIntP) << std::endl;
return 0;
}
You were expected the memory for myInt to be dealloated after making myFunc() was called. However, container methods do not modify the supplied values. They copy them. When myFunc() pushed the myIntP pointer, it is pushing the pointer, the address of what myIntP points to, not the value in memory of that address. You would have to dereference the pointer, using call:
myVec.push_back(*myIntP);
Note that even if you did this, containers copy the value. So, myInt is still unaffected.
You are confusing and conflating "destruction" and "deletion" -- they are NOT the same thing, but are two different concepts in C++.
Deletion can only happen with pointers -- if you try to delete a non-pointer, you'll get a compile-time error. Deletion first destroys the pointed at object and then returns the memory for it to the heap.
Destruction on the other hand can happen with anything, but is mostly only of interest with classes, where it calls the destructor. With any type that has no destructor (such as int or any raw pointer type), destruction does nothing. While you CAN destroy an object manually, you pretty much never do -- it happens automatically for you when something else happens. For example, when a local variable goes out of scope, it is destroyed.
So in your code above, what happens? Well you have a local std::vector which is destroyed when it goes out of scope. Its destructor will delete anything it allocated internally, and destroy all the elements of the vector. It will NOT however, delete any elements of the vector. When you have vector<int>, that's all there is, since nothing else was allocated, but when you have a vector<int *>, if those pointers were allocated, they'll leak. If they weren't allocated (if they point at locals), there's nothing to leak.
I think you need to learn deep the scope variable validity
exemple :
{
int myVar;//Construct myVar on the stack
}//At this point, myVar is deleted with the stack
in your last exemple, you declare myInt at the start of main and don't do anything on value in myFunc.
It's normal to don't lose myInt data. it will be erase after the return 0;