473,700 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22044
On 2月13日, 下午4时46分, amit.at...@gmai l.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...@gmai l.com" <yunyua...@gmai l.comwrote:
On 2鏈13鏃, 涓嬪崍4鏃46鍒 , amit.at...@gmai l.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...@gmai l.comwrote:
On Feb 13, 4:23 pm, "yunyua...@gmai l.com" <yunyua...@gmai l.comwrote:


On 2鏈13鏃, 涓嬪崍4鏃46鍒 , amit..at...@gma il.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...@gmai l.com" <yunyua...@gmai l.comwrote:
On 2鏈13鏃, 涓嬪崍4鏃46鍒 , amit.at...@gmai l.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********@gmai l.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.pr ogramming) 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.pr ogramming 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********@gmai l.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********@gmai l.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
1628
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 same content in the post. First, is there a simple way around this error other than running the long function found here http://www.pstruh.cz/tips/detpg_largepost.htm.
4
6717
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, Allen
3
2710
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) { do required stuff if(some conditions obeyed)
7
7072
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 if that's your only reason not to help, please do. Otherwise, I guess it's OK. But, just remember, I'm not asking you to do my work for me, just to point out my error. My problem is not with the algorithm itself (standard divide and conquer on...
11
3816
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++ will ask me whether to debug the program(in assembly). My friend told me it is mostly cause by stack overflow. Is he right? And is there any document on how to debug it? And how to avoid this bug in C and C++?
19
3137
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 (nested), three list views, one tree control with up to 30,000 nodes, maybe 15 comboboxes (half of which have a large recordset as rowsource), 20 or so buttons and around 30 text boxes (not to mention the images, labels, etc and around 1000 lines...
4
9053
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. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic variable in main( ), and assume this is a fairly good indicator of my base address. Then, I can...
2
2890
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 it I get a stack overflow: Unhandled exception at 0x0041e715 in NewMD.exe: 0xC00000FD: Stack overflow. The call stack is: NewMD.exe!_chkstk() Line 91 NewMD.exe!mainCRTStartup() Line 259 +0x19
4
1728
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. we are sorting an array having length one million using heap sort), we have to call a particular subroutine thousands time. Is their any chance of stack overflow ?? if yes how can we avoid ?? here we have no choice other than subroutine..
0
8642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9209
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9069
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8918
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7804
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project梡lanning, coding, testing, and deployment梬ithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5900
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4400
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2385
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2024
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.