CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: vector

Threaded View

  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Arrow vector

    The below code is giving me segment fault during delete [] (*iter) sometimes
    Please suggest what is the fault. Is it not the proper way to delete the vector with char *
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector<char*> nameList;
        for (int i=0;i<1000;++i)
        {
            char *p = new char[10];
            strcpy(p,"test");
            nameList.push_back(p);
        }
    
        vector<char*>::iterator itr;
        for(itr=nameList.begin();itr!=nameList.end();++itr)
        {
            cout<<*itr<<endl;
        }
    
        vector<char*>::iterator iter;
        for(iter= nameList.begin();iter!=nameList.end();++iter)
        {
            delete[] (*iter);
        }
        return 0;
    }
    Last edited by Rajesh1978; April 19th, 2012 at 01:46 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured