How to avoid stack overflow in C????

A

amit.atray

Environement : Sun OS + gnu tools + sun studio (dbx etc)

having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)

say for ex.
char a[8000];

(this type of code and other complex mallc/free etc is used frequently
and total approx 450 files, each file is of approx/avg 1000 line,
multithreaded , socket code)

When i tried to add few new variables of some size (say 1000 bytes)
its was crashing. But later I assigned few variable (a[8000]) through
calloc call, I was able to add other varibles also, This time code was
working fine without crashing down.

It seems i am getting stack overflow if i use char a[8000]
but when i assign same memory at runtime through calloc, it is working
fine.

1. Is this really a stack overflow.????
2. How to detect stack overflow in old big C code??? (and Fix it)
3. Tips to avoid futher, stack overflow without much change
(hardwork).....
4. How to make decision of the memory to use dynamically or automatic.
(i.e. if total program is having big number of variables, should i use
all big-size variable dynamically ? )
 
Y

yunyuaner

Environement : Sun OS + gnu tools + sun studio (dbx etc)

having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)

say for ex.
char a[8000];

(this type of code and other complex mallc/free etc is used frequently
and total approx 450 files, each file is of approx/avg 1000 line,
multithreaded , socket code)

When i tried to add few new variables of some size (say 1000 bytes)
its was crashing. But later I assigned few variable (a[8000]) through
calloc call, I was able to add other varibles also, This time code was
working fine without crashing down.

It seems i am getting stack overflow if i use char a[8000]
but when i assign same memory at runtime through calloc, it is working
fine.

1. Is this really a stack overflow.????
2. How to detect stack overflow in old big C code??? (and Fix it)
3. Tips to avoid futher, stack overflow without much change
(hardwork).....
4. How to make decision of the memory to use dynamically or automatic.
(i.e. if total program is having big number of variables, should i use
all big-size variable dynamically ? )

Firstly, to avoid stack overflow in the case you mentioned, you can
adjust your compiler for bigger stack size. But you'd better store big
scale data in either data segment(which means to use static variable
or global variable) or in heap(which means use malloc etc.)
 
S

shuLhan

Environement : Sun OS + gnu tools + sun studio (dbx etc)
having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)
say for ex.
char a[8000];
(this type of code and other complex mallc/free etc is used frequently
and total approx 450 files, each file is of approx/avg 1000 line,
multithreaded , socket code)
When i tried to add few new variables of some size (say 1000 bytes)
its was crashing. But later I assigned few variable (a[8000]) through
calloc call, I was able to add other varibles also, This time code was
working fine without crashing down.
It seems i am getting stack overflow if i use char a[8000]
but when i assign same memory at runtime through calloc, it is working
fine.
1. Is this really a stack overflow.????
2. How to detect stack overflow in old big C code??? (and Fix it)
3. Tips to avoid futher, stack overflow without much change
(hardwork).....
4. How to make decision of the memory to use dynamically or automatic.
(i.e. if total program is having big number of variables, should i use
all big-size variable dynamically ? )

i think this is the answer for number 1, 3 and 4
Firstly, to avoid stack overflow in the case you mentioned, you can
adjust your compiler for bigger stack size. But you'd better store big
scale data in either data segment(which means to use static variable
or global variable) or in heap(which means use malloc etc.)

if you want to detect what your program does with memory,
i suggest you to use valgrind.
 
R

raxitsheth2000

On 2月13æ—¥, 下åˆ4æ—¶46分, (e-mail address removed) wrote:
Environement : Sun OS + gnu tools + sun studio (dbx etc)
having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)
say for ex.
char a[8000];
(this type of code and other complex mallc/free etc is used frequently
and total approx 450 files, each file is of approx/avg 1000 line,
multithreaded , socket code)
When i tried to add few new variables of some size (say 1000 bytes)
its was crashing. But later I assigned few variable (a[8000]) through
calloc call, I was able to add other varibles also, This time code was
working fine without crashing down.
It seems i am getting stack overflow if i use char a[8000]
but when i assign same memory at runtime through calloc, it is working
fine.
1. Is this really a stack overflow.????
2. How to detect stack overflow in old big C code??? (and Fix it)
3. Tips to avoid futher, stack overflow without much change
(hardwork).....
4. How to make decision of the memory to use dynamically or automatic.
(i.e. if total program is having big number of variables, should i use
all big-size variable dynamically ? )

i think this is the answer for number 1, 3 and 4
Firstly, to avoid stack overflow in the case you mentioned, you can
adjust your compiler for bigger stack size. But you'd better store big
scale data in either data segment(which means to use static variable
or global variable) or in heap(which means use malloc etc.)

if you want to detect what your program does with memory,
i suggest you to use valgrind.- Hide quoted text -
AFAIK, Valgrind only checks memory on HEAP.

--raxit
 
R

raxitsheth2000

Environement : Sun OS + gnu tools + sun studio (dbx etc)
having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)
say for ex.
char a[8000];
(this type of code and other complex mallc/free etc is used frequently
and total approx 450 files, each file is of approx/avg 1000 line,
multithreaded , socket code)
When i tried to add few new variables of some size (say 1000 bytes)
its was crashing. But later I assigned few variable (a[8000]) through
calloc call, I was able to add other varibles also, This time code was
working fine without crashing down.
It seems i am getting stack overflow if i use char a[8000]
but when i assign same memory at runtime through calloc, it is working
fine.
1. Is this really a stack overflow.????
2. How to detect stack overflow in old big C code??? (and Fix it)
3. Tips to avoid futher, stack overflow without much change
(hardwork).....
4. How to make decision of the memory to use dynamically or automatic.
(i.e. if total program is having big number of variables, should i use
all big-size variable dynamically ? )

