Board index » delphi » Newbie needs help please.

Newbie needs help please.

Ok here is my problem. I am trying to write a program that can read a txt
file of up to 100 names.  Need to be able to sort them and search them.  My
problem is that I am trying to read the names into an array [0..100] of
string.  I think I am doing it wrong cause I keep getting a stack overflow
error.  What is the best way to read a bunch of names into an array?  I will
figure out the rest when I can finally get the names in place where I can
manipulate them.  Thanks for the help.
post reply or reply to ot...@flash.net
 

Re:Newbie needs help please.


Quote
> Ok here is my problem. I am trying to write a program that can read a txt
> file of up to 100 names.  Need to be able to sort them and search them.  My
> problem is that I am trying to read the names into an array [0..100] of
> string.  I think I am doing it wrong cause I keep getting a stack overflow
> error.  What is the best way to read a bunch of names into an array?  I will
> figure out the rest when I can finally get the names in place where I can
> manipulate them.  

  Stack Overflow error?  Where did you declare this array?  How did you
declare it?  (The point is, you should be getting Stack Overflow errors
unless you declared this array within a subprogram, and given your
application here, I don't think you want to be storing the data in the
subprogram and then calling a sort routine...which either couldn't access
the data or would _really_ create Stack Overflow.)
   It seems there are many things wrong here, and I don't know exactly
how to solve/correct them.  Here are some suggestions:
  1. Make the array smaller, but using string[50] or some realistic size
for your "names" - could any of them possibly be 255 characters in
length?  Using "string" defaults to "string[255]"...
  2. Declare this array as global, not local to a subprogram.
  3. If you _must_ keep this data local, increase your $M Stack value:
the default is 16K, but you may reserve up to 64K.  Still, this is not a
good thing to do in most cases.
  4. You might declare the array as an array of pointers, and allocate
each data item on the Heap when the data is read from the file.  I assume
you know little or nothing of Pascal pointers, but this is always the
best solution for (1) unknown amounts of data and (2) situations where
you don't have enough Data Segment (max. 64K) available for your
application.

Re:Newbie needs help please.


Quote
Ollie T wrote:

> Ok here is my problem. I am trying to write a program that can read a txt
> file of up to 100 names.  Need to be able to sort them and search them.  My
> problem is that I am trying to read the names into an array [0..100] of
> string.  I think I am doing it wrong cause I keep getting a stack overflow
> error.  What is the best way to read a bunch of names into an array?  I will
> figure out the rest when I can finally get the names in place where I can
> manipulate them.  Thanks for the help.

Here is an example of a program that causes overflow when the stack size
is 16384

program overflow;
const n=100;
type atype=array[1..n]of string;
var a:atype;

procedure dosomething(a:atype);
var i:integer;
begin for i:=1 to n do a[i]:='';
end;

begin dosomething(a)
end.

The reason is that a new array is created when the procedure
"dosomething" is called. A simple change is to make "a" a "var"
parameter, so that the procedure accesses the original array. Of course,
the advice given by Pedt Scragg also helps.

--
Rommert J. Casimir
Tilburg University, Room B435, tel 31 13 4662016
P.O. Box 90153, 5000LE Tilburg, The Netherlands
http://cwis.kub.nl/~few/few/BIKA/rc_home.htm

Other Threads