c memory question

Discussion in 'Mac Programming' started by farmerdoug, Jan 11, 2010.

  1. farmerdoug macrumors 6502a

    Sep 16, 2008
    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?
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    Post the actual code. Without it we're just guessing.
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    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.

  4. ChrisA macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    Even beter write a test case that shows the problems with as few lines of code as you can, then post that. Just cut out everything you can and post what's left.
  5. gnasher729 macrumors P6


    Nov 25, 2005
    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?

Share This Page