|
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 the delete [] 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
|
|
|
|
|
You mean your code will cause undefined behavior? CPallini?
regards,
George
|
|
|
|
|
Why do you ask people to repeat themselves all the time? Why?
led mike
|
|
|
|
|
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
|
|
|
|
|
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<foo> and initialization at the same time?
regards,
George
|
|
|
|
|
ankita patel has given you again the answer I gave you because it is the correct answer. Who cares how to do something that shouldn't be done.
I recommend you stop wasting your time with all this deep diving into subtle mechanics of something just to find the answer. Start studying Design Patterns and learn about designing maintainable flexible software. When you run into a specific situation within a real context not an imagined one, then you take the time to go deep. Until then it's a giant waste of time trying to figure out something that you will never use. Don't you think?
led mike
|
|
|
|
|
Thanks all the same led mike!
regards,
George
|
|
|
|
|
So this way he can read the answer once again, just to be sure he read it correctly
|
|
|
|
|
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
|
|
|
|
|
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 Clarke
modified 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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
Thanks CPallini,
Do we have any ways to saving typing?
regards,
George
|
|
|
|
|
Yes: employ someone and let he/she do the job for you.
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
|
|
|
|
|
Thanks CPallini,
I believe it is a limitation for initialization approach for auto_ptr array.
regards,
George
|
|
|
|
|
That applies to all arrays initialization. Actually I dont know if there is a smarter way to do it.
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
|
|
|
|
|
No CPallini,
For a normal array, we can declare/initialize in the simple way, saving type work. Right?
int array[20] = {100};
regards,
George
|
|
|
|
|
No George. You are wrong. Your code
int array[20] = {100};
initializes only the first element of the array (please make a test).
You can indeed apply the same syntax to an auto_ptr 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
|
|
|
|
|
Cool, CPallini!
I have tried, you are correct!
regards,
George
|
|
|
|
|
|
Good resource, Steve!
Unfortunately, in current project, I have to use MSVC, and it does not support shared_ptr.
regards,
George
|
|
|
|