Hello all,
Please suppose I have a function that accepts a pointer as a parameter. This function can throw an exception, as it uses std::vector<>::push_back() to manage the lifecycle of this pointer. If I declare it like this:
void manage(T *ptr);
and call it like this:
manage(new T());
if it throws an exception pushing the pointe...
I have a class like this:
class Inner;
class Cont
{
public:
Cont();
virtual ~Cont();
private:
Inner* m_inner;
};
in the .cpp, the constructor creates an instance of Inner with new and the destructor deletes it. This is working pretty well.
Now I want to change this code to use auto_ptr so I write:
class Inner;
class Con...
I was going through the auto_ptr documentation on this link auto_ptr
There is something which i could not fully understand why is it done. In the interface section there are two declarations for its copy constructor
1)
auto_ptr(auto_ptr<X>&) throw ();
2)
template <class Y>
auto_ptr(auto_ptr<Y>&) throw();
What purpose i...
auto_ptr_ref documentation here says this
This is an instrumental class to allow certain conversions that allow auto_ptr objects to be passed to and returned from functions.
Can somebody explain how auto_ptr_ref helps in achieving this. I just want to understand the auto_ptr class and its internals
...
I come from a managed world and c++ automatic memory management is quite unclear to me
If I understand correctly, I encapsulate a pointer within a stack object and when auto_ptr becomes out of scope, it automatically calls delete on the pointed object?
What kind of usage should I make of it and how should I naturally avoid inherent c++...
Will auto_ptr be deprecated in incoming C++ standard?
Should unique_ptr be used for ownership transfer instead of shared_ptr?
If unique_ptr is not in the standard, then do I need to use shared_ptr instead?
...
is the following function OK:
void DoSomething(auto_ptr< … >& a)....
...
I read here about std::auto_ptr<>::operator=
Notice however that the left-hand side
object is not automatically
deallocated when it already points to
some object. You can explicitly do
this by calling member function reset
before assigning it a new value.
However, when I read the source code for header file C:\Program Fil...
#include <stdlib.h>
#include <iostream>
#include <memory>
#include "copy_of_auto_ptr.h"
#ifdef _MSC_VER
#pragma message("#include <string>")
#include <string>
// http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas
#endif
/*
case 1-4 is the requirement of the auto_ptr.
which form http://ptgmedia.pearsoncmg.com/...
Consider the following code:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
struct A
{
int a;
A(int a_):a(a_) {}
};
int main()
{
vector<auto_ptr<A> > as;
for (int i = 0; i < 10; i++)
{
auto_ptr<A> a(new A(i));
as.push_back(a);
}
for (vector<auto_ptr<A> >::itera...
Code snippet (normal pointer)
int *pi = new int;
int i = 90;
pi = &i;
int k = *pi + 10;
cout<<k<<endl;
delete pi;
[Output: 100]
Code snippet (auto pointer)
Case 1:
std::auto_ptr<int> pi(new int);
int i = 90;
pi = &i;
int k = *pi + 10; //Throws unhandled exception error at this point while debugging.
cout<<k<<endl;
//delete pi; (It...
Hi,
I'm trying to use smart pointers such as auto_ptr, shared_ptr. However, I don't know how to use it in this situation.
CvMemStorage *storage = cvCreateMemStorage();
... use the pointer ...
cvReleaseMemStorage(&storage);
I'm not sure, but I think that the storage variable is just a malloc'ed memory, not a C++ class object. Is there...
I know containers of auto pointers should not be used and can cause problems. What is the actual reason for that? Is there any other kind of "smart" pointer which is safe to use in a container?
...
I am confused with unique_ptr and rvalue move philosophy.
Let's say we have two collections:
std::vector<std::auto_ptr<int>> autoCollection;
std::vector<std::unique_ptr<int>> uniqueCollection;
Now I would expect the following to fail, as there is no telling what the algorithm is doing internally and maybe making internal pivot copies...
I just discovered the concept of an auto_ptr and am liking it! As Qt often requires a QList or QVector<(some QObject or QWidget) *>, is there any concrete reason why auto_ptr should be avoided. If I'm right, it allows you to replace this:
std::vector<MyClass*> vec;
/* add several elements to the vector and do stuff with them */
for(size...
What I want to do is this:
#include <memory>
class autostr : public std::auto_ptr<char>
{
public:
autostr(char *a) : std::auto_ptr<char>(a) {}
autostr(autostr &a) : std::auto_ptr<char>(a) {}
// define a bunch of string utils here...
};
autostr test(char a)
{
return autostr(new char(a));
}
void main(int args, char **ar...
If i use auto_ptr to hold a pointer to a dynamically allocated array, when the auto_ptr gets killed it will use a plain delete operation and not delete[] thus not deleting my allocated array.
How can i (properly) use auto_ptr on dynamically allocated arrays?
If this is not possible, is there another smart pointer alternative for dynami...
auto_ptr on wikipedia said that "an auto_ptr containing an STL container may be used to prevent further modification of the container.". It used the following example:
auto_ptr<vector<ContainedType> > open_vec(new vector<ContainedType>);
open_vec->push_back(5);
open_vec->push_back(3);
// Transfers control, but now the vector cannot be...
Hey S.O. Guys
I am currently trying to write a basic wrapper for the cml (http://www.cmldev.net/) math library for a project i am working on.
I have a wrapper for the cml vector class which has one private member
#ifndef VECTOR3_H_
#define VECTOR3_H_
#include "cml\cml.h"
#include <memory>
namespace Math
{
template<typename T>
...
If I have a class
template <typename T>
struct C {
...
private:
auto_ptr<T> ptr;
};
How do I define the copy constructor for C:
It cannot be
template <typename T>
C<T>::C(const C& other)
because I want to if I copy the auto_ptr from other, I have changed other by removing
ownership.
Is it legal to define a copy constructor ...