3 lines c code has stack overflow, help ! 
Author Message
 3 lines c code has stack overflow, help !

Quote:

>Hi, everybody, I got this c code compiled under turbo c++ 3.0
>using large model. I also set stack overflow check. The executable
>has stack overflow. Maybe there is an obvious mistake, so please
>point it out for me. adTHANKSvance.

>---- c code source here ----
>#include <mem.h>

>int main(void)
>{
>   unsigned char arr[10000];

the problem here is stack space. turbo-c, and most other pc-based C
compilers, set the default stack to 4K which is more than ample for
most applications. it can be reset using a statement such as
_stklen=16384  or some such which is compiler dependant. other ways
to solve this problem:
  (1) use malloc. this should probably be done anyway for a 10K array
  (2) declare arr[] globally

unsigned char att[10000];

int main(void)
{
..

Quote:
}

  this will put attr[] in the data segment where you have around 64K
to play with.

--kyle



Mon, 05 Dec 1994 22:25:30 GMT  
 3 lines c code has stack overflow, help !

Quote:


>>Hi, everybody, I got this c code compiled under turbo c++ 3.0
>>using large model. I also set stack overflow check. The executable
>>has stack overflow. Maybe there is an obvious mistake, so please
>>point it out for me. adTHANKSvance.

>>---- c code source here ----
>>#include <mem.h>

>>int main(void)
>>{
>>   unsigned char arr[10000];

>the problem here is stack space. turbo-c, and most other pc-based C
[...]
>other ways
>to solve this problem:
>  (1) use malloc. this should probably be done anyway for a 10K array
>  (2) declare arr[] globally

    or

   (3)  int main(void)
        {
            static unsigned char arr[10000]
            ...
            ...
        }

Lee Hollingworth



Tue, 06 Dec 1994 05:42:17 GMT  
 3 lines c code has stack overflow, help !

Quote:

> ...
> the problem here is stack space. turbo-c, and most other pc-based C
> compilers, set the default stack to 4K which is more than ample for
> most applications. it can be reset using a statement such as
> _stklen=16384  or some such which is compiler dependant. other ways
> to solve this problem:
>   (1) use malloc. this should probably be done anyway for a 10K array
>   (2) declare arr[] globally

> unsigned char att[10000];

> int main(void)
> {
> ..
> }

>   this will put attr[] in the data segment where you have around 64K
> to play with.

> --kyle

kyle,

I've never really understood "the stack".  Can you give me a general
description of when things go on "the stack" and when they don't?  I
have a large program that has a single routine that appears to make it
so it can't run on a PC because as soon as I get to the first line of
that routine I get a stack overflow, even with a 64K stack.  The
routine is a Newton-type equation solver which allocates a whole bunch
of arrays which I wouldn't particularly mind giving file scope but
which I wouldn't want to make global since they're part of a library.

Any advice?

Thanks,

Nick



Wed, 07 Dec 1994 04:20:48 GMT  
 3 lines c code has stack overflow, help !

[ huge local array causing a stack overflow. advice was : malloc or global ]

|>   this will put attr[] in the data segment where you have around 64K
|> to play with.
|>
|> --kyle
|
|kyle,
|
|I've never really understood "the stack".  Can you give me a general
|description of when things go on "the stack" and when they don't?

Though most implementations use a stack, this is not necessarilly the
one and only possible implementation. Just imagine a machine _without_
a hardware stack (I've seen some of them, years ago ...) A software
implemented stack is not the ideal solution there; a linked list of
activation records would do better ...

BTW Good ol' fortran didn't use a stack for the storage of its local
variables and parameters either. That's why it didn't allow recursion ...

But back to your question:

All automatic variables are stored on the stack, i.e. all _non-static_
local variables and parameters are pushed on the stack. I presume you're
working on a PC, because `even with a 64K stack' (see below) is not
that much of a stack. Try to port the GCC preprocessor to a PC ;-) That
silly program uses the alloca() function to allocate enough space on
the stack to store the _entire_ source file to be processed!

|I have a large program that has a single routine that appears to make it
|so it can't run on a PC because as soon as I get to the first line of
|that routine I get a stack overflow, even with a 64K stack.  The
|routine is a Newton-type equation solver which allocates a whole bunch
|of arrays which I wouldn't particularly mind giving file scope but
|which I wouldn't want to make global since they're part of a library.

If it allocates these arrays using the malloc() functions, there's no
problem then. If it uses non-static local arrays, there are a few
possible alternatives (like you suggested yourself:)

1: allocate them using the [mc]alloc() function and free() them afterwards;

2: define them to be static local variables. This causes these arrays to
   have a function scope, so no-one outside that function can access them;

3: define them to be static global variables. This makes these arrays
   visible, just within that particular file. So, again, no-one outside
   that file can access them.

All three alternatives cause the arrays _not_ to be stored on the stack.
If your BSS segment is too small to contain all these arrays, use the
first alternative.

|Thanks,
|
|Nick

You're welcome,




Wed, 07 Dec 1994 19:38:17 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Stack Overflow in inherited Linux C code

2. Stack Structure and Stack Overflow for CE

3. stack overflow help?

4. stack overflow help

5. Stack overflow help sought

6. C++ Builder Stack Overflow Error - Please help - Need to know how to overcome this Debugger Exception

7. Help! Stack Overflow in Recursive

8. HTML help gives a stack overflow?

9. Help! Stack Overflow in Recursive

10. need help with stack overflow

11. Stack Overflow ( HELP PLEASE )

12. VC++ 1.52 Stack Overflow problem pls Help !!!

 

 
Powered by phpBB® Forum Software