473,396 Members | 2,011 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,396 software developers and data experts.

tracking a segmentation fault

Hi,

I am trying to track a segmentation fault (EXC_BAD_ACCESS) that's
happening in a program I use on Mac OS X (not my own code). The reason
for the fault is that the default stack on Mac OS X is small. If I
call 'unlimit stack' before I start the program, there is no error.
However, I would like to avoid typing ulimit stack every time.

Examining the code indicates that 2 large static arrays are defined
outside main() as well as many array's inside main():

static int myarray[20][10000];
static int anotherarray[20][10000];

int main (int argc, char **argv)
{
int labels[20];
int names[20][10000];
float num[20][10000];
.....
So, after some reading, I understand I can add something like this to
the code, to increase the stack:

struct rlimit rlim;
getrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = RLIM_INFINITY;
setrlimit(RLIMIT_STACK, &rlim);

Unfortunately, even if I put this right after int main (int argc, char
**argv), I still get the error. If I use gdb, I immediately drop in
the error after main() starts. So I suspect the compiler is creating
all the array's first, using all the available stack.

Is there a way to modify the code so that the 'rlimit' snippit is
called before everything else happens? Or maybe I need to use
different compiler settings?
thanks,
- Koen.
Nov 13 '05 #1
7 5010
kv******@earthlink.net (Koen) wrote in
news:dd*************************@posting.google.co m:
Hi,

I am trying to track a segmentation fault (EXC_BAD_ACCESS) that's
happening in a program I use on Mac OS X (not my own code). The reason
for the fault is that the default stack on Mac OS X is small. If I
call 'unlimit stack' before I start the program, there is no error.
However, I would like to avoid typing ulimit stack every time.

Examining the code indicates that 2 large static arrays are defined
outside main() as well as many array's inside main():

static int myarray[20][10000];
static int anotherarray[20][10000];

int main (int argc, char **argv)
{
int labels[20];
int names[20][10000];
float num[20][10000];
.....
So, after some reading, I understand I can add something like this to
the code, to increase the stack:

struct rlimit rlim;
getrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = RLIM_INFINITY;
setrlimit(RLIMIT_STACK, &rlim);


This is not a C thing. I think you would be much better served
malloc()'ing the memory you need rather than increasing the stack size.

--
- Mark ->
--
Nov 13 '05 #2
bd
Koen wrote:
Hi,

I am trying to track a segmentation fault (EXC_BAD_ACCESS) that's
happening in a program I use on Mac OS X (not my own code). The reason
for the fault is that the default stack on Mac OS X is small. If I
call 'unlimit stack' before I start the program, there is no error.
However, I would like to avoid typing ulimit stack every time.

Examining the code indicates that 2 large static arrays are defined
outside main() as well as many array's inside main():

static int myarray[20][10000];
static int anotherarray[20][10000];

int main (int argc, char **argv)
{
int labels[20];
int names[20][10000];
float num[20][10000];
.....
So, after some reading, I understand I can add something like this to
the code, to increase the stack:

struct rlimit rlim;
getrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = RLIM_INFINITY;
setrlimit(RLIMIT_STACK, &rlim);

Unfortunately, even if I put this right after int main (int argc, char
**argv), I still get the error. If I use gdb, I immediately drop in
the error after main() starts. So I suspect the compiler is creating
all the array's first, using all the available stack.

Is there a way to modify the code so that the 'rlimit' snippit is
called before everything else happens? Or maybe I need to use
different compiler settings?


Changing the size of the stack is off-topic here. However, I'd change
names[][] and num[][] to be allocated from the malloc() pool - usually it's
much larger than the stack.
Nov 13 '05 #3
On 31 Oct 2003 11:12:18 -0800, in comp.lang.c , kv******@earthlink.net
(Koen) wrote:
Hi,

I am trying to track a segmentation fault (EXC_BAD_ACCESS) that's
happening in a program I use on Mac OS X (not my own code). The reason
for the fault is that the default stack on Mac OS X is small.


most implementations limit the amount of auto variables you can have.
You are obviously exceeding that.

There may be a system specific way to avoid the problem (ask in an OSX
programming group please), but the usual C answer is NOT to use huge
fixed-size arrays, but malloc the memory instead.

int *p = malloc(200000 * sizeof *p);
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #4
Mark McIntyre <ma**********@spamcop.net> wrote in message news:<20********************************@4ax.com>. ..
There may be a system specific way to avoid the problem (ask in an OSX
programming group please),
Ahh - sorry, I didn't realize the setrlimit call was OS specific.
but the usual C answer is NOT to use huge
fixed-size arrays, but malloc the memory instead.

Anyway, it got solved by creating a new main() function which
increases the stacksize and then calls the original main() function.

So, a semi-C solution and more or less on-topic :)
- Koen.
Nov 13 '05 #5
Koen <kv******@earthlink.net> wrote:
Hi,
For future reference, this would have been best posted in
comp.unix.programmer. I'd set follow-ups to go there, but I don't
know how with the tool I"m using.
I am trying to track a segmentation fault (EXC_BAD_ACCESS) that's
happening in a program I use on Mac OS X (not my own code). The reason
[snip]
static int myarray[20][10000];
static int anotherarray[20][10000]; int main (int argc, char **argv)
{
int labels[20];
int names[20][10000];
float num[20][10000];
..... So, after some reading, I understand I can add something like this to
the code, to increase the stack: struct rlimit rlim;
getrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = RLIM_INFINITY;
setrlimit(RLIMIT_STACK, &rlim);


OK, since we're in comp.lang.c and tradition says we must be pedantic,
I will start off by pointing out that the C language has no concept of
a stack. That's an OS or a machine architecture concept. There are
some architectures where this would run just fine because local
variables are allocated as part of the overall memory the program
uses (System\370 works this way) and the PowerPC could be made to
run this way if it weren't for the ABI conventions.

But, practically speaking, your implementation has a stack, and yes
you're most likely trashing it long before you get to call setrlimit.
The entrance to main is going to allocate the stack space necessary
for those locals and you end up with your fault.

There are three ways to solve this that spring immediately to mind:

1) Use malloc for names and num. 'int **names; float **num;' and
malloc them properly.
2) Move names and num to be outside of main().
Yes, you're making them global.
3) Look up exec(3). Using your setrlimit idea, set the stack to
RLIM_INFINITY and then exec the existing program unmodified.
Read the man page on exec to see why this is.

[snip]

Hope this helps.

dbtid

Nov 13 '05 #6
On Wed, 05 Nov 2003 14:07:50 GMT, in comp.lang.c ,
db***@NOSPAM.dbtid.is-a-geek.org (DBTID) wrote:
Koen <kv******@earthlink.net> wrote:
Hi,


For future reference, this would have been best posted in
comp.unix.programmer. I'd set follow-ups to go there, but I don't
know how with the tool I"m using.


http://www.cs.unca.edu/~edmiston/handouts/tinfaq.html

might be useful for you in that case, esp 3.12

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #7
Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 05 Nov 2003 14:07:50 GMT, in comp.lang.c ,
db***@NOSPAM.dbtid.is-a-geek.org (DBTID) wrote: http://www.cs.unca.edu/~edmiston/handouts/tinfaq.html might be useful for you in that case, esp 3.12 --
Mark McIntyre


Thanks, Mark!
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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.