Question - Dynamic 2d arrays in C
Author |
Message |
Gregory EISENBER #1 / 8
|
 Question - Dynamic 2d arrays in C
Hey FOLKS check this out: I can't seem to figure it out I declare 4, variable size, 2 dimensional arrays as follows typedef int** BOARD_T; BOARD_T PlayBoard=NULL; /* PLAY board */ BOARD_T Libs=NULL; /* liberty matrix */ BOARD_T ScoreBoard=NULL; /* Markup Board for ScoreBoard() */ BOARD_T CountBoard=NULL; /* Markup Board for Count() */ then I intialize each of them when the size is determined, as follows *PlayBoard = (int *) calloc(sizeof(int),size); for (i=0; i<BoardSize; i++) PlayBoard[i] =(int *) calloc(sizeof(int),size); *Libs = (int *) calloc(sizeof(int),size); for (i=0; i<BoardSize; i++) Libs[i] =(int *) calloc(sizeof(int),size); *CountBoard = (int *) calloc(sizeof(int),size); for (i=0; i<BoardSize; i++) CountBoard[i] =(int *) calloc(sizeof(int),size); *ScoreBoard = (int *) calloc(sizeof(int),size); for (i=0; i<BoardSize; i++) ScoreBoard[i] =(int *) calloc(sizeof(int),size); Now everything works pretty well - but when I modify CountBoard, or Libs I am also modifying PlayBoard It is like they are the same piece of memory!! I thought what I was doing was declaring 4 separate 2d arrays but instead I get 4 pointers to a single 2d array Any clues? What am I doing wrong? I thought I was right! Thanx in advance for any help BTW : I am compiling on a Pentium 160 using DJGPP v2.01 (read gcc for DOS) ----------------------------------------------------------------------------- GREGORY A EISENBERG Computer Science Undergraduate Society PREZ
http://www.*-*-*.com/ ~tele check out THREE WAY STREET http://www.*-*-*.com/ ~tele/3way "Kirk to Enterprise -- beam down yeoman Rand and a six-pack." Tue Dec 23 03:38:01 EST 1997 --
|
Sat, 10 Jun 2000 03:00:00 GMT |
|
 |
Bryan J. Polya #2 / 8
|
 Question - Dynamic 2d arrays in C
Quote:
> I declare 4, variable size, 2 dimensional arrays as follows > typedef int** BOARD_T; > BOARD_T PlayBoard=NULL; /* PLAY board */ > then I intialize each of them when the size is determined, as follows > *PlayBoard = (int *) calloc(sizeof(int),size);
You're not actually initializing Playboard, but them memory location pointed to by Playboard. Try: PlayBoard = (int **) calloc(sizeof(int*),size); Make sure you initialize Playboard before you ever use *Playboard (remember: *ptr dereferences ptr). You'll also want to make sure you're using sizeof(int*). A pointer to int may have a different size than the size of an int. I'm surprised you didn't get a run-time error. -- Bryan J. Polyak - Senior Programmer Analyst Raytheon STX - EROS Data Center, Sioux Falls, SD 57198
Fax: (605)594-6940 | http://edcwww.cr.usgs.gov/ --
|
Sat, 10 Jun 2000 03:00:00 GMT |
|
 |
Pierre Assel #3 / 8
|
 Question - Dynamic 2d arrays in C
Quote:
>typedef int** BOARD_T; >BOARD_T PlayBoard=NULL; /* PLAY board */ > *PlayBoard = (int *) calloc(sizeof(int),size);
Is this for real? Your left-hand side just dereferenced a NULL. Other mistakes are left as an exercise. OK, here's a clue. Learn a de{*filter*}, single-step through the code and check the assigned values. Merry Christmas. -- --Pierre Asselin, Westminster, Colorado
--
|
Sat, 10 Jun 2000 03:00:00 GMT |
|
 |
Sean O'Sulliva #4 / 8
|
 Question - Dynamic 2d arrays in C
PlayBoard = (BOARD_T) calloc(sizeof(int *),size); for (i=0; i<BoardSize; i++) PlayBoard[i] =(int *) calloc(sizeof(int),size); etc. I'm curious if the paramaters for calloc are reversed, i.e. calloc(size, sizeof(...)). -- Sean --
|
Sat, 10 Jun 2000 03:00:00 GMT |
|
 |
Christopher R Volp #5 / 8
|
 Question - Dynamic 2d arrays in C
Quote:
> >typedef int** BOARD_T; > >BOARD_T PlayBoard=NULL; /* PLAY board */ > > *PlayBoard = (int *) calloc(sizeof(int),size); > Is this for real? Your left-hand side just dereferenced a NULL. > Other mistakes are left as an exercise. > OK, here's a clue. Learn a de{*filter*}, single-step through the code > and check the assigned values. Merry Christmas.
I disagree with the clue. De{*filter*}s are useful for people who already know the language but don't understand where the logic of their code is going wrong. This person needs to understand the language better first. My guess is that the person doesn't fully appreciate the fact that "*" isn't just a "type change operator" that removes a level of "pointer-to" from the type of its operand, but rather is a dereference operator, and therefore depends on the *value* of its operand, which in this case is null. The above problem is not one solved by using a de{*filter*}, but rather one solved by thorough reading and understanding of a C text and the FAQ list, IMO. -- Chris Volpe Phone: (518) 387-7766 GE Corporate R&D Fax: (518) 387-6981 PO Box 8 Email: "volpecr" AT "crd.ge.com" Schenectady, NY 12301 Web: http://www.*-*-*.com/ ~volpecr Note: News postings do not constitute a GE Corporate communication --
|
Sun, 11 Jun 2000 03:00:00 GMT |
|
 |
