Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Segfault due to array declaration!

Segfault due to array declaration!
Thread Tools
King_Rat
Dedicated MacNNer
Join Date: Aug 2001
Location: Socorro, NM
Status: Offline
Reply With Quote
Dec 4, 2005, 01:01 PM
 
I have been having the strangest problem, and I am wondering if it is a computer hardware problem, OS X software problem, compiler error, or something else. I have been working on a C program that uses a large 3d array of unsigned chars (yes, I know that is strange but there is a reason for it), and it has been segfaulting. I used gdb to locate the segfault and gdb said that it was on the first { after int main()! I finally isolated the problem as being the array itself, and wrote a couple test programs to verify it (I am running the programs from the terminal). Here is what happens:


% more test.c
int main()
{
unsigned char breadbox[2222][1966][2];
}
% gcc test.c
% ./a.out
Segmentation fault
% more test2.c
int main()
{
int breadbox[2222][1966][2];
}
% gcc test2.c
% ./a.out
Segmentation fault
%


I am programing on a dual 800 quicksilver; it has a rev B motherboard (it was replaced a few years ago), the rev A processors, 756MB RAM (more than enough free for an array of that size), and is running OS 10.4. I ran the silly apple hardware test that came with the computer and it passed. Any ideas what is the source of the problem and what I can do to fix it?
-King Rat
     
saddino
Mac Enthusiast
Join Date: Nov 2003
Status: Offline
Reply With Quote
Dec 4, 2005, 01:20 PM
 
You're blowing the stack by attempting to declare a ~8MB array. Instead, create your array dynamically (on the heap), using malloc().
     
King_Rat  (op)
Dedicated MacNNer
Join Date: Aug 2001
Location: Socorro, NM
Status: Offline
Reply With Quote
Dec 4, 2005, 02:08 PM
 
I could try to malloc it. However, there are problems with mallocing 3d arrays. If you want to be able to index them as 3d arrays (i.e. ArrayName[x][y][x]) you end up mallocing them in a bunch of smaller chunks, so you have an array of smaller arrays instead of one big array. If I did that it could kill performance; when processing a large list of items in a liner fashion you want to have the list in a contiguous part of memory because of cache paging issues. I suppose that I could make it into a 1d array and do some annoying addressing math, but that would require more rewriting of my program than I have the time to do right now.
I also tied running it on a G5 running 10.4 and had the same problem, but it works fine on a red hat 9 machine. I think that you are right about it being a stack overflow issue. Right now I am looking at making it into a global variable; at a glance that seems to take care of the problem. On the other hand, I might just finish this project on some other system. In the end it is not a mac that I will be running the program on.

Anyway, I will check around on the stack issue; thanks!
-King Rat
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Dec 5, 2005, 12:10 PM
 
Originally Posted by King_Rat
If I did that it could kill performance; when processing a large list of items in a liner fashion you want to have the list in a contiguous part of memory because of cache paging issues. I suppose that I could make it into a 1d array and do some annoying addressing math, but that would require more rewriting of my program than I have the time to do right now.
It will take 5 minutes to write a preprocessor macro to translate [x][y][z] coordinates into a 1 dimensional array. Don't worry about cache issues yet. Concentrate on correctness first, and performance second.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Dec 5, 2005, 12:34 PM
 
Originally Posted by King_Rat
I could try to malloc it. However, there are problems with mallocing 3d arrays. If you want to be able to index them as 3d arrays (i.e. ArrayName[x][y][x]) you end up mallocing them in a bunch of smaller chunks, so you have an array of smaller arrays instead of one big array.
unsigned char ***breadbox = malloc(sizeof(unsigned char) * 2222 *1966 * 2) should be effectively the same as your code, only not using the stack.

EDIT: Pointers and arrays are equivalent in C, so you can still address it with array semantics.
( Last edited by Chuckit; Dec 6, 2005 at 06:26 PM. )
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
King_Rat  (op)
Dedicated MacNNer
Join Date: Aug 2001
Location: Socorro, NM
Status: Offline
Reply With Quote
Dec 10, 2005, 01:21 PM
 
I finally have the time to get back to work on this project! I had a couple of final exams that I had to rush off and take care of .
Anyway, as suggested, I switched the array to dynamic allocation; that took care of the crashing when you start the program problem. Oh, and Samm is right about making it work before making it run quickly, but thanks to Chuckit's advice I was still able to get the array declared as a contiguous of RAM. Now that the program will run without crashing I have to track down the rest of the bugs... Thanks for all the help!

Note: I hope that this does not double or triple post; the first 2 times I attempted to post it the forums gave me a database error. Looks like I am not the only one that will need to spend some time fixing a broken program!
( Last edited by King_Rat; Dec 10, 2005 at 05:36 PM. Reason: Fixed spelling)
-King Rat
     
King_Rat  (op)
Dedicated MacNNer
Join Date: Aug 2001
Location: Socorro, NM
Status: Offline
Reply With Quote
Dec 10, 2005, 01:22 PM
 
Removed duplicate post...
( Last edited by King_Rat; Dec 10, 2005 at 05:33 PM. )
-King Rat
     
King_Rat  (op)
Dedicated MacNNer
Join Date: Aug 2001
Location: Socorro, NM
Status: Offline
Reply With Quote
Dec 10, 2005, 01:23 PM
 
Removed duplicate post...
( Last edited by King_Rat; Dec 10, 2005 at 05:32 PM. )
-King Rat
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 11:09 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,