[comp.lang.pascal] How to go over Stack

robby@nuchat.sccsi.com (Robert Oliver Jr.) (06/12/91)

 
  Hi,
 
    Has anyone out there been over 65520 in stack and had the program work?
I need to know how to get over the stack limit.  Sence the pointer will not 
work right.  Is there something wrong with this statement ?
 
     Testing : Array[1..220] of ^Test;   {Test is a Type Record}
  
and calling the data with         Testing[Index]^.Whois  ???
 
  Is there something wrong with this ?   
          
                                                      Thanks,
 
                                                      Robert Oliver  

dm2368@eecs1.eecs.usma.edu (Fichten Mark CPT) (06/13/91)

Robert Oliver writes:

>   Hi,
>     Has anyone out there been over 65520 in stack and had the program work?
> I need to know how to get over the stack limit.  Sence the pointer will not 
> work right.  Is there something wrong with this statement ?
>  
>      Testing : Array[1..220] of ^Test;   {Test is a Type Record}
>   
> and calling the data with         Testing[Index]^.Whois  ???
>  
>   Is there something wrong with this ?   

This will work fine IF you 'attach' a record to each element of the
array like this:

for i:=1 to 220 do
begin
	New(Testing[i]);
	{ Initialize the fields of your record here... }
	Testing[i]^.Next := nil;
end;

This loop will set you up a structure like the following:

  ARRAY         Records 
 TESTING
|-------|      ---------
|   1   |----->|       |--nil
|-------|      --------- ---------
|   2   |--------------->|       |--nil
|-------|                ---------
|   .   |
|   .   |
|   .   |
|-------|      ---------
|  218  |----->|       |--nil
|-------|      --------- ---------
|  219  |--------------->|       |--nil
|-------|      --------- ---------
|  220  |----->|       |--nil
|-------|      ---------


The loop allocates memory OFF OF THE HEAP, NOT THE STACK.  Assuming that
you have not decreased the size of the heap with a compiler directive,
or used up most of your heap, this should work fine.

Remember to deallocate all the memory if you are finished with the array
and the program does other things.  A loop like the following will work:

for i:=1 to 220 do
	Dispose(Testing[i]);

This will free all the memory ONLY if you have not 'attached' any other
nodes on the ends of the records.  I'll leave any more complex disposal
loop to you!

Hope this helps.
____________________________________________________________________________

CPT Mark Fichten      | INTERNET: fichten@eecs1.eecs.usma.edu              |
Captain U.S. Army     |           @trotter.edu:dm2368@eecs1.eecs.usma.edu  |
Work: (914) 938-5580  | USENET:   rutgers.edu!trotter!eecs1!dm2368         |
                      |           harvard.edu!trotter!eecs1!dm2368         |
____________________________________________________________________________

dave@tygra.Michigan.COM (David Conrad) (06/14/91)

In article <1991Jun12.071024.12162@nuchat.sccsi.com> robby@nuchat.sccsi.com (Robert Oliver Jr.) writes:
>
> 
>  Hi,
> 
>    Has anyone out there been over 65520 in stack and had the program work?
>I need to know how to get over the stack limit.  Sence the pointer will not 
>work right.  Is there something wrong with this statement ?
> 
>     Testing : Array[1..220] of ^Test;   {Test is a Type Record}
>  
>and calling the data with         Testing[Index]^.Whois  ???
> 
>  Is there something wrong with this ?   
>          

I think there are two misunderstandings here.  The first is that data is
not stored in the stack, but the Data Segment (hence the name).  I think
the limit you are trying to break is the 64k limit on data.  The second
misunderstanding lies in the use of pointers; although you haven't given
us a complete example program, I think I can guess the problem.  If I've
guessed wrong and am talking down at you, I apologize.

When you declare a pointer, no space is allocated for the data.  E.g.

  type
    Test = record
      name : string[50];
      address : string[110];
    end;

  var
    t : Test;
    tptr : ^Test;

Variable t has 162 bytes allocated to it.  (Remember, there's an additional
byte for the length of the string.)  There's room enough there for a name
and an address.  But variable tptr only has 4 bytes allocated to it.  It
can't contain a name and address, just a *pointer* to a Test record.  You
have to allocate the space from the heap, either with New and Dispose,
or Getmem and Freemem.  E.g.

  {continuing the previous example}
  begin
    t.name := 'Sam Pull';
    t.address := '1234 Main Street, Nowhere IN 23456';
    New (tptr);
    tptr^.name := 'Anne Teek';
    tptr^.address := '101 Cherry Lane, Nowhere IN 23456';
    writeln (t.name);
    writeln (tptr^.name);
    Dispose (tptr);
    {Now we'll use Getmem and Freemem}
    Getmem (tptr, sizeof(Test));
    tptr^.name := 'Minnie Mum';
    tptr^.address := '1 Little Place, Nowhere IN 23456';
    writeln (tptr^.name);
    Freemem (tptr, sizeof(Test));
  end.

The output of this little program would, of course, be:
 
Sam Pull
Anne Teek
Minnie Mum

I hope I haven't insulted your intelligence, I realize this is much
simpler faire than is usually seen in this newsgroup, but I decided
to post instead of mail in case anyone else was having similar troubles.
Pointers can be confusing, and it's easy to confuse the pointer with
the thing pointed at, or vice versa.
--
David R. Conrad
dave@michigan.com
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave@Michigan.COM