Board index » delphi » Need help with stack crash!

Need help with stack crash!

I wrote this tiny program, hardly 100 lines of code, for my
daughter to generate random story titles.  The idea is that it
takes a list of title prefixes, like "The adventures of" or "I
was a {*word*249}age" or whatever, then selects from a list of
adjectives, then from a list of subjects - so the end result is a
title like "I was a {*word*249}age banana" or "The mystery of the weird
moose".  Hey, it goes over great with an 8-year-old.

So anyway, I was polishing it up for my own anal-retentive
aesthetic reasons, and I thought "There's no reason to have the
main body of code right there; it should be a procedure so that
my main program is basically begin/print titles/end."  I took the
section of code that actually generates the random titles and
stuck it in Procedure MakeTitles, and recompiled.

Suddenly I get hieroglyphics when I run.  It dumps out the
suggestion "Write a story with the title"... and several lines of
gibberish.

The program has three procedures:  Load the title portions into
an array from an ASCII file, create and print a story title, then
wait for a keypress.  Nothing else.  The titles are loaded into
an array of this type:
   TYPE TitlePart = RECORD
      nSize: integer;
      Words: array[ 1..50 ] of string[30];
   END;
   TYPE TitleList = array [ 1..3 ] of TitlePart;
                   {1=prefix, 2=adjective, 3=subject}

Maybe 5400 bytes, right?  Yet the only way I could get it to work
is by putting the VAR for that TitleList into the data segment as
a global variable.

I tried increasing stack size to 64K; I made sure stack checking
was enabled (it didn't report a problem); I ensured sizeof(
TitleList) really is 5400 bytes or so.

What am I doing wrong?  For a bit of fluff like this that's just
for my own amu{*word*224}t it doesn't matter, but I've spent 15 years
being trained to minimize the use of global variables and instead
to put everything on the stack.

Oh, I guess I should mention this is TP 7.0 for DOS.  If you
wanna see the code and laugh at my hopeless incompetence, email
me and I'll send it along.

---------
Reply to pbs002 at hotmail dot com.  All adverti{*word*224}ts will
be returned with complaints to your postmaster, and all chain
letters will be forwarded to pyra...@ftc.gov.

 

Re:Need help with stack crash!


In article <199712311829.KAA16...@fat.doobie.com>, "Peter B. Steiger"
<see....@for.email.address> writes

<snip>

Quote
> I took the
>section of code that actually generates the random titles and
>stuck it in Procedure MakeTitles, and recompiled.

>Suddenly I get hieroglyphics when I run.  It dumps out the
>suggestion "Write a story with the title"... and several lines of
>gibberish.

>The program has three procedures:  Load the title portions into
>an array from an ASCII file, create and print a story title, then
>wait for a keypress.  Nothing else.  The titles are loaded into
>an array of this type:
>   TYPE TitlePart = RECORD
>      nSize: integer;
>      Words: array[ 1..50 ] of string[30];
>   END;
>   TYPE TitleList = array [ 1..3 ] of TitlePart;
>                   {1=prefix, 2=adjective, 3=subject}

>Maybe 5400 bytes, right?

4656 bytes for TitleList plus whatever other variables you have
declared.

