Thread: Segmentation fault (core dumped) in #c?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Segmentation fault (core dumped) in #c?

    This is my first time writing in C and I'm trying to compile and run a program in C that is supposedly standard code for dense matrix multiplication. However i'm getting this segmentation fault (core dumped) error and i have no idea what the problem is. Here is my code:
    Code:
    int main(){ 
    
    int N = 1024; 
    int x[N][N]; 
    int y[N][N]; 
    int z[N][N]; 
    int i; 
    int r; 
    int k; 
    int j; 
    
    for (i = 0; i < N; i = i+1) { 
       for (j = 0; j < N; j = j+1) 
           {r = 0; 
           for (k = 0; k < N; k = k+1){ 
              r = r + y[i][k]*z[k][j];}; 
              x[i][j] = r; 
           }; 
       }; 
    
    return 0; 
    }
    
    Last edited by hs101; 12-06-2012 at 12:43 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your arrays are 1024*1024, or 1,048,576 ints each. If each int is 4 bytes (32 bit machine), that's 4MB per array, and for 8 byte ints (64 bit machine), that's 8MB per array. You have 3 of them, for a total of 12-24 MB on your stack. Most systems limit the stack to somewhere between 1-8MB, so you are probably overflowing this. Try reducing N to something like 32 and see if that fixes the problem.

    EDIT: Please make sure your code is properly indented in the future. Also, you should also past your code as plain text, so our forum can add line numbering and syntax highlighting. That makes it easier to read, which means it's easier for us to help you.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Sorry about the coding format, I will fix it next time. My instructor required me to make N=1024. Is there anything i can do to make this work?

    Edit: Also changing N to 32 does indeed make the error go away.
    Last edited by hs101; 12-06-2012 at 12:47 PM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hs101 View Post
    Sorry about the coding format, I will fix it next time. My instructor required me to make N=1024. Is there anything i can do to make this work?
    Either move the arrays outside of main() or declare them as static. You can't put that much stuff on the stack. Hopefully your instructor will understand that, because he/she is asking for the impossible.

    EDIT: Also, using a variable (N) as the size of an array is not standard C. What you've written isn't C, it's C with non-standard extensions.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by brewbuck View Post
    EDIT: Also, using a variable (N) as the size of an array is not standard C. What you've written isn't C, it's C with non-standard extensions.
    Just to clarify, it is legal per C99 (and C11), but not for static storage duration variables, i.e. not for global or static local declaractions. Only for automatic storage duration ("regular" local) arrays.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    My professor hinted that i can use malloc() to make this possible:

    "When the array size is large, allocating the array as static one may cause segmentation
    error. You can use malloc() to allocate array elements instead."

    How would malloc() be used in this case?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Check this link: Question 6.16

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by anduril462 View Post
    Just to clarify, it is legal per C99 (and C11), but not for static storage duration variables, i.e. not for global or static local declaractions. Only for automatic storage duration ("regular" local) arrays.
    I thought that was only permitted for the first dimension of the array. Maybe I am wrong.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by brewbuck View Post
    I thought that was only permitted for the first dimension of the array. Maybe I am wrong.
    I'm not always the best at groking the standard, maybe you can take a look (section 6.7.5.2) but I think it's legal. At the very least, paragraphs 9 and 10 (examples 3 and 4) seem to show such usage as valid. Also, gcc with -stc=c99 -pedantic, which should reject any extensions and be totally ISO C99 compliant, allows it.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Quote Originally Posted by anduril462 View Post
    Check this link: Question 6.16
    Thanks for your help! This fixed my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (core dumped)
    By TimI in forum C Programming
    Replies: 4
    Last Post: 11-05-2012, 07:45 AM
  2. Segmentation Fault (core dumped)
    By Kevin Jerome in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2012, 12:58 AM
  3. Segmentation fault (core dumped)
    By jonagondo in forum C Programming
    Replies: 6
    Last Post: 01-04-2012, 03:56 PM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM