Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. STL iterators?

STL iterators?

Scheduled Pinned Locked Moved C / C++ / MFC
c++comgraphicsdebuggingquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fredrik Skog
    wrote on last edited by
    #1

    Why do I get an access violation when I try to get the value of the iterator "it" in this piece of code? When I look at the iterators they have no valid pointers to the strings.

    typedef vector TFileList;
    typedef TFileList::iterator TFileListIt;
    
    CStringList strlistFiles;
    fsDlg.SelectFile(&strlistFiles);
    int nFiles = strlistFiles.GetCount();
    
    if (nFiles > 0)
    {
    	int nIndex;
    	TFileList FileList(nFiles);
    
    	CString strFileName, strNewFileName;
    	POSITION pos;
    
    	// Populate the list with the pointers to the file names.
    	for (nIndex = 0; nIndex < nFiles; nIndex++)
    	{
    		pos = strlistFiles.FindIndex(nIndex);
    		FileList.push\_back(&strlistFiles.GetAt(pos));
    	}
    	
    	TFileListIt start, end, it;
    	start = FileList.begin();
    	end = FileList.end();
    	for (it = start; it != end; it++)
    	{
    		TRACE("> %s\\n", \*\*it);   // <-- Access violation!
    	}
    }
    

    Cheers, Fredrik
    Sonork ID: 100.11430:PhatBoy
    "Felix qui potuit rerum cognoscere causas."

    A 2 Replies Last reply
    0
    • F Fredrik Skog

      Why do I get an access violation when I try to get the value of the iterator "it" in this piece of code? When I look at the iterators they have no valid pointers to the strings.

      typedef vector TFileList;
      typedef TFileList::iterator TFileListIt;
      
      CStringList strlistFiles;
      fsDlg.SelectFile(&strlistFiles);
      int nFiles = strlistFiles.GetCount();
      
      if (nFiles > 0)
      {
      	int nIndex;
      	TFileList FileList(nFiles);
      
      	CString strFileName, strNewFileName;
      	POSITION pos;
      
      	// Populate the list with the pointers to the file names.
      	for (nIndex = 0; nIndex < nFiles; nIndex++)
      	{
      		pos = strlistFiles.FindIndex(nIndex);
      		FileList.push\_back(&strlistFiles.GetAt(pos));
      	}
      	
      	TFileListIt start, end, it;
      	start = FileList.begin();
      	end = FileList.end();
      	for (it = start; it != end; it++)
      	{
      		TRACE("> %s\\n", \*\*it);   // <-- Access violation!
      	}
      }
      

      Cheers, Fredrik
      Sonork ID: 100.11430:PhatBoy
      "Felix qui potuit rerum cognoscere causas."

      A Offline
      A Offline
      Alexandru Savescu
      wrote on last edited by
      #2

      Try *(*it)? (Just guessing :)). Best regards, Alexandru Savescu

      F 1 Reply Last reply
      0
      • A Alexandru Savescu

        Try *(*it)? (Just guessing :)). Best regards, Alexandru Savescu

        F Offline
        F Offline
        Fredrik Skog
        wrote on last edited by
        #3

        The problem is that my vector seems to be empty, even though I am filling it with valid pointers.:( Cheers, Fredrik
        Sonork ID: 100.11430:PhatBoy
        "Felix qui potuit rerum cognoscere causas."

        A 1 Reply Last reply
        0
        • F Fredrik Skog

          The problem is that my vector seems to be empty, even though I am filling it with valid pointers.:( Cheers, Fredrik
          Sonork ID: 100.11430:PhatBoy
          "Felix qui potuit rerum cognoscere causas."

          A Offline
          A Offline
          Alexandru Savescu
          wrote on last edited by
          #4

          Did you call size() to see if it is empty? Best regards, Alexandru Savescu

          F 1 Reply Last reply
          0
          • A Alexandru Savescu

            Did you call size() to see if it is empty? Best regards, Alexandru Savescu

            F Offline
            F Offline
            Fredrik Skog
            wrote on last edited by
            #5

            size() returns 8, but I have added 4 pointers to the vector.:confused: I have noticed that the vector elements does not point to m_pchData of the string list elements I am using, but to some other address. _First and _Last of the vector are updated when adding to the vector. Cheers, Fredrik
            Sonork ID: 100.11430:PhatBoy
            "Felix qui potuit rerum cognoscere causas."

            A 1 Reply Last reply
            0
            • F Fredrik Skog

              size() returns 8, but I have added 4 pointers to the vector.:confused: I have noticed that the vector elements does not point to m_pchData of the string list elements I am using, but to some other address. _First and _Last of the vector are updated when adding to the vector. Cheers, Fredrik
              Sonork ID: 100.11430:PhatBoy
              "Felix qui potuit rerum cognoscere causas."

              A Offline
              A Offline
              Alexandru Savescu
              wrote on last edited by
              #6

              Try not to store pointers, store CStrings directly. There is not much of a performance differnce since the CString does not get copied, its reference is increased. Best regards, Alexandru Savescu

              F 1 Reply Last reply
              0
              • A Alexandru Savescu

                Try not to store pointers, store CStrings directly. There is not much of a performance differnce since the CString does not get copied, its reference is increased. Best regards, Alexandru Savescu

                F Offline
                F Offline
                Fredrik Skog
                wrote on last edited by
                #7

                Nah, didn't work either. The push_back seems broken... Cheers, Fredrik
                Sonork ID: 100.11430:PhatBoy
                "Felix qui potuit rerum cognoscere causas."

                1 Reply Last reply
                0
                • F Fredrik Skog

                  Why do I get an access violation when I try to get the value of the iterator "it" in this piece of code? When I look at the iterators they have no valid pointers to the strings.

                  typedef vector TFileList;
                  typedef TFileList::iterator TFileListIt;
                  
                  CStringList strlistFiles;
                  fsDlg.SelectFile(&strlistFiles);
                  int nFiles = strlistFiles.GetCount();
                  
                  if (nFiles > 0)
                  {
                  	int nIndex;
                  	TFileList FileList(nFiles);
                  
                  	CString strFileName, strNewFileName;
                  	POSITION pos;
                  
                  	// Populate the list with the pointers to the file names.
                  	for (nIndex = 0; nIndex < nFiles; nIndex++)
                  	{
                  		pos = strlistFiles.FindIndex(nIndex);
                  		FileList.push\_back(&strlistFiles.GetAt(pos));
                  	}
                  	
                  	TFileListIt start, end, it;
                  	start = FileList.begin();
                  	end = FileList.end();
                  	for (it = start; it != end; it++)
                  	{
                  		TRACE("> %s\\n", \*\*it);   // <-- Access violation!
                  	}
                  }
                  

                  Cheers, Fredrik
                  Sonork ID: 100.11430:PhatBoy
                  "Felix qui potuit rerum cognoscere causas."

                  A Offline
                  A Offline
                  Alexandru Savescu
                  wrote on last edited by
                  #8

                  It's this line: Fredrik Skoog wrote: TFileList FileList(nFiles); This creates nFiles items initially empty and I guess NULL pointers. That's why you had 8 items, because 4 have already been created. You shoud use:

                  TFileList FileList;
                  FileList.reserve (nFiles);

                  reserve allocates enough space, but does not create any items. Best regards, Alexandru Savescu

                  F 1 Reply Last reply
                  0
                  • A Alexandru Savescu

                    It's this line: Fredrik Skoog wrote: TFileList FileList(nFiles); This creates nFiles items initially empty and I guess NULL pointers. That's why you had 8 items, because 4 have already been created. You shoud use:

                    TFileList FileList;
                    FileList.reserve (nFiles);

                    reserve allocates enough space, but does not create any items. Best regards, Alexandru Savescu

                    F Offline
                    F Offline
                    Fredrik Skog
                    wrote on last edited by
                    #9

                    Yep, that's it. Thanks a million! I think I'll have to hit the books now :-O Cheers, Fredrik
                    Sonork ID: 100.11430:PhatBoy
                    "Felix qui potuit rerum cognoscere causas."

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups