howto wrap char* in auto_prt

This is a discussion on howto wrap char* in auto_prt within the C++ Programming forums, part of the General Programming Boards category; Hi, Is this allowed? Code: auto_ptr<char> a( new char[10] ); Because shouldn't this be deleted by delete [] and auto_ptr ...

  1. #1
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    howto wrap char* in auto_prt

    Hi,
    Is this allowed?
    Code:
    auto_ptr<char> a( new char[10] );
    Because shouldn't this be deleted by delete [] and auto_ptr does not support this?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, it's not allowed.

    If you want automatic management of your array, use a std::vector. If that's really not what you want, look at the Boost libraries. Their smart pointer library has scoped_array and shared_array, which may fit your needs.
    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. #3
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    won't this do?

    Code:
    class ScopeArray
    {
    char* p;
    public:
    ScopeArray(char* _p):p(_p) {}
    ~ScopeArray() { free(p); }
    };

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's basically what scoped_array is, yes. Except for the free, which has absolutely no business being there! The memory was allocated with new[]. It must be freed with delete[].

    Of course, then there's pointer access, validation of non-deletion of incomplete type, indexing and dereference operator overloads, resetting, preventing copying, debug assertions for not dereferencing null pointers ... I think that's all that scoped_array does.
    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. #5
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Thanks,
    I am not allowed in work to use boost .

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    &#167;"(/&#37; programming classes!

    OK, from memory. But remember: A std::vector is preferable.

    Code:
    template <typename T>
    void checked_array_delete(T *p)
    {
      void( sizeof(T) ); // ensure type is fully defined
      delete [] p; // because if it isn't, this will compile but give undefined behaviour
    }
    
    template <typename T>
    class scoped_array
    {
      T *m_ptr;
    
      // Prevent copying.
      scoped_array(const scoped_array &o);
      scoped_array &operator =(const scoped_array &o);
    
    public:
      explicit scoped_array(T *p = 0) m_ptr(p) { }
      ~scoped_array() { reset(); }
    
      T *get() { return m_ptr; }
      T &operator *() { assert(m_ptr); return *get(); }
      T &operator [](std::size_t idx) { assert(m_ptr); return get()[idx]; }
    
      void reset(T *p = 0) { checked_delete(m_ptr); m_ptr = p; }
    };
    I have to say it again: A std::vector is preferable.
    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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I am not allowed in work to use boost .
    Why not? According to the Boost FAQ:
    "The Boost license permits the creation of derivative works for commercial or non-commercial use with no legal requirement to release your source code."
    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

  8. #8
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Beautiful, Thanks CornedBee.

    When you say vector do you mean create a wrapper for the vector?
    one that will be able to do something like that:
    Code:
    VectorWrapper vw
    strcpy( vw, str);
    or maybe it is possible with stl algorithms, and you meant that?
    Last edited by kroiz; 11-22-2007 at 04:56 AM.

  9. #9
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by laserlight View Post
    Why not? According to the Boost FAQ:
    "The Boost license permits the creation of derivative works for commercial or non-commercial use with no legal requirement to release your source code."
    It is not a license issue, I am not sure ( and it is hard getting an answer, The one with the answer is a busy man with little patience ) but I think that it has to do with trouble building boost on other platforms with the compilers we have.

    I have been asking for boost for a long time.
    My guess is that it is possible in any case but just too much work.
    Last edited by kroiz; 11-22-2007 at 05:01 AM.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, I mean a simple vector, as it is.

    You can use strcpy with a vector like this:
    Code:
    std::vector<char> v(strlen(str) + 1);
    strcpy(&v[0], str);
    Or like this:
    Code:
    std::vector<char> v(str, str + strlen(str) + 1);
    That should be the fastest code if your vector implementation is properly optimized for primitives. Alternatively, how about this?
    Code:
    size_t slen = strlen(str) + 1;
    std::vector<char> v(slen);
    memcpy(&v[0], str, slen);
    Or yes, even with STL algorithms.
    Code:
    std::vector<char> v;
    std::copy(str, str + strlen(str) + 1, std::back_inserter(v));
    or (faster):
    Code:
    size_t slen = strlen(str) + 1;
    std::vector<char> v(slen);
    std::copy(str, str + slen, v.begin());
    GCC's STL optimizes this to a single memmove. (A tiny bit slower than memcpy.)

    Or you could use a proper std::string.
    Code:
    std::string s(str);

    You must work on some obscure platforms. Boost is quite portable, really. OK, not all libraries, and there are some compilers that have serious problems, but generally, why not at least identify some "safe" Boost libraries? Not being allowed to use Boost's smart pointers is absurd.

    Alternatively, perhaps, the C++ Technical Report 1 also includes the smart pointers. This document encourages vendors to supply components within the namespace std::tr1. GCC 4.1 and VC++.Net 2005 both provide the smart pointers, I think. For other compilers, other implementations could be found.
    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

  11. #11
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Thanks again, this is realy beautiful.

    It did not occure to me that since vector uses continues memory I can pass it to functions that has the form of

    Code:
    something f(char* output, int outputLen);
    Is string continues in memory too?

    You must work on some obscure platforms.
    We do, I only build on those platforms I do not however create the makefiles and all that libraries and dependacies. so I dont know much about the obstackles there.

    Building on windows and linux is easy compared to those.
    I guess they dont understand how much it cost them to let us develop in such a crippled environment.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can use the c_str() member function of std::string to obtain a const char*. There is no way to get mutating access to the string.

    By the way, forgot boolean conversion in my quick scoped_array. These members:
    Code:
      typedef T* (scoped_array::*safe_bool)();
      operator safe_bool() { return get() ? &scoped_array::get : 0; }
      bool operator !() { return !get(); }
    The weird boolean conversion is chosen so that use in boolean contexts is possible, but unintended conversions to integers are not.
    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++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,662
    You could make your own, crude smart pointer. It doesn't take a lot of work. It doesn't need to be perfect or full of functionality - just... work.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Sorry, if someone is reinventing a wheel you suggest reinventing a square wheel? ;P
    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).

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,662
    If you can't use a wheel but you need one, then why not use your own crude wheel (that you can use)?

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

Similar Threads

  1. [C#] howto encrypth and decrypth a string value
    By Jelte in forum C# Programming
    Replies: 16
    Last Post: 09-20-2008, 05:29 AM
  2. which design is better to wrap another class instance
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2008, 12:27 AM
  3. Word wrap
    By Orrill in forum C++ Programming
    Replies: 6
    Last Post: 10-14-2005, 02:00 PM
  4. Word Wrap
    By sethjackson in forum Windows Programming
    Replies: 4
    Last Post: 09-21-2005, 04:35 PM
  5. Word Wrap
    By osal in forum Windows Programming
    Replies: 4
    Last Post: 07-02-2004, 11:16 AM

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