Thread: Can you have an array in a structure that has a 2D array in a lower level structure

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    3

    Can you have an array in a structure that has a 2D array in a lower level structure

    Hey guys, I'm currently in an intro to C Programming course at university and we have to write some code for a maze solving algorithm for an input text file. The code below is just the code to read the maze input and then print it out with each symbol doubled along the row. Just ignore all the constants and function prototypes that aren't used, since I cut off the rest of the program to make it simpler. This part of the program itself compiles fine and prints out the right output but as soon as I take the comment marks off of the 2D array in the first structure, I get a segmentation fault. This is making me wonder if it is possible to have a 2D array within a lower-level structure? If it is, what is the proper way of coding it? I'd appreciate any comments. Sorry if my coding isn't up to par, I'm still pretty new at this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAXSIZE 100
    #define NOGO '#'
    #define PASS '.'
    #define NEXTCELL '\n'
    #define REACHABLE '+'
    #define UNREACHABLE '-'
    #define NOCOST 0
    #define EMPTYCOST -1
    
    
    #define UNDERLINE "======="
    
    
    typedef struct {
            char type;
            char reach;
            int exitcol, exitrow;
            int startcol, startrow;
            /* int costs[MAXSIZE-1][MAXSIZE-1]; */
    } mazecell_t;
    
    
    typedef struct {
            int nrows;
            int ncolumns;
            int nentrances, entcount;
            int nexits, exitcount;
            int XP, YP; /* position placeholders */
            int hassoln;
            int costcount;
            int ncostarrays, costarraycount;
            mazecell_t maze[MAXSIZE-1][MAXSIZE-1];
            mazecell_t list[MAXSIZE-1];
            
    } maze_t;
    
    
    void read_maze(maze_t *M);
    void print_stage_one(maze_t *M);
    void maze_prep(maze_t *M);
    void assign_reachability(maze_t *M);
    void print_stage_two(maze_t *M);
    
    
    int
    main(int argc, char *argv[]) {
            
            maze_t M;
        
            read_maze(&M);
            print_stage_one(&M);
       
            return 0;
    }
    
    
    void
    read_maze(maze_t *M){
        
        int i, j, temp;
        
        for(i=0; i<(MAXSIZE-1); i++){
            /* no maze or end of maze */
            if ((temp=getchar())==EOF){
                break;
            }
            else {
                j=0;
                /*fill maze rows*/
                while (temp!=NEXTCELL){
                    if(temp==NOGO){
                        M->maze[i][j].type = temp;
                    }
                    if(temp==PASS){
                        M->maze[i][j].type = temp;
                    }
                    j++;
                    temp=getchar();
                }
            }
        }
        M->ncolumns = j;
        M->nrows = i;
        return;
    
    
    }
    
    
    void 
    print_stage_one(maze_t *M){
        
        int x, y, i, j;
    
    
        x = M->ncolumns;
        y = M->nrows;
        
        printf("Stage 1\n%s\n", UNDERLINE);
        printf("maze has %d rows and %d columns\n", y, x);
        
        for(i=0; i<y; i++){
            for(j=0; j<x; j++){
                if((M->maze[i][j].type)==NOGO){
                    printf("%c%c", NOGO, NOGO);
                }
                if((M->maze[i][j].type)==PASS){
                    printf("%c%c", PASS, PASS);
                }
            }
            printf("\n");
        }
        return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if it is possible to have a 2D array within a lower-level structure?
    Yes.
    But beware of the size of your structure as a result.

    If you put costs into your mazecell_t, then I get this
    Code:
    int main ( ) {
      printf("%zd %zd\n", sizeof(mazecell_t), sizeof(maze_t) );
      return 0;
    }
    
    $ gcc foo.c
    $ ./a.out 
    39224 388317648
    The default stack size usually between 1 and 8MB.

    Your structure in it's complete state is 388MB!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is certainly possible. The problem is probably that you end up with a struct object that is way too big to fit into contiguous memory from the stack. Consider: your maze_t object will have 99 * 99 + 99 = 9900 mazecell_t members. Each mazecell_t object has 4 + 99 * 99 = 9805 int members. So, assuming sizeof(int) == 4, each mazecell_t object has a size of at least 2 + 9805 * 4 = 39222 bytes. Therefore, your maze_t object has a size of at least 12 + 9900 * 39222 = 388297812 bytes, possibly more if there is padding. This is more than 370 MB, which is likely to be more than the stack space available to your program (perhaps a few MB at most).

    To overcome this, you may need to introduce dynamic memory allocation with say, malloc and free so that space from the heap will be allocated.

    By the way, you declared the arrays to have MAXSIZE-1 elements, which is strange. I would have expected you to declare them as having MAXSIZE elements instead, then your loops would look like:
    Code:
    for (i = 0; i < MAXSIZE; i++) {
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    3
    Okay that makes sense thanks for the help. And also that use of MAXSIZE definitely makes more logical sense, I'll change that!

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    3
    I didn't know there was a default stack size. Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-19-2015, 05:51 PM
  2. Replies: 2
    Last Post: 02-07-2015, 11:19 AM
  3. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  4. Array with a structure
    By Jenna_Javason in forum C Programming
    Replies: 7
    Last Post: 10-09-2007, 06:58 AM
  5. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM

Tags for this Thread