Re: How to allcate an array of strings dynamically
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sat, 23 Jun 2007 20:04:11 -0400
OK, here's a little exercise in algorithm design...
Working on an embedded system, data is loaded. Because there can be insertions and
deletions during execution, it is stored as a 2-way linked list. In the original model of
the code, the data came in unsorted order and it didn't matter. In the new code, for
performance reasons the data had to be sorted. Whole new algorithms were added for
processing, to add to the original functionality. And performance mattered.
First approximation: read the data into the list. Build an array, copy pointers to the
objects into the array. Call qsort on the array and sort it according to the criteria for
comparison. Then relink the list elements based on the sequence of items in the array.
Now the array is sorted, and we can proceed.
OK, some files are so large that after they are read in, there is no space to allocate the
array. What to do? The list still needs to be sorted...the new code can no longer
operate with an unsorted list.
Next task: while traversing the list when the device is running, there are "conditional
branches" (in effect). If a criterion is met when the node is processed, control proceeds
either to the next node, or a nonadjacent node. But the nonadjacent node is determined by
the input data, and can be any of the several thousand nodes that exist. This is realtime
system on a relatively slow embedded system, and therefore O(n/2) is completely
unacceptable from a performance viewpoint; we'd miss the realtime window by orders of
magnitude. But there isn't space to allocate an array and do bsearch. What to do?
Parameters: Assume a 5MHz machine or thereabouts (ok, it was an 8088). Assume there will
be < 5000 elements in the list (all we could fit into 640K).
Additional problem: for localization, we didn't have room in memory to hold all the user
messages, prompts, etc. So all text for messages was kept in a file. How to implement
LoadString? [Parameters: most messages were < 20 characters, but there were a lot > 45
characters, particularly error messages]. Design an implementation for what we would now
recognize as LoadString that ran on a bare machine, using only standard C file I/O.
[These were very real problems I had to solve. So the assignment is to come up with
solutions to these problems. Answers not due for a week, because I'll be off teaching a
device driver course near Washington DC and I never go online when I travel]
Code never matters, Data is all that matters. The above problems represent data
problems. Code size grew because code was added to enhance performance, both performance
in speed and performance in memory size. Code size was irrelevant compared to data size
problems. Ultimately, we had to address the fact that we were running 1.7MB code in a
640K machine with no virtual memory, but that's a different story (once we solved the
problem, the code size grew from 380K to 1.7MB with no change in the size of the data
space, but that represents a now-dead technological solution, and one of the few remaining
manifestations of this technology today is in the Space Shuttle).
joe
On Sat, 23 Jun 2007 22:53:06 GMT, MrAsm <mrasm@xxxxxxx> wrote:
On Sat, 23 Jun 2007 18:47:30 -0400, Joseph M. NewcomerJoseph M. Newcomer [MVP]
<newcomer@xxxxxxxxxxxx> wrote:
Ah, but the issue here is whether or not the access needs to be done while the list is
being built, or is done AFTER the list is built! If AFTER the list is built, then it is
trivial:
I understand: you convert to array after the list is built: you're
right.
...
Without an understanding of the ultimate use of the data structure, a complete solution
cannot be proposed.
Yes, you're right.
MrAsm
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: How to allcate an array of strings dynamically
- From: Joseph M . Newcomer
- Re: How to allcate an array of strings dynamically
- References:
- How to allcate an array of strings dynamically
- From: rindam2002
- Re: How to allcate an array of strings dynamically
- From: Joseph M . Newcomer
- Re: How to allcate an array of strings dynamically
- From: MrAsm
- Re: How to allcate an array of strings dynamically
- From: Joseph M . Newcomer
- Re: How to allcate an array of strings dynamically
- From: MrAsm
- How to allcate an array of strings dynamically
- Prev by Date: Re: Creating a CChildFrame
- Next by Date: Re: Dynamically allocating an array
- Previous by thread: Re: How to allcate an array of strings dynamically
- Next by thread: Re: How to allcate an array of strings dynamically
- Index(es):
Relevant Pages
|