auto_ptr array

This is a discussion on auto_ptr array within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I have tried to initialize an auto_ptr array, but failed. My C++ Programming Language book does not contain ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    auto_ptr array

    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?

    Code:
    #include <memory>
    
    using namespace std;
    
    int main()
    {
    	auto_ptr<int[]> pi (new int[10]); // compile error
    	
    	auto_ptr<int> pi (new int[10]); // compile error
    	
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm only sort of guessing, but wouldn't it be:
    Code:
    auto_ptr<int> pi[10];
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    But how to initialize it with an int array?

    Quote Originally Posted by matsp View Post
    I'm only sort of guessing, but wouldn't it be:
    Code:
    auto_ptr<int> pi[10];
    --
    Mats

    regards,
    George

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can't.
    auto_ptr's destructor does:
    Code:
    delete ptr;
    but to delete an array you would need to do:
    Code:
    delete [] ptr;
    That's why I wrote my own AutoPtr class which can take a custom "deletor" object. Boost has many other types of auto pointer classes which might do what you need.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Alternatively, you can use a vector inside the auto_ptr for an array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi cpjust,


    What is your answer to my question? My question is how to define an auto_ptr array with initialization at the same time.

    Seems your answer is we can not make an auto_ptr point to an array -- I already understand it. This is not my question.

    Quote Originally Posted by cpjust View Post
    You can't.
    auto_ptr's destructor does:
    Code:
    delete ptr;
    but to delete an array you would need to do:
    Code:
    delete [] ptr;
    That's why I wrote my own AutoPtr class which can take a custom "deletor" object. Boost has many other types of auto pointer classes which might do what you need.

    Hi Elysia,


    Vector does its own memory management and it is ensured when the vector is out-of scope, all elements' destructor is called. Is there any special benefits we can get if we use auto_ptr to make an additional wrapper, like auto_ptr<vector<int>>?

    Quote Originally Posted by Elysia View Post
    Alternatively, you can use a vector inside the auto_ptr for an array.

    regards,
    George

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Of course, just as for anything else. If you need a vector (or array) that needs to live beyond the end of the function. That's what smart pointers are for - to encapsulate raw pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Wouldn't
    Code:
    auto_ptr <int> pi[10] = {new int, new int, new int, new int, new int, 
    	new int, new int, new int, new int, new int};
    work?
    Yes, it's terrible.
    Last edited by Boksha; 04-01-2008 at 02:37 PM.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It would work, but it's not exception-safe. If any of these allocations fail, you leak all others.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    What are you trying to do? auto_ptr is not intended to manage arrays. vector is. std::tr1::array is. boost::scoped_array is. boost::shared_array is. Use those options (and others designed to manage arrays).

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    It would work, but it's not exception-safe. If any of these allocations fail, you leak all others.
    It fails to compile on the MinGW port of g++ 3.4.5, the Comeau online compiler and MSVC8. The reason is that auto_ptr's constructor that takes a T* is explicit. A solution would be:
    Code:
    std::auto_ptr<int> pi[10] = {std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int),
                                 std::auto_ptr<int>(new int)};
    This modified code should be exception safe, methinks. Nonetheless, I do not see what is the point of an array of auto_ptrs.

    Incidentally, this is cross posted in Code Guru: auto_ptr array .
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This modified code should be exception safe, methinks.
    Nope, still isn't. The compiler is free to execute all allocations before constructing a single auto_ptr.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Nope, still isn't. The compiler is free to execute all allocations before constructing a single auto_ptr.
    What is the corresponding section concerning that in the C++ standard?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The definition of sequence points. I'm pretty sure there's no sequence point between the individual initializers of an array.

    Although, I probably should look that up.
    ...
    Yeah, no such thing there. So, the individual initializers of an aggregate initializer are not relatively ordered, and as such, the argument computations are not ordered between them.

    That's not to say that any sane compiler would actually mix them up.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Your reply is clear.

    Quote Originally Posted by Elysia View Post
    Of course, just as for anything else. If you need a vector (or array) that needs to live beyond the end of the function. That's what smart pointers are for - to encapsulate raw pointers.

    Thanks CornedBee,


    Let us know if you have found definite result from standard whether the code in post #11 is exception safe or not? To my point of view, I think it should be safe.

    Quote Originally Posted by CornedBee View Post
    The definition of sequence points. I'm pretty sure there's no sequence point between the individual initializers of an array.

    Although, I probably should look that up.
    ...
    Yeah, no such thing there. So, the individual initializers of an aggregate initializer are not relatively ordered, and as such, the argument computations are not ordered between them.

    That's not to say that any sane compiler would actually mix them up.

    regards,
    George

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21