473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to avoid stack overflow in C????


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
7 22003
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Travis Pupkin | last post by:
Hi, I'm getting the Stack Overflow error on the request.form from a large TEXTAREA submission. Actually, the problem is _I'm_ not getting it, only my client is, and we're testing the exact...
4
by: Allen | last post by:
Hi all, What are some different approaches to dealing with stack overflows in C++? I'm especially interested in approaches concerned with speed for infrequent overflows. -- Best wishes,...
3
by: ip4ram | last post by:
I am quite puzzled by the stack overflow which I am encountering.Here is the pseudocode //define stack structure //function operating on stack void my_stack_function( function parameters) {...
7
by: Aguilar, James | last post by:
Hello all, To begin, yes, this -is- a homework assignment. However, it is for my Algorithms class, and my instructor has given us explicit permission to use "expert" groups like newsgroups, so...
11
by: Yvad | last post by:
When I encounter software crash, the software always pop-up something like " The instruction at "0x1000a1eb" referenced memory at "0x000000c0". The memory could not be "read"". Then Visual C++...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
2
by: David W. Walker | last post by:
I am attempting to port a C code that runs OK on a number of Linux and Unix machines to Windows XP using Visual Studio C++. I have set the program up as a console application, but when I try to run...
4
by: asit | last post by:
we kno during call of a subroutine, the address of next instruction( from which the execution is supposed to start after the subroutine is executed ) is stored in stack. in case of recursion (e.g....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.