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

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    28,413
    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[].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    28,413
    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[].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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]);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    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]);

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