[Sussex] C++ question
Steve Dobson
SDobson at manh.com
Mon Jan 20 15:31:01 UTC 2003
Geoff
On 20 Jan 2003 Geoff Teale wrote
> Steve wrote:
> > 2). Use a linked list. Slowwer to search, but very fast to add and
> > remove on the fly. As you said this was a C++ question not a
> > C question I'll assume that you could hold a flag that marks if
the
> > pointer array is dirty or not with the (master) linked list
structure.
> > Then you need only re-size the array when needed - don't bother
> > keeping the array in step with the linked list.
>
> Hadn't really thought of this - there is really no reason why I couldn't
do
> this for my purposes. Effectively I'm writing a simplified wrapper for
Xlib
> as part of a little something I'm rustling up, I have an "Application"
> object which (in order to present some higher level functionality) needs
to
> present all of its "Window" objects through a user-searchable data
> structure. This may end up being an iterator so the internal data
structure
> doesn't really matter that much - though the point here is to keep it
simple
> for the user (coders) point of view rather than rewriting QT!!
>
> Shall give this some thought - does it really give me any advantages?
Only one: speed. If there is space at the end of your dynamic array then
the OS can just adjust the size of this chunk in the heap. This saves a lot
of time as getting more heap space from the kernel and adding into the
processes MMU structures is expensive.
You way of doing it all by hand ganentees that that there will be a
call to malloc(2) and free(2). Both of these trap into the kernel and
that's
a long processes. realloc(2) is only one trap into kernel and when it
returns the copy (if needed) has been done for you.
When I found this I quite liked using it. Don't know if I really gained,
but my code looked cool, and I expanded my knowleage of system calls. So
I won - it gave me a chance to look good on this list :-)
Steve
More information about the Sussex
mailing list