Quote
> Yet the only way I could get it to work
>is by putting the VAR for that TitleList into the data segment as
>a global variable.
>I tried increasing stack size to 64K; I made sure stack checking
>was enabled (it didn't report a problem);
>What am I doing wrong?  

Getting gibberish probably means that you are either having a problem
with the code for reading / filling the arrays or something is
overwriting the memory you have stored the array in.

You could try running the program in the IDE de{*word*81} and watch the
value of the variable of type TitleList.

This would enable you to see whether the array was read in correctly or
where the corruption occurred.

Quote

--
Pedt Scragg <postmas...@pedt.demon.co.uk>

In principle,   is there uncertainty that
Heisenberg was working his best in chaos?

Re:Need help with stack crash!


Quote
Peter B. Steiger wrote:

> I wrote this tiny program, hardly 100 lines of code, for my
> daughter to generate random story titles.  The idea is that it
> takes a list of title prefixes, like "The adventures of" or "I
> was a {*word*249}age" or whatever, then selects from a list of
> adjectives, then from a list of subjects - so the end result is a
> title like "I was a {*word*249}age banana" or "The mystery of the weird
> moose".  Hey, it goes over great with an 8-year-old.

[>snip<]

Why do you think it's a stack-problem?
Couldn't it be something with the variable-declaration/initialization?

If you haven't found out what it is already let's see the code...

Remco de Korte

Re:Need help with stack crash!


"Peter B. Steiger" <see....@for.email.address> wrote:

Quote
>Maybe 5400 bytes, right?  Yet the only way I could get it to work
>is by putting the VAR for that TitleList into the data segment as
>a global variable.
[...]
>Oh, I guess I should mention this is TP 7.0 for DOS.  If you
>wanna see the code and laugh at my hopeless incompetence, email
>me and I'll send it along.

TP 7.0 will clear all variables in the data segment.  This has the
effect of making all integer-types 0, real-types 0.0, all chars
#0, and all strings empty.  When you declare local variables on
the stack, they initially contain whatever values where in memory
at that location.  This means your 30 character strings could
actually contain 255 characters!  Certainly these would appear as
hieroglyphics if you depended upon them being empty. ;-(

Begin you procedure with --  
  FillChar(TitleList, sizeof(TitleList), 0)

and you'll be back to where you were when TitleList was a global
variable.

    ...red

--
Support the anti-Spam amendment
  Join at http://www.cauce.org/

Re:Need help with stack crash!


Quote

> Suddenly I get hieroglyphics when I run.  It dumps out the
> suggestion "Write a story with the title"... and several lines of
> gibberish.

> I tried increasing stack size to 64K; I made sure stack checking
> was enabled (it didn't report a problem); I ensured sizeof(
> TitleList) really is 5400 bytes or so.

> What am I doing wrong?  For a bit of fluff like this that's just
> for my own amu{*word*224}t it doesn't matter, but I've spent 15 years
> being trained to minimize the use of global variables and instead
> to put everything on the stack.

Difficult to comment without seeing your code, but the normal cause of your
gibberish is normally caused by
1) Uninitialised variables.
2) Functions that do not return a result
3) Using pointers that are walking parts of memory that do not belong to
them
4) Array indexes that are out of range.

Stack overflow is normally caused by recursive routines.

Make sure you compile with range checking on and you may get an address of
the error at run time.
If you cant come right then e-mail your code and I will have a look at it
for you.

Regards
Mike Heydon

Re:Need help with stack crash!


Mr. de Korte, Heydon, and Donais all pointed out that it wasn't a
stack problem at all; it was variable initialization.  Mr. Donais'
explanation of how TP 7.0 will initialize data segment variables at
startup but not stack segment variables was a big help in
understanding why it worked when I was using a global variable.

To clarify for the curious, it wasn't exactly the string space that
was corrupt.  What I had was a record like this:
   TYPE TitlePart = RECORD
      nSize: integer;
      Words: array[ 1..50 ] of string[30];
   END;

My program would keep track of how many strings had been loaded
into the array with nSize - and it was that nSize that was not
being initialized.  So when I wrote:
  aTitles.nSize := aTitles.nSize + 1;
  aTitles.Words[ aTitles.nSize ] := cTemp;

.. there's no telling where it was putting the string,
but it sure wasn't going to the right part of the array.

I appreciate all the Usenet and email responses I received; I know
where to look the next time I run into trouble!  I'm also impressed
at your ability to diagnose my error without seeing my code.

Thanks a lot, guys... and happy new year, eh?

---------
Reply to pbs002 at hotmail dot com.  All adverti{*word*224}ts will
be returned with complaints to your postmaster, and all chain
letters will be forwarded to pyra...@ftc.gov.

Re:Need help with stack crash!


Peter B. Steiger <see....@for.email.address> wrote in article
<199712311829.KAA16...@fat.doobie.com>...

Quote
> ...
> So anyway, I was polishing it up for my own anal-retentive
> aesthetic reasons, and I thought "There's no reason to have the
> main body of code right there; it should be a procedure so that
> my main program is basically begin/print titles/end."  I took the
> section of code that actually generates the random titles and
> stuck it in Procedure MakeTitles, and recompiled.

> Suddenly I get hieroglyphics when I run.  It dumps out the
> suggestion "Write a story with the title"... and several lines of
> gibberish.

As I understand it, you are taking the main part of the program, making it
a procedure and then it behaves differently when you call it.

The variables used by the "main" section of the program are in the data
segment, they are global variables. TP7 fills these variables with zeros
when it starts up. However, when you make the code into a procedure, the
variables are on the stack, and are not initialised. I would suspect that
an index into your array of title bits'n'pieces, or some other variable, is
not being initialised, but that 0 is what you would initialise it to. This
bug is no longer hidden when the variables are on the stack. Put the
variables in a watch window and step through the code, you should see which
one is at fault.

FP

Other Threads