Re: Strategies for avoiding new?
From: Leor Zolman (leor_at_bdsoft.com)
Date: 02/23/04
- Next message: osmium: "Re: multiple file programs"
- Previous message: Josh Sebastian: "Re: Strategies for avoiding new?"
- In reply to: Wolfie: "Strategies for avoiding new?"
- Next in thread: Wolfie: "Re: Strategies for avoiding new?"
- Reply: Wolfie: "Re: Strategies for avoiding new?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 23 Feb 2004 00:33:31 GMT
On Sun, 22 Feb 2004 23:57:16 GMT, Wolfie <dbgbdwolf@gte.net> wrote:
>
>I'm wondering if I'm missing something. Coming from a
>C background, I'm using a typical "for each data read,
>allocate and store (the pointer) in a container" strategy.
>IOW, I'm using map (in some cases) but (generally) the
>data type stored is a pointer.
>
>I'm just wondering if there's an alternative to using new
>in these general cases where you don't know the exact number
>of items to store in the container at design time (and/or
>don't want to create a huge container and individual items
>one may not need.)
The criteria for deciding whether to store values or pointers in your
"container" is pretty much the same regardless of whether you're working in
C or C++. You have to balance your use of memory, time, complexity, etc.
against the requirements of the app.
So whether you store pointers or values in the container depends upon what
serves the purposes of your program the best. If you've chosen to use a
std::vector and have to sort it a lot, and the objects are big, then
storing pointers may work out better than storing bulky values.
I don't quite understand your question about whether "there's an
alternative to using new", since you're not forced to use new in the first
place. Even if you're storing pointers, they don't /have/ to be pointing to
dynamically allocated objects (although they typically would be.)
Are you asking about using new in the sense of allocating a dynamic array
in C, so that you're then "stuck" with its size unless you do a complete
re-allocation? STL containers handle their own resizing (std::vector and
std::string, which also let you reserve space if you know the minimum size
up front and want to save a bit of execution time) or other memory
management needs (the other containers) automatically. If you choose your
data structures carefully, the price for that overhead should not be an
issue.
>
>Something about the whole "always storing pointers in
>containers" bothers me, but maybe I'm missing the cases
>where a container stores objects (rather than pointers
>to objects).
Are you saying that you've only seen code where the value_type of an STL
container is a pointer? I'm not sure you've looked at very much code. You
may find Scott Meyers' book _Effective STL_ a good investment at this
point.
Good luck!
-leor
>
Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
- Next message: osmium: "Re: multiple file programs"
- Previous message: Josh Sebastian: "Re: Strategies for avoiding new?"
- In reply to: Wolfie: "Strategies for avoiding new?"
- Next in thread: Wolfie: "Re: Strategies for avoiding new?"
- Reply: Wolfie: "Re: Strategies for avoiding new?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|