Re: How to avoid stack overflow in C????
- From: Joe Estock <jestock@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 13 Feb 2007 08:00:11 -0600
amit.atray@xxxxxxxxx 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).
.
- References:
- How to avoid stack overflow in C????
- From: amit . atray
- How to avoid stack overflow in C????
- Prev by Date: Re: free()
- Next by Date: Re: returning the Fibonacci string separated by comma
- Previous by thread: Re: How to avoid stack overflow in C????
- Next by thread: Re: How to avoid stack overflow in C????
- Index(es):
Relevant Pages
|