Thread: Damage error with clearing a vector

  1. #16
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    found the function that fills the vector and it seems to be causing the problems. I use almost the same code as in this function within the program and that vector can be cleared without a problem. I've been looking at it for a while but can't pin point the problem. Could someone take a look at it, I must be missing something:
    Code:
    void ReadDataSec(FILE * inp, FSAD &Section , int nExpected)
    {
    	char line[110];
    	int  check;
    	int newline =0, endcount=0;
    	DataLine dl;
    
    	int size = nExpected*2 +1;
    
    	while ( fgets(line, sizeof line, inp) != NULL) // reads in a line of text from file
    	{
    		dl.Param = dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=dl.Max=dl.Min = '\0';
    		newline =0;
    
    		if (strncmp(line, "\n", 1) == 0)
    			newline =1;
    
    		check = (sscanf(line, "%s %d %f %d %d %d %f %f", dl.Param, &dl.Label,
                       &dl.SFactor, &dl.SigBits, &dl.Bit30, &dl.Bit31, &dl.Max, &dl.Min)); 
    		
    		if ( strncmp(dl.Param, "ENDL", 4) == 0 && newline ==0) // Exit if at the end of a section
    		{
    			dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=dl.Max=dl.Min='\0';
    			
    			Section.vectDL.push_back(dl);
    			break;
             }
    
    		if ( strcmp(dl.Param, "END") == 0 && newline == 0) // set all fields to 0 
    at the end of subsection marker
    		{
    			dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=dl.Max=dl.Min='\0';
    			
    			Section.vectDL.push_back(dl);
    			
    			endcount++;
    		}
    
    		if (strncmp(dl.Param, "END", 3) != 0 && newline ==0) // If the line is not
     the end of subsection marker, copy the line into global vector
    		{
    			
    			if (check == 8)
    			{	
    				Section.vectDL.push_back(dl);
    			}
    
    			else if (check == 6)
    			{
    				dl.Max=dl.Min='\0';
    
    				Section.vectDL.push_back(dl);
    			}
    		}
    
    		
    	}
    
    	if (endcount != nExpected)
    		EXIT_FAILURE;
    }
    Last edited by earth_angel; 08-25-2005 at 01:48 PM.
    Everything is relative...

  2. #17
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    The orange line in the above code was removed, and the error does not occure anymore. However, the file is not parsed properly now. I hope it won't take me another million hours to fix that.

    I just don't understand why would resettign the structure variable would cause a amemory leak?
    Everything is relative...

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > dl.Param = dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=d l.Max=dl.Min = '\0';
    You should not be setting dl.Param to '\0'. Since this is a char array (assumption based on your sscanf()), you would lose the memory address. To clear dl.Param, use:
    Code:
    dl.Param[0] = '\0';

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And since dl.Label and the rest are numbers, you should zero them with:
    Code:
    ... = 0;
    instead of:
    Code:
    ... = '\0';
    The two statements are equivalent, but the second is usually used for strings.

  5. #20
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    dl.Param is a CString.

    and I'm using MS VC++ 6.0
    Everything is relative...

  6. #21
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I've amde the change from '\0' to 0, but I kept getting the demage error. So I figured that if I can't fix this problem I'll go around it. So I used an array of dl's instead of reusing and resetting the same one. This way I only use the data I just read into that cell and there is no demage error further on. That array is destryed automatically on the stack, so I don't have to worry about it.
    I have to admit I really don't understand how clearing that variable would cause a demage problem with the vector being filled later on.
    Everything is relative...

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I don't believe it's a good idea to pass a CString directly to sscanf(). I would try something like:
    Code:
    check = (sscanf(line, "%s %d %f %d %d %d %f %f", dl.Param.GetBufferSetLength(sizeof(line)), &dl.Label,
                       &dl.SFactor, &dl.SigBits, &dl.Bit30, &dl.Bit31, &dl.Max, &dl.Min));
    And then just leave out dl.Param in your clearing line:
    Code:
    dl.Label=dl.SFactor=dl.SigBits=dl.Bit30=dl.Bit31=dl.Max=dl.Min = 0;

  8. #23
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Then I cannot call any other cString function until I call ReleaseBuffer(). and I'd like to work with it before I release it.
    Everything is relative...

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Then I cannot call any other cString function until I call ReleaseBuffer()
    I don't think that's true with GetBufferSetLength(), only with GetBuffer().

    But irregardless, CString is a class, and cannot be passed directly to sscanf(). You can read a CString like a char array, but not write to one as a char array.

  10. #25
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    OK. I see. that makes a lot of sense, I always froget about it being a class in these cases.

    Thanks.
    Everything is relative...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM