|
-
April 19th, 2012, 01:28 AM
#1
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.
-
April 19th, 2012, 02:35 AM
#2
Re: vector
Assuming that std::bad_alloc is not thrown, it looks okay to me, except for the missing #include <cstring>.
-
April 19th, 2012, 04:14 AM
#3
Re: vector
 Originally Posted by Rajesh1978
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.
-
April 19th, 2012, 07:17 AM
#4
Re: vector
Compiles and runs without error for me.
-
April 19th, 2012, 09:11 AM
#5
Re: vector
I suppose I should point out that you can save yourself the trouble if you use a vector<string> instead.
-
April 19th, 2012, 09:27 AM
#6
Re: vector
 Originally Posted by Rajesh1978
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.
-
April 20th, 2012, 03:24 AM
#7
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.
-
April 20th, 2012, 04:59 AM
#8
Re: vector
 Originally Posted by Rajesh1978
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|