Thread: Overlapping arrays problem

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Overlapping arrays problem

    Hi,
    I've run into thi sproblem a few times in my program. I have a number of arrays in a class, and I fill them in order one by one. But once I write to a praticular array the contents get copied at the end of another array. here's a snipet of my code:
    Class deffinition:
    Code:
    class gVars        
    	{
    	public:
    
    		std::vector < CString > vectBP;	
    		std::vector <CString> vectParmSelected;
    		char hEngName[13];
    		char hVersion[13];
    		char EngName[11];
    		int VersionNum;
    
    		gVars()
    		{
    			VersionNum=0;
    			memset(hEngName, 0, sizeof(hEngName));
    			memset(hVersion, 0, sizeof(hVersion));
    			memset(EngName, 0, sizeof(EngName));
    
    		};
    	};
    the main to fill the arrays:
    Code:
    .......................................
    FindWord(inp,"!EICAS_SW_VER", vars.hVersion);
    
    	fprintf(out, ">%s<\n\n", vars.hVersion);
    
    	SkipWS(inp);
    	GetWord(inp,Word);
    
    	vars.VersionNum=atoi(Word);
    
    	fprintf(out, ">%s<\n\n", vars.hVersion);
    	
    	FindWord(inp,"!ENGINE_NAME", vars.hEngName); 
    	SkipWS(inp);
    	fprintf(out, ">%s<\n\n", vars.hVersion);
    
    	GetWord(inp,Word);
    	fprintf(out, ">%s<\n\n", vars.hVersion);
    
    	strcpy(vars.EngName, Word);
    	fprintf(out, ">%s<\n\n", vars.hVersion);
    .........................
    the output:
    Code:
    >!EICAS_SW_VER<
    
    >!EICAS_SW_VER<
    
    >!EICAS_SW_VER<
    
    >!EICAS_SW_VER<
    
    >!EICAS_SW_VER"Engversion"<
    All the strings should be identical.
    Last edited by earth_angel; 07-05-2005 at 06:38 AM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    MFC + STL + C i/o .... geez !!!

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    The output is only to test the contents. It is actually used for something else.

    and I think I fixed it. One of the arrays was too short. But I'm not sure why it would copy to another array?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char hVersion[13];
    char EngName[11];
    
    ...
    
    memset(hVersion, 0, sizeof(hVersion));
    memset(EngName, 0, sizeof(EngName));
    
    ...
    
    strcpy(vars.EngName, Word);
    Though it may look it, it is not copying to another array. You're getting lucky with the first few fprintf's. The string you copy into hVersion is exactly the length of the array which means no room for the NULL terminating character. Your print statments work because you memset the array EngName to all 0's which acts as a NULL when it is time to print the hVersion (the next character after the hVersion array hapens to be the first character of the EngName array which is set to 0, i.e. NULL, so the fprintf works by luck for those first few times). When you copy your new text into EngName using the strcpy, you overwrite the NULL character that was keeping the fprintf looking OK so you get output that is messed up.

    The bottom line is that you should use arrays of sufficient size to store your expected data and also to use safer functions that will not store more data than can be handled by your program. Since you're obviously using C++ I would suggest switching to using the string container instead of char arrays.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I know how long the strings are supposed to be that's why I used the arrays of char, but I forgot about the good old NULL terminator.

    Thanks.

    AS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 01:26 PM
  2. assignment of arrays problem
    By HumbuckeR in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2006, 04:25 PM
  3. Problem with arrays
    By dogbert234 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 03:06 AM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM