Vector push_back() giving SIGSEGV error

S

sravanreddy001

Hi,
I've the below piece of code. I'm collecting all the links in text_files.

This code worked fine for the first 26 files and giving this error for 27th
When I skip 27th file using continue, its giving error at 89, and also at..
121 122 131.

For all the other files also. this piece of code is being executed.

When i traced it using (eclipse) i think this is because, the memory is not being alloted to this vector.

Additional Information:
I'm using around 6-7 other vector<string> objects to store other information.
And all these will have a max of 1 MB data all of them combined. and these vectors are recreated in the loop.

Could this be because, i'm not erasing the contents of vector. I don't think this is the reason.

Any inputs will be very helpful..


while(begin != string::npos && end != string::npos){
string link = line.substr(begin+2,end-begin-2);
try{
Links.push_back(link);
}
catch(...){
printf("%d - ",Links.size());
}
File_Content.push_back("L: "+link);
//printf("%s\n", line.substr(begin+2,end-begin-2).c_str());
begin = line.find("[[",end);
end = line.find("]]",begin+2);
//begin = end+1; // can be end+2, but end+1 for safety
}

Thanks,
Sravan.
 
V

Victor Bazarov

Hi,
I've the below piece of code. I'm collecting all the links in text_files.

What's "text_files"? Is that the name of the variable?
This code worked fine for the first 26 files and giving this error for 27th
When I skip 27th file using continue, its giving error at 89, and also at..
121 122 131.

For all the other files also. this piece of code is being executed.

When i traced it using (eclipse) i think this is because, the memory is not being alloted to this vector.
Huh?


Additional Information:
I'm using around 6-7 other vector<string> objects to store other information.
And all these will have a max of 1 MB data all of them combined. and these vectors are recreated in the loop.

Could this be because, i'm not erasing the contents of vector. I don't think this is the reason.

Any inputs will be very helpful..


while(begin != string::npos&& end != string::npos){
string link = line.substr(begin+2,end-begin-2);
try{
Links.push_back(link);
}
catch(...){
printf("%d - ",Links.size());
}
File_Content.push_back("L: "+link);
//printf("%s\n", line.substr(begin+2,end-begin-2).c_str());
begin = line.find("[[",end);
end = line.find("]]",begin+2);
//begin = end+1; // can be end+2, but end+1 for safety
}

Not enough information. For instance, how is 'File_Content' declared?
How is 'Links' declared?

Create a test setup in which program would take your 27th file only and
parse it (parsing is what you're doing, yes?) See if it fails then.

IOW, try to debug your program. Oh, and read the FAQ 5.8.

V
 
S

sravanreddy001

Hi Paavo,

<My apologies, I overlooked my question and missed the core part>

Please let me know if you need more info.
{I'm not a newbie to C++ (may be Intermediate), I checked if its refereing any out-of-bound reference etc.}

The "Links" is a vector<string> object.

The problem is:
The size of link has reached its capacity for a size of 4. (during one of the(26th) iterations of this code.)

When I'm trying to add the fifth element to the above vector, it is supposed to increase the size and add this element. But instead it is giving a SIGSEGV error.

True. There are no obvious errors.

Simply put.
I've a vector, and I was able to add 4 objects to it, and I checked that its capacity is 4. and max_size() is over a 100,000. When I'm trying to add 5th element its returning the Termination Interrupt
 
S

sravanreddy001

My data is fetched from text files *.txt

It was running properly for upto 26 iterations(files)

The vector is not cumulative. (Its created for each iteration and result used elsewhere)

There is lot of memory on my machine.

(The total amount of RAM that my application at any point wont be more that 5 MB)
 
I

Ian Collins

On 09/23/11 12:17 PM, sravanreddy001 wrote:

Please keep the context of the message you are replying to, otherwise
your posts don't make sense.
Hi Paavo,

<My apologies, I overlooked my question and missed the core part>

Please let me know if you need more info.
{I'm not a newbie to C++ (may be Intermediate), I checked if its refereing any out-of-bound reference etc.}

The "Links" is a vector<string> object.

The problem is:
The size of link has reached its capacity for a size of 4. (during one of the(26th) iterations of this code.)

When I'm trying to add the fifth element to the above vector, it is supposed to increase the size and add this element. But instead it is giving a SIGSEGV error.

True. There are no obvious errors.

Simply put.
I've a vector, and I was able to add 4 objects to it, and I checked that its capacity is 4. and max_size() is over a 100,000. When I'm trying to add 5th element its returning the Termination Interrupt

You really should do what Paavo and Victor suggested and build a
complete, self-contained example demonstrating your problem that you can
post. Odds are that if you can't, your problem lies elsewhere.
 
J

Jorgen Grahn

When I'm trying to add the fifth element to the above vector, it is
supposed to increase the size and add this element. But instead it is
giving a SIGSEGV error.

Then debug the core dump to see where the crash happens. If it is in
new/malloc(), you have corrupted the heap memory somewhere else in
your code. Also, run valgrind if it exists for your platform.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top