Re: How to avoid stack overflow in C????



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



Relevant Pages

  • Re: Alpha remembrance day
    ... DEC's disk drive business, they were starting to catch on, but since ... to integrate a TCP/IP stack into on OS? ... UNIX Server/Workstation. ... IBM courted him for a long while ...
    (comp.os.vms)
  • Re: Signs of Maccie desperation!
    ... they cannot be implemented in UNIX? ... Network mapping magically detects what your ... Same with the new stack. ... interface, and a ton load of new technologies in the file system, kernel, ...
    (comp.sys.mac.advocacy)
  • Re: How can I detect a carriage return using java.net
    ... The stack used by DEC Unix on the alphaserver did the same - and that was a Mach based implementation. ... If you got an error you can report it, but this doesn't affect the logic unless it was EAGAIN or EINTR, which both indicate that the recv() returned without reading any valid data but the connection is still intact. ...
    (comp.lang.java.programmer)
  • Re: UDP checksum calculation - uses lenght twice ?
    ... TCP headers have no relation with Unixstack because TCPIP stack was ... Unix long time was single computer Os ... > for UDP), and the IP header checksum, which is apparently ...
    (microsoft.public.win32.programmer.networks)
  • Re: If Macs have no spyware....
    ... >had made a complete code review of its operating system and removed all ... and writing new data into those memory locations would ... >but when the data exists on the stack, it can cause very large problems. ... >location that needs to be written in place of the correct execution ...
    (comp.sys.mac.advocacy)