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

Thread: vector

  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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: vector

    Assuming that std::bad_alloc is not thrown, it looks okay to me, except for the missing #include <cstring>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: vector

    Quote Originally Posted by Rajesh1978 View Post
    The below code is giving me segment fault during delete [] (*iter) sometimes
    1) Please state the compiler and if necessary, operating system you're using (compiler name and version -- for example, don't just say g++, say g++, the version of g++ being used, and Linux as the OS).

    2) You are missing the <cstring> header, as laserlight pointed out.

    3) Other than that, the code should work correctly, unless again, std::bad_alloc() was thrown because of the call to new[].

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 19th, 2012 at 04:16 AM.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: vector

    Compiles and runs without error for me.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: vector

    I suppose I should point out that you can save yourself the trouble if you use a vector<string> instead.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: vector

    Quote Originally Posted by Rajesh1978 View Post
    Please suggest what is the fault.
    Since the code looks okay it's probably a linker error. Check that you're using the correct object files and libraries.

  7. #7
    Join Date
    Jul 2007
    Posts
    249

    Re: vector

    Thanks all.
    This a extracted code from my actual project where it was crashing. Due to some other memory corruption the crash was happening and the BT was pointing some time some where. I doubted that the deletion using iterator may invalidating the iterator and it was crashing as it has pointed to this iterator deletion once.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: vector

    Quote Originally Posted by Rajesh1978 View Post
    Thanks all.
    This a extracted code from my actual project where it was crashing.
    No, what you posted is a completely different program then the one you are running that is causing the issue. You posted a complete, valid program with no runtime issues.

    In general, when you have corrupted memory, any part of your application is susceptible to error. The place where the application breaks down is usually not where the problems originated.

    Regards,

    Paul McKenzie

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