Initialising a 3D array - Segmentation fault

This is a discussion on Initialising a 3D array - Segmentation fault within the C Programming forums, part of the General Programming Boards category; Can anyone tell me where the error is here? I get a Segmentation Fault error upon running the code (it ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Cool Initialising a 3D array - Segmentation fault

    Can anyone tell me where the error is here? I get a Segmentation Fault error upon running the code (it compiles fine). Cheers.

    The variable "size" and the constants are all initialised/defined properly.

    Code:
       int array[MAX_HOUR - MIN_HOUR + 1][MAX_MINUTE - MIN_MINUTE + 1][size];
    
       for (i = 0; i <= MAX_HOUR - MIN_HOUR; i++) {
          for (j = 0; j <= MAX_MINUTE - MIN_MINUTE; j++) {
             for (k = 0; k < size; k++) {
                array[i][j][k] = 0;
             }
          }
       }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What do you have for the values of MAX_HOUR, etc. ? Are these defines or what?

    3D arrays really eat up a lot of memory fast. May be crashing the stack.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Yeah, they are defines.

    #define MAX_HOUR 12
    #define MIN_HOUR 6
    #define MAX_MINUTE 30
    #define MIN_MINUTE 0

    size is just an integer variable. By the time this code is run, size = 10.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm running your code with no problems - but I had to make size a define or constant - required for my compiler to make static arrays.

    Are you getting any warnings? Maybe size is NOT 10 at this point in the program?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    You're right, the error occurs because "size" is being accessed. Not because of the code itself. I tried printing the value of size but get the same segmentation error.

    I'll investigate further.

    Thanks heaps for your help.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome, and good hunting.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    and don't cross-post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic array segmentation fault
    By Argentius in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2007, 05:50 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Segmentation fault with array?
    By whiphub in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 02:51 PM
  4. Replies: 3
    Last Post: 04-19-2004, 07:42 PM
  5. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 08:03 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21