auto_ptr array
-
Thanks CPallini, 1. The compiler deduce _Ty,
_Ty=int [10]
is because of the following code?
auto_ptr<int[10]>
2. In your sample, compiler will make A. one auto_ptr object wrapps an int array on heap; or B. ten auto_ptr objects and each object wrapps an int on heap? regards, George
1. Yes. 2. A. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
1. Yes. 2. A. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThanks CPallini, I read the compile error message you posted, -------------------- warning C4156: deletion of an array expression without using the array form of 'delete' -------------------- I think your code will have memory leak potentially? Because delete other than delete[] will work on the array? regards, George
-
Thanks CPallini, I read the compile error message you posted, -------------------- warning C4156: deletion of an array expression without using the array form of 'delete' -------------------- I think your code will have memory leak potentially? Because delete other than delete[] will work on the array? regards, George
George_George wrote:
I think your code will have memory leak potentially? Because delete other than delete[] will work on the array?
The
delete
syntax will be used (insted of thedelete []
one). About potential memory leaks, from MSDN [^] The following two cases produce undefined results: using the array form of delete (delete [ ]) on an object and using the nonarray form of delete on an array. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
George_George wrote:
I think your code will have memory leak potentially? Because delete other than delete[] will work on the array?
The
delete
syntax will be used (insted of thedelete []
one). About potential memory leaks, from MSDN [^] The following two cases produce undefined results: using the array form of delete (delete [ ]) on an object and using the nonarray form of delete on an array. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeYou mean your code will cause undefined behavior? CPallini? regards, George
-
You mean your code will cause undefined behavior? CPallini? regards, George
-
Sorry led mike, What is your reply to my original question? How to make an auto_ptr array and initialize it? regards, George
-
So this way he can read the answer once again, just to be sure he read it correctly ;P
Cédric Moonen Software developer
Charting control [v1.3] -
You mean your code will cause undefined behavior? CPallini? regards, George
George_George wrote:
You mean your code will cause undefined behavior? CPallini?
Well, MSDN says it and of course I cannot object to Microsoft. See here [^]. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Tuesday, April 1, 2008 1:55 PM
-
Sorry led mike, What is your reply to my original question? How to make an auto_ptr array and initialize it? regards, George
George_George wrote:
What is your reply to my original question?
I have no answer for it. I don't understand the premise. auto_ptr should be used to implement exception safe locality and I don't understand the need to have an array of int pointers for local use. I would just put the ints on the stack and I would not use an array I would use a vector.
led mike
-
Hello everyone, I have tried to initialize an auto_ptr array, but failed. My C++ Programming Language book does not contain a sample about how to initialize an auto_ptr array. (not an auto_ptr pointing to an array, which is not legal) Any solutions?
#include <memory>
using namespace std;
int main()
{
auto_ptr<int[]> pi (new int[10]); // compile errorauto\_ptr<int> pi (new int\[10\]); // compile error return 0;
}
thanks in advance, George
As you have already discovered auto_ptr is not designed to work as an array of pointers. There are multiple solutions to your problem and they are described in the below links. http://www.codeproject.com/KB/cpp/COAP.aspx[^] http://www.gotw.ca/gotw/042.htm[^] If you are just interested finding out how auto_ptr can be used with an array then above links are suffice. but in real use, you might want to take a look at the boost smart pointer library. you can also use the shared_ptr as it is designed to work with STL containers. Ankita
-
George_George wrote:
You mean your code will cause undefined behavior? CPallini?
Well, MSDN says it and of course I cannot object to Microsoft. See here [^]. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Tuesday, April 1, 2008 1:55 PM
Sure, CPallini! About my original question, your option is we can not define an auto_ptr array and do initialization at the same time? regards, George
-
George_George wrote:
What is your reply to my original question?
I have no answer for it. I don't understand the premise. auto_ptr should be used to implement exception safe locality and I don't understand the need to have an array of int pointers for local use. I would just put the ints on the stack and I would not use an array I would use a vector.
led mike
Hi led mike, int is just used for demo purpose. You can use user defined data types, like class Foo. How to define an array of auto_ptr and initialization at the same time? regards, George
-
So this way he can read the answer once again, just to be sure he read it correctly ;P
Cédric Moonen Software developer
Charting control [v1.3]I agree, Cedric! Confirmation is good practice for the flat world. Since you are not sit next to me, or climb through the network cable. :-) regards, George
-
As you have already discovered auto_ptr is not designed to work as an array of pointers. There are multiple solutions to your problem and they are described in the below links. http://www.codeproject.com/KB/cpp/COAP.aspx[^] http://www.gotw.ca/gotw/042.htm[^] If you are just interested finding out how auto_ptr can be used with an array then above links are suffice. but in real use, you might want to take a look at the boost smart pointer library. you can also use the shared_ptr as it is designed to work with STL containers. Ankita
Hi Ankita, I have not made myself understood. My question is (say in another way), how to define an array of auto_ptr and initialization at the same time of definition? Any ideas? regards, George
-
Sure, CPallini! About my original question, your option is we can not define an auto_ptr array and do initialization at the same time? regards, George
Do you need an array of
auto_ptr
?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Do you need an array of
auto_ptr
?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeSure, CPallini. My requirement is, I need to have an array of pointers to class Goo, wrapped in class Foo as member variables. I want to make them auto_ptr array to make it exception safe. Do you think in this situation using auto_ptr array is a good idea? If you have better ideas, please feel free to let me know. :-) regards, George
-
Sure, CPallini. My requirement is, I need to have an array of pointers to class Goo, wrapped in class Foo as member variables. I want to make them auto_ptr array to make it exception safe. Do you think in this situation using auto_ptr array is a good idea? If you have better ideas, please feel free to let me know. :-) regards, George
You probably need something like the following: (Sample for a 3-items array)
auto_ptr<Foo> pi[3]={auto_ptr<Foo>(new Foo), auto_ptr<Foo>(new Foo), auto_ptr<Foo>(new Foo)};
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
You probably need something like the following: (Sample for a 3-items array)
auto_ptr<Foo> pi[3]={auto_ptr<Foo>(new Foo), auto_ptr<Foo>(new Foo), auto_ptr<Foo>(new Foo)};
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThanks CPallini, Do we have any ways to saving typing? regards, George
-
Thanks CPallini, Do we have any ways to saving typing? regards, George
Yes: employ someone and let he/she do the job for you. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Yes: employ someone and let he/she do the job for you. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThanks CPallini, I believe it is a limitation for initialization approach for auto_ptr array. :-) regards, George