Thread: Help using Vectors

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

    Help using Vectors

    Hey,
    I want to fill a vector usign the following code:
    Code:
    for(i=0; i<FAD.nExpected; i++)
    	{
    		
    		pGetEdit[i] = (CEdit*) GetDlgItem(i+100);
    
    		int len = pGetEdit[i]->LineLength();
    
    		pGetEdit[i]->GetLine(1,str.GetBuffer(len), len);
    
    		if(len != 0)
    			vars.vectParmSelected.push_back(str);
    
    	}
    aside from the visual part, the coe fails at run-time saying that the memory could not be read if I have the if statement. If I remove the if statement and copy every entry without checking into the vector it works fine. len represents the length of a line copied into str. So if there is nothign there len=0 and I don't want to copy it into the vector.
    Is there another way to do it? or what am I doing wrong?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This is a problem with your CString. Do you need to call ReleaseBuffer? Is your vector holding CStrings or LPTSTR or something else - because if it is holding a pointer then there will be a problem when the value of the pointer is changed.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I actually rewrote that vector type. Here's the new thing, and sorry I forgot to post the variable def's.
    Code:
    structLink templink;
    
    	CEdit* pGetEdit[512];
    	
    	for(i=0; i<FAD.nExpected; i++)
    	{
    		
    		pGetEdit[i] = (CEdit*) GetDlgItem(i+100);
    
    		int len = pGetEdit[i]->LineLength();
    
    		pGetEdit[i]->GetLine(1,templink.ParamName.GetBuffer(len), len);
    		templink.Index = i;
    
    		if(len != 0)
    			vars.vectParmSelected.push_back(templink);
    
    	}
    where
    Code:
    struct structLink
    {
    	CString ParamName;
    	int Index;
    };
    
    class gVars        
    	{
    	public:
    
    		std::vector < structBafParam > vectBP;
    		std::vector <structLink> vectParmSelected;
    		char hEngName[13];
    ..............
    It only fails if len ever has a vlue of 0.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Everything with your vector is fine.

    From MSDN: If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.

    Where do you call ReleaseBuffer?

    Also, if len is 0, why are you even calling GetLine? Will GetBuffer even work with 0 specified as the length of the buffer to get?
    Last edited by Daved; 07-11-2005 at 01:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM