Segmentation error

`I try to write a program that changes two lines, although I did it pretty well, but when I start it, the program runs until line 26, and then I get a segmentation error. The program compiles fine. I am wondering if there is a simple or obvious problem in my functions that I don't see. Any help would be appreciated!

Thank you in advance

#include <iostream> #include <string> using namespace std; // Reversing the characters in strings. void reverse(string str); void swap(char * first, char *last); int main() { // declarations and initialization string str1; string str2; cout << "Please enter the first string of characters:\n"; cin >> str1; cout << "Please enter the second string of characters:\n"; cin >> str2; cout << "The strings before reversing are:" << endl; cout << str1 << " " << str2 << endl; // reverse str1 reverse(str1); // reverse str2 reverse(str2); // output cout << "The strings after reversing: " << endl; cout << str1 << " " << str2 << endl; return 0; } void reverse(string str) { int length = str.size(); char *first = NULL; char *last = NULL; first = &str[0]; last = &str[length - 1]; for (int i = 0; first < last; i++) { swap(first, last); first++; last--; } } void swap(char *first, char *last) { char * temp; *temp = *first; *first = *last; *last = *temp; } 
+4
source share
5 answers

In your swap function, you assign the value *temp when temp does not indicate anything (it is not initialized). So your segmentation error.

Do you want to:

 void swap(char* first, char* last) { char temp = *first; *first = *last; *last = temp; } 
+3
source

I don't know where line 26 is, but

 char * temp; *temp = ... 

not valid. temp should point to a char or (even better) rewrite the function to where temp is a char .

Seth Carnegie notices that you need to pass string by reference if you want to change the originals.

 void reverse(string& str) { //pass by reference, so origional is modified 
+6
source

You pass strings by value, which means that only a local copy of the string will be undone in the reverse function. You must pass them by reference.

Also, do not modify direct memory directly. Use operator[] as follows:

 for (size_t beg = 0, size_t end = str.size() - 1; beg < end; ++beg, --end) str[beg] = str[end]; 

So, all together:

 void reverse(string& str); // prototype .... void reverse(string& str) { // note the & which is pass by reference int length = str.size(); for (size_t beg = 0, size_t end = str.size() - 1; beg < end; ++beg, --end) str[beg] = str[end]; } 

And as Mooing Duck pointed out, the place where you are likely to crash is looking for a pointer that has garbage value here:

 char * temp; *temp = ... 

You are trying to assign some random memory to a value that is likely to rob you.

+2
source

Other answers are valid regarding the reason for segfault.

I just think you might be interested to know that you can easily change the string using std::string reverse_iterator :

 std::string reverse(std::string str) { std::string out; for (std::string::reverse_iterator it = str.rbegin(); it != str.rend(); it++) { out += *it; } return out; } 

So by calling:

 reverse("foo"); 

... will return oof .

+2
source

Again, others indicated what the problem was and would like to show you the following:

 void reverse(std::string & str) { for (int i = 0, last = str.size() - 1, lim = str.size() / 2 ; i < lim;) { std::swap(str[i++], str[last--]); } } 

I have not tested it completely, though.

0
source

Source: https://habr.com/ru/post/1381607/


All Articles