Firstly, to avoid stack overflow in the case you mentioned, you can
adjust your compiler for bigger stack size. But you'd better store big
scale data in either data segment(which means to use static variable
or global variable) or in heap(which means use malloc etc.)
this may be good option but Caution...!
1. while using global and/or static variable in multithreaded program.

2. auto variable has one benefit that you need not to free the
variable, but in case of mallc/related you (and the person who is
doing code maintanance in future) need to take care of Memory Leak/
Heap Corruption/Forgot to Free memory/Double free etc

3. I think one of the PRACTICAL thing is to do/redo Unit Testing
older code thoroughly to avoid frequent crash.



--Raxit
 
J

Joe Estock

Environement : Sun OS + gnu tools + sun studio (dbx etc)

Your question would be better answered in comp.sys.sun (or another
newsgroup dedicated to your platform or perhaps a Unix specific
newsgroup such as comp.os.unix.programming) as it is venturing on the OT
line here. Regardless I will attempt to answer the best I can based upon
my experiences, however YMMV. Follow-up's set to
comp.os.unix.programming as it is a more relevant group for your question.
having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)

say for ex.
char a[8000];

(this type of code and other complex mallc/free etc is used frequently
and total approx 450 files, each file is of approx/avg 1000 line,
multithreaded , socket code)

When i tried to add few new variables of some size (say 1000 bytes)
its was crashing. But later I assigned few variable (a[8000]) through
calloc call, I was able to add other varibles also, This time code was
working fine without crashing down.

Define "crashing". I can allocate 25 bytes and write 3,500 bytes past
the end of the allocated memory and make my program "crash". There are
very large differences between a segmentation fault, stack fault,
[insert your abort code here].
It seems i am getting stack overflow if i use char a[8000]
but when i assign same memory at runtime through calloc, it is working
fine.

Are you sure that it is crashing due to a stack overflow? If so, your
operating system may have a way to increase the stack size, however on
unix (and unix like systems) you may need to recompile the kernel (or
reboot your machine and pass a larger value for the maximum stack size,
the details of which I do not readily know).
1. Is this really a stack overflow.????

Perhaps. You would need to provide more detail in order for me to
confirm (a small example which exhibits the same behavior would be helpful).
2. How to detect stack overflow in old big C code??? (and Fix it)
3. Tips to avoid futher, stack overflow without much change
(hardwork).....

I don't know of any off-hand, however there are several commercial
utilities available, including several free utilities. A code profiler
would certainly be a good start. I don't know of any magical utility
that can automatically fix your code with little to no human
interaction. The obvious one line answer would be "don't write bad code
in the first place".
4. How to make decision of the memory to use dynamically or automatic.
(i.e. if total program is having big number of variables, should i use
all big-size variable dynamically ? )

That question would be better answered by some of the other c.l.c
regulars like Keith Thompson or Richard Heathfield (there are others as
well, however these are the first two that come to mind).
 
M

Mark McIntyre

It seems i am getting stack overflow if i use char a[8000]
but when i assign same memory at runtime through calloc, it is working
fine.

Most implementations have a limit on how much memory is available for
automatic variables. Often this memory is on the stack, though C
doesn't require it to be there.
1. Is this really a stack overflow.????

On your system, yes.
2. How to detect stack overflow in old big C code??? (and Fix it)

You can't. Your compiler /may/ complain at compile time, or yur
application may just crash at run time
3. Tips to avoid futher, stack overflow without much change

use malloc.
4. How to make decision of the memory to use dynamically or automatic.

Whenever you have to create large objects, use malloc.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Matt Kowalczyk

Environement : Sun OS + gnu tools + sun studio (dbx etc)

having some Old C-Code (ansi + KR Style) and code inspection shows
some big size variable (auto) allocated (on stack)

say for ex.
char a[8000];

(this type of code and other complex mallc/free etc is used frequently
and total approx 450 files, each file is of approx/avg 1000 line,
multithreaded , socket code)

When i tried to add few new variables of some size (say 1000 bytes)
its was crashing. But later I assigned few variable (a[8000]) through
calloc call, I was able to add other varibles also, This time code was
working fine without crashing down.

It seems i am getting stack overflow if i use char a[8000]
but when i assign same memory at runtime through calloc, it is working
fine.

1. Is this really a stack overflow.????
2. How to detect stack overflow in old big C code??? (and Fix it)
3. Tips to avoid futher, stack overflow without much change
(hardwork).....
4. How to make decision of the memory to use dynamically or automatic.
(i.e. if total program is having big number of variables, should i use
all big-size variable dynamically ? )


See if you have getrlimit(2) and setrlimit(2) functions on your OS. getrusage()
may also be helpful. The function allows you to get the current resource usage
for a process.

The man page for getrlimit states:

RLIMIT_STACK
The maximum size of the process stack, in bytes. Upon reaching
this limit, a SIGSEGV signal is generated. To handle this sig-
nal, a process must employ an alternate signal stack (sigalt-
stack(2)).

-Matt Kowalczyk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top