Thread: Smart pointer class

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, should I try to throw it at the stack?
    Problem is the string function will try to allocate from the heap and fail.

  2. #47
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Then don't add a string, or provide just a small, static buffer. But really, is it that important?
    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

  3. #48
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Tried providing small, static buffers, but it just doesn't want to work right when other function try to allocate like MFC's idiotic CMemoryException class (which can't return an error through a function with a CString reference).
    But ah... it doesn't really matter that much with memory shortage. Likely the whole program will crash anyway.

  4. #49
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Oh, yeah, forgot another assumption my smart pointers make: that when given a raw pointer, they have exclusive access to it. If allocation of the share count fails, they will delete the pointer.

    Eh, reminds me that I have a bug in there. Hold on a second.

    Ah, there you go.



    By the way:
    Uses virtual functions to determine if to call the time critical functions or not.
    Do you realise that virtual functions carry a speed overhead?
    Last edited by CornedBee; 11-01-2007 at 10:42 AM.
    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

  5. #50
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    By the way:

    Do you realise that virtual functions carry a speed overhead?
    'Course, but not much. Unless you have another way to do it without rewriting the class with the current implemtation, then...

  6. #51
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, instead of making different smart pointers for single vs arrays... My AutoPtr deletes memory in any way you want by passing in a custom "deletor" functor. You can write a deletor to do delete p; or delete [] p; or free( p ); or anything else you can think of.
    Actually you could probably use it as a general purpose RAII object, maybe with some small changes...

  7. #52
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, as you all have mentioned, vector is enough for arrays AND for buffers so support for arrays is not necessary.

  8. #53
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The smart pointers in std::tr1 are probably enough for any other smart pointer needs your class is trying to achieve as well. I thought that wasn't the point.

  9. #54
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I found another dangerous behaviour today:
    Code:
    pp< vector<CString> > strFiles = Stuff::FindFiles(Stuff::GetWindowText(m_Directory), m_Recursive.GetCheck() == 1, _T("*.*"), NULL, NULL, strFiles, false, NULL, NULL);
    As far as I'm concerned, it shouldn't work at all since we're passing strFiles when we haven't declared it. What's worse is that the compiler will create strFiles and pass it to the function without calling the objects constructor, thus resulting in a crash.
    Last edited by Elysia; 11-02-2007 at 02:32 PM.

  10. #55
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    int main()
    {
        int i = i + 0;
        std::string str = str + "World!";
    }
    Sometimes you can do bad things in C++. If there's nothing stopping you from doing it with int or string, then I wouldn't worry about it for your class.

  11. #56
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Apparently this compiles without a warning:

    Code:
    #include <iostream>
    
    class A
    {
        public:
        A(): value(10) {}
        void print()const {std::cout << value << '\n';}
        private:
        int value;
    };
    
    A foo(const A& a)
    {
        a.print();
        return A();
    }
    
    int main()
    {
        A a = foo(a);
        std::cin.get();
    }
    My output is 16384 (should be 10 if the constructor was called before print).

    I guess this might be a problem of the language that it allows it. But then you should just avoid writing code that cannot possibly make sense (using an instance to construct the same instance) and stop worrying about particularly broken use cases when designing classes.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #57
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    return A();

    isn't that the equivalent of returning a local variable?

  13. #58
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's ok, because the function returns by value.

  14. #59
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    oh ok.

  15. #60
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    Code:
    int main()
    {
        int i = i + 0;
        std::string str = str + "World!";
    }
    Sometimes you can do bad things in C++. If there's nothing stopping you from doing it with int or string, then I wouldn't worry about it for your class.
    Does anyone know if that is a compiler bug or whether somebody missed that case when defining the C++ language?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM