Like Tree1Likes

If you declare a local pointer with the same name & type as a pointer data member ...

This is a discussion on If you declare a local pointer with the same name & type as a pointer data member ... within the C++ Programming forums, part of the General Programming Boards category; Point is, the OP is doing just that, and hence, a smart pointer should relieve some of that burden. A ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Point is, the OP is doing just that, and hence, a smart pointer should relieve some of that burden.
    A smart pointer hacked with a deleter doesn't solve the problem either though -- you need to use new[] as well. Custom allocator maybe? I don't even know if they work like that to be honest. But you are fighting me more about this than you should be.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by whiteflags
    A smart pointer hacked with a deleter doesn't solve the problem either though -- you need to use new[] as well.
    Err... that generally is less of a problem than not using a deleter to invoke delete[].
    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

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    I never took the time to care about such a minute detail.

    new[] pairs with delete[]
    new pairs with delete

    And I don't change that just because I'm using template magic and RAII.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by whiteflags
    And I don't change that just because I'm using template magic and RAII.
    You shouldn't change that, which is precisely why if you do choose to use new[] with unique_ptr or shared_ptr, then you should provide a deleter to invoke delete[].
    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

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Since whiteflags still does not accept private messages, and since this might be of use to other people who wants to use custom deleters, here is an example on how to do it:

    Code:
    std::shared_ptr<int> test(new int[5], [](int* p) { delete [] p; });
    Yep. That's it. That's all you need.

    If you really need unqiue_ptr, then this should do the trick:
    Code:
    auto deleter = [](int* p) { delete [] p; };
    std::unique_ptr<int, decltype(deleter)> test2(new int[5], deleter);
    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. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Elysia
    If you really need unqiue_ptr, then this should do the trick:
    Code:
    auto deleter = [](int* p) { delete [] p; };
    std::unique_ptr<int, decltype(deleter)> test2(new int[5], deleter);
    I believe that you only need to write:
    Code:
    std::unique_ptr<int, std::default_delete<int[5]>> test2(new int[5]);
    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

  7. #22
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Actually from these 2 souces:
    std::default_delete - Cppreference
    std::unique_ptr - Cppreference

    It should be enough to just write
    Code:
    std::unique_ptr<int[]> uptr(new int[10]);
    laserlight likes this.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static pointer data member
    By suppu143 in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2011, 07:46 AM
  2. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  4. Pointer to array as member data...
    By JulesGlass in forum C++ Programming
    Replies: 6
    Last Post: 04-19-2008, 08:42 AM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread


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