Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
I declare a number of arrays as float[200]. The programs loops close to 100 times and the crashes with a segmentation error. If I calloc the arrays, the program runs. Why?
 
You should definitely post code, as robbieduncan said. I can hazard a guess that if you have many arrays of 200 floats (each are likely 800 bytes), you may be overflowing the stack. When you use calloc/malloc/etc. you get space from the heap (there is much more space available on the heap than the stack). When you declare things locally, they get allocated on the stack. It's possible that if you have a few MB worth of stack variables that you are overwhelming the space available for the stack, running into the heap (they grow in opposite directions), hitting an area of memory you don't have access to yet (the system hasn't allocated any memory for you there) and your program crashes. Again, this is a wild guess based on things i've seen before (massive local structs or arrays overwhelming the stack).

Post your code (even if you can't post "everything", post enough that we can compile and run and get the error) and we'll be glad to try to help further.

-Lee
 
I declare a number of arrays as float[200]. The programs loops close to 100 times and the crashes with a segmentation error. If I calloc the arrays, the program runs. Why?

1. Because there's a bug in your code.
2. Because there is no bug in the code using calloc, or it runs without error by coincidence.

But why don't you use XCode, start a debug version, run it with debugger, and check what your code does?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.