Gregory EISENBER #6 / 8
|
 Question - Dynamic 2d arrays in C
Well michael - Thanx for the info - but your reply lacked a solution Actually about fif{*filter*} minutes after emailling the newsgroups I figured this bug out I had been doing: Quote: > > *PlayBoard = (int *) calloc(sizeof(int),size); > > for (i=0; i<BoardSize; i++) > > PlayBoard[i] =(int *) calloc(sizeof(int),size); > > *Libs = (int *) calloc(sizeof(int),size); > > for (i=0; i<BoardSize; i++) > > Libs[i] =(int *) calloc(sizeof(int),size);
WHAT I wanted to be doing is: PlayBoard = (int **) calloc(sizeof(int *),size); for (i=0; i<BoardSize; i++) PlayBoard[i] =(int *) calloc(sizeof(int),size); Now it works great!! Thanx ----------------------------------------------------------------------------- GREGORY A EISENBERG Computer Science Undergraduate Society PREZ
http://www.*-*-*.com/ ~tele check out THREE WAY STREET http://www.*-*-*.com/ ~tele/3way Fairy Tale, n.: A horror story to prepare children for the newspapers. Tue Dec 23 22:36:27 EST 1997 --
|
Sun, 11 Jun 2000 03:00:00 GMT |
|
 |
Michael C. Kaste #7 / 8
|
 Question - Dynamic 2d arrays in C
(posted and mailed)
[snip] Quote: > typedef int** BOARD_T; > BOARD_T PlayBoard=NULL; /* PLAY board */ > BOARD_T Libs=NULL; /* liberty matrix */ > BOARD_T ScoreBoard=NULL; /* Markup Board for ScoreBoard() */ > BOARD_T CountBoard=NULL; /* Markup Board for Count() */ > then I intialize each of them when the size is determined, as follows > *PlayBoard = (int *) calloc(sizeof(int),size); > for (i=0; i<BoardSize; i++) > PlayBoard[i] =(int *) calloc(sizeof(int),size); > *Libs = (int *) calloc(sizeof(int),size); > for (i=0; i<BoardSize; i++) > Libs[i] =(int *) calloc(sizeof(int),size);
[snip -- likewise for *CountBoard and *ScoreBoard] Quote: > Now everything works pretty well - but when I modify CountBoard, or Libs > I am also modifying PlayBoard > It is like they are the same piece of memory!!
They are. You declare four double pointers, initializing each to NULL, and never give them any other value. Hence they all point to the same location, which is of type pointer to int, and whose address is zero. Then you assign a series of pointer values to the location to which they all point, each value overlaying the previous one. At that point, *PlayBoard, *Libs, *ScoreBoard and *Board are all the same variable by four different names, so naturally they all point to the same place. In most operating systems you would have crashed and burned by now, by assigning through a null pointer.
http://home.swbell.net/mck9/cobol/cobol.html --
|
Sun, 11 Jun 2000 03:00:00 GMT |
|
 |
Michael C. Kaste #8 / 8
|
 Question - Dynamic 2d arrays in C
(posted and mailed)
[snip] Quote: > typedef int** BOARD_T; > BOARD_T PlayBoard=NULL; /* PLAY board */ > BOARD_T Libs=NULL; /* liberty matrix */ > BOARD_T ScoreBoard=NULL; /* Markup Board for ScoreBoard() */ > BOARD_T CountBoard=NULL; /* Markup Board for Count() */ > then I intialize each of them when the size is determined, as follows > *PlayBoard = (int *) calloc(sizeof(int),size); > for (i=0; i<BoardSize; i++) > PlayBoard[i] =(int *) calloc(sizeof(int),size); > *Libs = (int *) calloc(sizeof(int),size); > for (i=0; i<BoardSize; i++) > Libs[i] =(int *) calloc(sizeof(int),size);
[snip -- likewise for *CountBoard and *ScoreBoard] Quote: > Now everything works pretty well - but when I modify CountBoard, or Libs > I am also modifying PlayBoard > It is like they are the same piece of memory!!
They are. You declare four double pointers, initializing each to NULL, and never give them any other value. Hence they all point to the same location, which is of type pointer to int, and whose address is zero. Then you assign a series of pointer values to the location to which they all point, each value overlaying the previous one. At that point, *PlayBoard, *Libs, *ScoreBoard and *Board are all the same variable by four different names, so naturally they all point to the same place. In most operating systems you would have crashed and burned by now, by assigning through a null pointer.
http://home.swbell.net/mck9/cobol/cobol.html --
|
Sun, 11 Jun 2000 03:00:00 GMT |
|
|
|