Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to avoid stack overflow in C????

Reply
Thread Tools

How to avoid stack overflow in C????

 
 
amit.atray@gmail.com
Guest
Posts: n/a
 
      02-13-2007

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

 
Reply With Quote
 
 
 
 
yunyuaner@gmail.com
Guest
Posts: n/a
 
      02-13-2007
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.)

 
Reply With Quote
 
 
 
 
shuLhan
Guest
Posts: n/a
 
      02-13-2007
On Feb 13, 4:23 pm, "yunyua...@gmail.com" <yunyua...@gmail.com> wrote:
> 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.

 
Reply With Quote
 
raxitsheth2000@yahoo.co.in
Guest
Posts: n/a
 
      02-13-2007
On Feb 13, 2:56*pm, "shuLhan" <m.shul...@gmail.com> wrote:
> On Feb 13, 4:23 pm, "yunyua...@gmail.com" <yunyua...@gmail.com> wrote:
>
>
>
>
>
> > 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 -



 
Reply With Quote
 
raxitsheth2000@yahoo.co.in
Guest
Posts: n/a
 
      02-13-2007
On Feb 13, 2:23*pm, "yunyua...@gmail.com" <yunyua...@gmail.com> wrote:
> 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

 
Reply With Quote
 
Joe Estock
Guest
Posts: n/a
 
      02-13-2007
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).
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-13-2007
On 13 Feb 2007 00:46:00 -0800, in comp.lang.c ,
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
 
Reply With Quote
 
Matt Kowalczyk
Guest
Posts: n/a
 
      02-14-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
large array?? how to avoid stack overflow? A.E lover C Programming 5 01-14-2009 01:28 AM
Why stack overflow with such a small stack? Kenneth McDonald Ruby 7 09-01-2007 04:21 AM
avoid buffer overflow using sprintf? Susan Rice C++ 3 07-04-2006 05:02 AM



Advertisments