On Thu, Jun 19, 2003, Orna Agmon wrote about "[Haifux] stack size":
> Hello eveyone,
> 
> I was wondering if there was anything that detected C stack overflow in a 
> nice way (i.e., not by having to cut down my program until I stop getting 
> a segmentation fault on the first line of main).

Theoretical but useless answer:
You can catch the SIGSEGV signal to see when you're out of stack (but at that
point you can't do much). You can increase the stack-size ulimit (by default,
8MB) to something much larger.

Real answer:
You're doing something wrong. Huge data structures do not belong on the
stack, which is by definition a small area used for temporary allocation
of small things during function calls (automatic variables, parameters,
and sometimes a little more).
Huge data structures belong on the heap. The heap is a huge place, basically
the entire address space minus the stack (hmm, and text (code) and kernel
mapping), and is designed for managing large data structures dynamically.

> typedef struct{
>  /*a huge struct definition*/
> } a_t;
> 
> int main(int argc, char *argv){
>   a_t a;
>   return 0;
> }

Don't do this!
Do a_t *a = (a_t*) malloc(sizeof(a_t));

> BTW, it is not a problem of lack of memory- the same struct can be 
> allocated on the heap.

Right. And this is what you must do. The stack has its uses, but not what
you're trying to do.

-- 
Nadav Har'El                        |     Thursday, Jun 19 2003, 19 Sivan 5763
[EMAIL PROTECTED]             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |If you tell the truth, you don't have to
http://nadav.harel.org.il           |remember anything.

--------------------------------------------------------------------------
Haifa Linux Club Mailing List (http://www.haifux.org)
To unsub send an empty message to [EMAIL PROTECTED]


Reply via email to