Weird behavior when populating array from file.

Hello folks!

I am new here. I am trying to go back and revisit C/C++ programming and to refresh myself I decided to try some projects from the MIT C programming course. Here is the link for anyone interested: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-087-practical-programming-in-c-january-iap-2010/

I am working on the first lab which is a Game of Life program.
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-087-practical-programming-in-c-january-iap-2010/labs/

The Lab comes with the source code where the student has to fill in the functions so that the program runs successfully.

One of the functions requires reading a file that contains initial values for the Game of Life to work. The file will need to be read but if the file hits a newline before the end of the array row is reached then the remaining elements within that row will need to be filled in with spaces (ASCII 32).

so for example if the file being read is the following (I will use a '-' to represent a space due to formatting issue):
-------\n
--*----\n
-**----\n
--**---\n

And if the array is 32x32 the file array would look something like this
array[0][0] - array[0][31] all filled with ascii value 32.
array[1][0] and [1][1] filled with 32, but array[1][2] will have '*' value ASCII value 42 and arrays[1][3] to [1][31] fill in with space (32).
and so on.

Based on this criteria I wrote the function the following way:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
       void initialize_world_from_file(const char * filename) {
	/* TODO: read the state of the world from a file with
	   name "filename". Assume file exists, is readable, and
	   the ith character of the jth line (zero-indexed) describes
	   world[i][j] according to the characters CHAR_ALIVE and
	   CHAR_DEAD

	   Assume a line does not contain more than 256 characters
	   (including newline). If a line doesn't contain WORLDWIDTH
	   characters, remaining cells in line are presumed DEAD.
	   Similarly, if the file does not contain WORLDHEIGHT lines,
	   remaining lines are presumed dead.

	   On error, print some useful error message and call abort().

	   Also need to reset the next generation to DEAD
	 */
     
     // initialize array
     
	int val;
    int fill;
	FILE *fd = fopen(filename, "r+");
	
    /* zero out the array */
    //memset(world, 32, sizeof(world[0][0]) * WORLDWIDTH * WORLDHEIGHT);


    int test = 0;
    val = 0; fill = 0;
    int i=0, j=0;
    while(i < WORLDHEIGHT)
    {
           
       if(!fill && (val != EOF))
       {
           val=fgetc(fd);
       }
       
       if(val==10)
           fill = 1;
           
       if(fill || (val == EOF))
       {
           world[i][j] = 32;
       }
       else
       {
           world[i][j] = val;
       }
       j++;
       if(j > WORLDWIDTH)
       {
           i++;
           j=0;
           fill=0;
       }
    }
    

    fclose(fd);
}


The problem is that at certain parts of the code during runtime unexpected behavior occurs.
Specifically when the world[i][j] if i=1 and j=3 the value of world[0][23] get modified to the same value that world[1][3] are set to.

I am not sure why this is happening.

I tried looking through GDB but I am left confused.

Here is what I found with GDB:
(gdb) p i
$1 = 1
(gdb) p j
$2 = 3
(gdb) p world[0][23]
$3 = 32
:
(gdb) n
95 val=fgetc(fd);
:
(gdb) n
109 world[i][j] = val;
(gdb) p world[1][3]
$20 = 42
(gdb) p world[0][23]
$21 = 42

As you can see the value of world[0][23] also gets modified!??!

I appreciate any help cause I am not sure what is the issue at this point.

Thanks!
Last edited on
What is the array dimensions? How is it declared?
Hi MiiNiPaa,

Here is the declaration of the Array.

1
2
3
4
5
/* hard-coded world size */
#define WORLDWIDTH 39
#define WORLDHEIGHT 20

static int world[WORLDWIDTH][WORLDHEIGHT];


Also the entire code is available online at: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-087-practical-programming-in-c-january-iap-2010/labs/lab01.zip

Thanks!
so, your array is essentually   
static int world[39][20];  
And you access it as   
world[0][23]  
Do you see problem now?

WOW I don't know why I would miss this.
Feel dumb, I guess sometimes you need another person to point out the easy answers.

Thanks for pointing this out I overlooked the obvious :(
Topic archived. No new replies allowed.