Thursday, 15 March 2012

String reversal in C++ getting 'Segmentation fault (core dumped) error -


This gives a 'segmentation fault' (core dump) error after printing the strings I mentioned below Could?

  #include & lt; Iostream & gt; #include & lt; String & gt; # Include & lt; Cstdio & gt; using namespace std; Zero print (char * str) {if (* str) {print (str + 1); Cout & lt; & Lt; * Str; }} Int main () {char * str; Cin & gt; & Gt; Str; Print (STR); Cout & lt; & Lt; Endl; Return 0; }  

Unspecified non-static local variable has uncertainty The price, and the reality appears to be random, without the initialization of it, it is used, which is one of the most common causes of the crash.

The problem is that you have an indicator, but it does not indicate anywhere when you use it to read the input, input stream cin in a random memory Will write on the place.

The solution is not to use the character indicator for the string, but the square.


If you is to use indicators, then you have two solutions: either declare the string as an array, or Allocate memory using the new operator. However, if you take the precautions, as much as you have allotted, you will write out of the limit and then once is undefined behavior .


No comments:

Post a Comment