So I have narrowed down a segfault to a trim function that's using substr, where the substr call is ultimately causing my segfault. I've been doing a fair amount of searching both on these forums and google and I understand that it's somehow related to how substr read or writes to memory where it's not allowed to, but none of the discussions have been fairly close to what I'm trying to do.
The function looks like this
1 2 3 4 5 6
string trim ( string s, unsignedint l )
{
if(s.length() >= l)
return s.substr(0,l) + "...";
return s;
}
Another interesting thing is that this only happens if tmp contains more than one element and the segfault always happens on the exact same element each time. But if the vector only contains one element it instead throws this error
1 2 3
3(30249) malloc: *** error for object 0x7fff5fbff850: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap
Like I said I've understood I'm doing some nasty things to the memory, but can't see what exactly. Oh, and if you need any other code or info that I haven't already provided, just let me know!
I do not see any problem with your code that you showed. Only it is not known how function alternateSort works and whether you defined correctly the copy constructor and the copy assignment operator for class Movie.
So I have narrowed down a segfault to a trim function that's using substr, where the substr call is ultimately causing my segfault. I've been doing a fair amount of searching both on these forums and google and I understand that it's somehow related to how substr read or writes to memory where it's not allowed to, but none of the discussions have been fairly close to what I'm trying to do.
The function looks like this
1 2 3 4 5 6
string trim ( string s, unsignedint l )
{
if(s.length() >= l)
return s.substr(0,l) + "...";
return s;
}
Another interesting thing is that this only happens if tmp contains more than one element and the segfault always happens on the exact same element each time. But if the vector only contains one element it instead throws this error
3(30249) malloc: *** error for object 0x7fff5fbff850: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap
Like I said I've understood I'm doing some nasty things to the memory, but can't see what exactly. Oh, and if you need any other code or info that I haven't already provided, just let me know!
My guess is that substr() assumes there are enough characters to cover for the value of 'l'. See if this gets rid of the problem:
My guess is that substr() assumes there are enough characters to cover for the value of 'l'. See ifthis gets rid of the problem:
return s.substr(0, std::min(l, s.length())) + "...";
The use of std::min() requires you to #include <algorithm>.
It is a wrong guess because there is the following check in the function
I see that the function has a serious problem. It deletes tmp that has the same value as some element of the vector. I do not think that you should delete tmp.