Thread: Boost shared_ptr and shared_array

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    Boost shared_ptr and shared_array

    Hi all,

    Finally I'll use the Boost libraries as well. Whahou!

    But, I've got a question about the shared_ptr and its array version, the shared_array.

    I want to transform the following code
    Code:
    myVariable* _data_service[8192]
    in something like this:
    .h
    Code:
    shared_array<myVariable> _data_service;
    .cpp
    Code:
    _data_service = shared_array<myVariable>( ??? );
    How can I instantiate the array with a fixed value? This fixed value is necessary for my has tables.

    If not, I'll use an std::vector<shared_ptr> , but this seems more heavy than the Boost shared_array solution.

    Thanks!!
    Last edited by MarkZWEERS; 09-24-2008 at 06:25 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    shared_array<myVariable> is not the equivalent of myVariable* [8192]. One is a pointer to a dynamically allocated array. The other is an array of pointers.

    What is the purpose of _data_service? How do you use it? Why do you want to make those pointers shared? Those are the questions you need to ask yourself to decide on the correct smart pointer.
    For example, it might be that the right thing to use here is a scoped_ptr<myVariabl>[8192].
    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
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    The usage is a storage of data in a hash table. Each element in the table will be a pointer, pointing to a dynamically allocated object. However, I use these pointers in "client" code, which might delete the objects. That's why I cannot delete the objects by its pointers at destruction of the hash table, which causes memory leaks. That's why I thaught that a shared_ptr might be the solution, weak_ptr or std::auto_ptr did not seem the right solution to me. From its name, I have the feeling that scoped_ptr is not the right solution either.

    I now see my confusion: a shared_array is equivalent to a shared_ptr to an std::vector

    I want the opposite, an array of shared_ptr s. So then the code
    Code:
    shared_ptr<myVariable> _data_service[8192]
    will do?

    thanks!

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yep. Of course, now you have to change the client code to consistently use shared_ptr (or weak_ptr where appropriate) too.
    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
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    :-( that's why people should think before doing... I'll spend my weekend replacing lines of code of colleagues !

    Just for verification: if my object is already managed by a shared_ptr, and I get this shared_ptr by some 'get' function on the hash table, than I'll do something like
    Code:
    weak_ptr<myVariable> p = storage->get("name");
    I store shared_ptr s. So will I need to return a weak_ptr in the 'get' function ?

    And how about dynamic casts? Something like
    Code:
    shared_ptr<my_derived_type> p = dynamic_cast<>(storage->get("name"));
    Code:
    if (you think casting is ugly)
    {
    i agree;
    see my first line;
    }
    Last edited by MarkZWEERS; 09-24-2008 at 08:28 AM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    For dynamic casts, there's dynamic_pointer_cast. It works exactly like dynamic_cast, but is in fact a template function using some tricky overloading to work with shared_ptr.

    And I think you've got the weak_ptr syntax right. But you can return a shared_ptr from get and should do so.
    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
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Hmm.. I have this bug which I can't get rid of.
    Code:
    weak_ptr<GenericContainer> GetContainer( ) {
        return _container;
    }
    Variable.h(195) : error C2143: syntax error : missing ';' before '<'
    Variable.h(195) : error C2501: 'weak_ptr' : missing storage-class or type specifiers
    Variable.h(195) : error C2059: syntax error : '<'
    Variable.h(195) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body


    as if it does not recognize the type "weak_ptr" (with shared_ptr I have the same problem).

    It only occurs when I use it as return type of a function, when I use it as input argument or in a body, it works.

    What can this be?

    ps: I'm using MSVC6 , for which the following directive is used in the "smart_ptr.hp" header:
    Code:
    #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
    I know it's a very old compiler...
    Last edited by MarkZWEERS; 09-24-2008 at 01:30 PM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    MSVC6 and Boost really don't mix. I think the devs have given up caring about it at all with 1.34. All problems that occur are therefore difficult.
    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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you sure you shouldn't return a shared pointer?
    Weak pointer should only be used to get rid of dependencies.
    But if you don't return a shared pointer, the pointer has the risk of becoming invalid as you use it.
    You're working for like, some company, I take it, who can't upgrade their compiler?
    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.

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Well, the smart pointer which owns the object will be the smart pointer I store in my dataservice. There where I only use the object for its data members, I should use a weak_ptr?

    But maybe you're right, because I also want to change the value of these data members. And then the smart pointer should own the object, right? Then I'd return a shared_ptr, as you say, and use the implicit cast to a weak_ptr when I only want to inspect its data members.

    Yes, my company does not want to upgrade their compiler yet. From january on we will upgrade to 2005, thank god. At home I stick to my gcc4.3 compiler, at least standard-compliant.

Popular pages Recent additions subscribe to a feed