Thread: Segmentation fault question

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41

    Segmentation fault question

    I am working on some code that multiplies 2 matrices in parallel. I statically allocate memory for the matrices so that each matrix has the memory to be 2304x2304. When I compile and run I get a segmentation fault error and the program quits. However, when I switch the dimensions to 100x100 the program runs fine. Can anyone think of why this might happen?

    I first thought maybe I should dynamically allocate memory but it seems that when I need to allocate memory for a 2304x2304 matrix I will still run into this problem.

    Maybe I am looking at this from a wrong angle. Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Q. What's 2304 * 2304 * sizeof( yourdatatype )?
    A. Too big.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    That's a ~21MB array, and you have two of them (2304 * 2304 * 4 * 2). An array this large should be gotten from the heap.

    We would be guessing on the error without seeing your code.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Ahh I just realized I don't need to allocate all that memory. Since I am doing this in parallel I only need to allocate memory for (2304/max_process_count)x(304/max_process_count) matrix, which in my case is bounded by 50x50.

    Briefly, what is heap? I just looked it up on Wikipedia but it didn't really make sense (I'm not a computer guy). I see that it is a tree based data structure and from your comment I take it that it is used for very large data sets. Is heap stored on the hard disk or in memory? Naively I would guess the hard disk but then again I have no clue what I am talking about.

    Thanks for the fast responses!

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can think of Stack storage are being part of your program that is used for variables you have defined in your program.

    Heap storage (aka, main memory, RAM, dynamically obtained storage at runtime, whatever you want to call it), is storage you ask the operating system to give you at runtime via malloc() and malloc()'s sister functions.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  3. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  4. Segmentation Fault?
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-10-2008, 07:26 AM
  5. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM