By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,506 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,506 IT Pros & Developers. It's quick & easy.

How to avoid stack overflow in C????

P: n/a

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 ? )

Feb 13 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 2月13日, 下午4时46分, amit.at...@gmail.com 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 ? )
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.)

Feb 13 '07 #2

P: n/a
On Feb 13, 4:23 pm, "yunyua...@gmail.com" <yunyua...@gmail.comwrote:
On 2鏈13鏃, 涓嬪崍4鏃46鍒, amit.at...@gmail.com 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.

Feb 13 '07 #3

P: n/a
On Feb 13, 2:56*pm, "shuLhan" <m.shul...@gmail.comwrote:
On Feb 13, 4:23 pm, "yunyua...@gmail.com" <yunyua...@gmail.comwrote:


On 2鏈13鏃, 涓嬪崍4鏃46鍒, amit..at...@gmail.com 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
- Show quoted text -

Feb 13 '07 #4

P: n/a
On Feb 13, 2:23*pm, "yunyua...@gmail.com" <yunyua...@gmail.comwrote:
On 2鏈13鏃, 涓嬪崍4鏃46鍒, amit.at...@gmail.com 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 ? )

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

Feb 13 '07 #5

P: n/a
am********@gmail.com wrote:
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).
Feb 13 '07 #6

P: n/a
On 13 Feb 2007 00:46:00 -0800, in comp.lang.c , am********@gmail.com
wrote:
>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
Feb 13 '07 #7

P: n/a
am********@gmail.com 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 ? )

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
Feb 14 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.