Professional Web Applications Themes

Arrays of objects - Mac Programming

Just read a bit of "C++ FAQs, " and saw a section about how keeping an array of pointers is a Bad Thing, due to the potential for zombies and stuff. Theyy recommend using arrays (actually the container classes) to store values or structs. However, being self-taught (well, book-taught), I've nver heard of this. I've used this technique many times without problems. I've often had LArrays (in PP) store thousands of pointers to objects I've created. I have such nice little objects - why devolve them into structs, right? Can someone more C++-savvy provide an alternative solution or justification (or ...

  1. #1

    Default Arrays of objects

    Just read a bit of "C++ FAQs, " and saw a section about how keeping an
    array of pointers is a Bad Thing, due to the potential for zombies and
    stuff. Theyy recommend using arrays (actually the container classes) to
    store values or structs.

    However, being self-taught (well, book-taught), I've nver heard of
    this. I've used this technique many times without problems. I've often
    had LArrays (in PP) store thousands of pointers to objects I've
    created. I have such nice little objects - why devolve them into
    structs, right? Can someone more C++-savvy provide an alternative
    solution or justification (or say, it don't matter)?
    Mikey Guest

  2. #2

    Default Re: Arrays of objects

    In article <311020031338135879%com>, Mikey <com>
    wrote:
     

    This is a question to which the right answer far exceeds what I am willing to
    put in a Usenet posting. I recommend you read Effective C++, More Effective C++,
    and Effective STL by Meyers, and Exceptional C++ and More Exceptional C++ by
    Sutter. Specifically, look for sections of those books having to do with
    exception safety in general and with regards to containers in particular.

    hth

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  3. #3

    Default Re: Arrays of objects

    dans l'article 311020031338135879%com, Mikey à
    com a écrit le 31/10/03 19:38:
     

    The best solution is the one that works.

    Generally speaking, the probability that you have a bug when using
    collections of pointers is higher than when using collections of values,
    because it requires a lot of attention to properly manage memory allocation
    and disposal. This is one of the reasons why many developers have moved to
    Java.

    However, since your code works, there is no reason you should change it.

    From a theorical standpoint, if your collection is filled with homogeneous
    objects, it is an improvement to alloc an array of structs rather than an
    array of pointers to structs, because it will save a lot of memory,
    especially with small structs. A pointer is typically 20 bytes + the
    allocation. So if have an array of 1000 Rect*, the alloced memory will be
    1000*(20+sizeof(Rect)) + 1000*sizeof(Rect*), that is 32000 bytes. With an
    arry of structs, the allocation would be only 1000*sizeof(Rect), that is
    8000 bytes.

    However, arrays of structs is generally more constraining than arrays of
    pointers, because you cannot cast a struct as easily as you cast a pointer.

    This is where C++ templates can be very helpful. Have a look at them, I
    think the standard C++ library provides templates for arrays of structs.

    An important point is that arrays of pointers cannot always be avoided. For
    example, say you have a class Shape, subclassed as Oval, Rectangle, Polygon
    and so on. Since Oval, Rectangle etc have different types, and most probably
    different sizes, there is no way you can collect their values in a single
    array. In that case, the only solution I know is using an array of pointers
    to Shape(s). However, even in that case, using templates is a good habit,
    because it avoids casting, therefore enabling compile-time type checking.

    Good luck,

    Eric

    Eric Guest

  4. #4

    Default Re: Arrays of objects

    In article <311020031338135879%com>,
    Mikey <com> wrote:
     

    There is nothing wrong with vector<vector<int> >. The contents of a
    standard container don't have to be non-objects, they just have to be
    objects with the correct semantics.

    Think about exception safety: if you run out of memory inside
    LArray::InsertItem(), who deletes the item? Similarly for
    std::vector<ItemType>::push_back().

    Also, The standard library gets much of its power by assuming it can
    make temporary copies of the items in the containers.

    Look at std::vector<>s of boost::shared_ptr<>s. See http://Boost.org/
    for more information.
    David Guest

  5. #5

    Default Re: Arrays of objects

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     
    >
    > This is a question to which the right answer far exceeds what I am willing to
    > put in a Usenet posting. I recommend you read Effective C++, More Effective
    > C++,
    > and Effective STL by Meyers, and Exceptional C++ and More Exceptional C++ by
    > Sutter. Specifically, look for sections of those books having to do with
    > exception safety in general and with regards to containers in particular.
    >[/ref]
    Actually the Effective C++ (vanilla + More) was the first thing C++
    text I ever bought. I must have skipped this bit <g>.
    Mikey Guest

  6. #6

    Default Re: Arrays of objects

    In article <BBC8AD80.14B37%fr>, Eric VERGNAUD
    <fr> wrote:
     
    Okay, thanks. I thought I really left a mess behind me, in theory. I'm
    obsessive about the relevant issues - exceptions, allocation and
    deallocation, and keeping track of stuff. It's the idea that it's a
    lame design is what bothers me the most.
     

    ....and naturally if they're homogeneous, you don't really need them to
    be anything _but_ structs. I think I was typically creating nice
    encapsulated objects, working with a single one, buyilding up, and then
    adding them to an app that would be dealing with _many_. Obviously the
    app doesn't need a whole bunch of homogenous objects being created; the
    methods are redundant. 

    Hmmm.... the template idea makes sense.

    Thanks to everyone!
    Mikey Guest

  7. #7

    Default Re: Arrays of objects

    In article <sf.sbcglobal.net>, David
    Phillip Oster <org> wrote:
     
    >
    > There is nothing wrong with vector<vector<int> >. The contents of a
    > standard container don't have to be non-objects, they just have to be
    > objects with the correct semantics.
    >
    > Think about exception safety: if you run out of memory inside
    > LArray::InsertItem(), who deletes the item? Similarly for
    > std::vector<ItemType>::push_back().
    >
    > Also, The standard library gets much of its power by assuming it can
    > make temporary copies of the items in the containers.
    >
    > Look at std::vector<>s of boost::shared_ptr<>s. See http://Boost.org/
    > for more information.[/ref]

    Okay, I've gotten over my panic attack. I realize now that I've been
    using all sorts of convenient objects classes when making my own
    objects, and that's a good thing. In fact it's probably unavoidable.
    How can anyone _not_ use objects for handling strings, GWorlds,
    arbitrary chunks of data, and so on...? In my case, PowerPlant has made
    me think in terms of "what encapsulated, convenient functionality can I
    make use of?" That's not a bad thing. Granted, I'm going to go thru
    "Effective" one more time....
    Mikey Guest

Similar Threads

  1. Merge/Concat two arrays of objects
    By flashharry! in forum Macromedia Flex General Discussion
    Replies: 3
    Last Post: May 20th, 02:31 AM
  2. Objects and Arrays...
    By Pedro miguel freire custódio in forum PERL Beginners
    Replies: 3
    Last Post: January 30th, 06:48 AM
  3. Storing Objects/Arrays in Stored Objects
    By teflon000 webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: January 24th, 07:11 PM
  4. [PHP-DEV] Objects as Arrays
    By Marcus Börger in forum PHP Development
    Replies: 4
    Last Post: September 19th, 10:38 PM
  5. Using & for arrays of objects
    By Jason Creighton in forum Ruby
    Replies: 30
    Last Post: July 6th, 10:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139