Question about array declaration/use

This is a discussion on Question about array declaration/use within the C++ Programming forums, part of the General Programming Boards category; I load a map from a file. I got it to work perfect thanks to Salem (^_^), that is the ...

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Question about array declaration/use

    I load a map from a file. I got it to work perfect thanks to Salem (^_^), that is the loading part works fine, and my game using the array works fine...... but only if I declare the array (it's global) like this:

    Code:
    byte Map[800] = {0};
    if I declare it like this:

    Code:
    byte Map[800];
    or
    Code:
    byte *Map = NULL;
    (and then create it using new and delete it properly, of course)

    then it doesn't work. What happens is that for some reason it has odd numbers in it. I don't think it has to do with the file loading code, because it loads the map fine if I add that = {0}; to the end.

    What can I do to make the other two declarations work as well, and what's the main difference between byte Map[800] = {0}; and byte Map[800];? Even if I fill Map with zeros before loading it, it still creates problems...

    sorry if this is a weird prob... thanks for any help...

  2. #2
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    I think the prob is in the loading map function, weird numbers is a symptom of creating a variable or array and not setting the values - consequently it will contain whatever junk value resides in that mem addr.

    Your load map function should fill the whole MAP array with values you want and if there is left over space have your func zerofill the rest of the array. If you have junk you have probs.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    OK, the total code isn't extremely long so I'll post it:

    Code:
    // Map declaration
    byte MAP[800] = {0};
    
    // Map loading code
    
    void LoadWorld (const char* fn)
    {
    	FILE *f;
    	if ((f = fopen(fn, "r")) == NULL)
    		return; 
    
    	for (int y = 0; y < MainMap.Height; ++y) {
    		for (int x = 0; x < MainMap.Width; ++x) {
    			int c;
    			if (fscanf(f, "%d,", &c) == 1) {
    			  MAP[(y * MainMap.Width) + x] = c;
    			} else {
    				if (feof(f))
    					break;
    			}
    		}
    	}
    
    	fclose(f);
    }
    
    // code to get tile at y, x
    inline byte GetTile (int y, int x) {
    	return (MAP[(y * MainMap.Width) + x]);
    }
    
    // code where map generates error: "c" is unexpected value
    #define MAP_PRIMARY_BLANK 254	// 254 id tiles == skip
    
    void DrawMap (int mapID)
    {
    	for (int y = 0; y < 16; ++y) {
    		for (int x = 0; x < 21; ++x) { // called 336 times
    			int c = GetTile(Cam.tCamy+y,Cam.tCamx+x);
    			if (c != MAP_PRIMARY_BLANK) {
    				masked_blit (tileset[mapID], vscreen, 
    					pTiles[c]->xLoc, pTiles[c]->yLoc,
    					(x << 4) - (Cam.CamxOff),
    					(y << 4) - (Cam.CamyOff), 16, 16);
    			}
    		}
    	}
    }
    I don't think the problem would be in any of the other variables (CamxOff, tCamx,x,y,etc), because the only time c ever becomes a problem is when the initialization to 0 is taken off. I tried memset(MAP, 0, 800 * sizeof (byte)); and it didn't change anything.

    = /

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by vVv
    If you only do byte Map[800];, the array contains the random data that happened to be where the space for Map was allocated.
    Actually that's not true. The second version IS 0'd out. Global primitive data is guaranteed to be all 0's unless otherwise specified. It only won't be 0'd out if it's created on the stack.

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    What's confusing me is the file loading code, because I don't think it has any effect that could cause the error.

    If after I load the map, I set it all to zero (using memset, or a loop), it still brings up strange values in the map drawing code. But, if I add that = {0}; ... no problems...

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Weird...

    Yeah, if I perform range checking it doesn't complain.

    EDIT: Fixed the other error too.

    Thanks!
    Last edited by rmullen3; 01-04-2003 at 12:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM

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