0

I am getting "Access violation" error but unable to identify the root cause for it . I am new to c++ and preparing for C++ coding contest as a beginner. Please help me to get the root cause for it. Please find the attached screenshot.

Thanks in advance .

Here is my code in a single .cpp file.

#include <iostream>
using namespace std;
void reverse(char *str) {
    char * end = str;
    char tmp;
    if (str) {
        while (*end) {
            ++end;
        }
        --end;
        while (str < end) {
            tmp = *str;
            *str++ = *end; //Getting exception here
            *end-- = tmp;
        }
    }
}

int main ()
{
    char *str="Test";
    cout << "Before change"<<str;     // prints Hello World!
    reverse(str);
    cout << "After change"<<str; // prints I'm a C++ program
    getchar();
}
Viku
  • 2,845
  • 4
  • 35
  • 63
  • 1
    You can't write to string literals, `char *str="Test";` is the root cause and isn't valid C++, I'm surprised your compiler isn't complaining it should be `char const* str ="Test";`. – user657267 Apr 10 '16 at 08:25
  • Actually char* str="Test" is allowed in c++. String literal are converted to const char* type. But the value pointed by str can not be changed because it is a constant. – Biruk Abebe Apr 10 '16 at 08:28
  • 1
    @bkVnet It's been deprecated since C++03, and it's invalid as of C++11. – user657267 Apr 10 '16 at 08:34
  • @user657267 You are right but shouldn't it be const char* str="Test" rather than char const* str="Test"( as in your first comment) because the second one will make the pointer constant and not the value it is pointing to. – Biruk Abebe Apr 10 '16 at 08:42
  • 2
    @bkVnet There is no difference between `char const*` and `const char*`, they are the same type. You're thinking of `char* const`. – user657267 Apr 10 '16 at 09:11
  • @bkVnet Thanks for your reply . One more clarification . Lets say i initialized it as a string array. So when i pass the same to reverse function it should throw exception as it accepts char *str which again going to be modified later inside the function – Viku Apr 10 '16 at 09:22
  • 1
    @vivek if you do it like the given answer char str[]="test", then what you are passing is not a const char* but char* which should work fine. – Biruk Abebe Apr 10 '16 at 09:25
  • @user657267 Wow, never paid attention to that detail, thanks!! – Biruk Abebe Apr 10 '16 at 09:26
  • @bkVnet Thanks mate . You made my day !! – Viku Apr 10 '16 at 09:26

2 Answers2

2

You are getting a segmentation fault, because you are trying to edit the read-only memory where the literal string "Test" is stored (since string literals are const char* in c++).

In order to make your code run, you can store the literal string in a char array so the first line in main() will look like this:

char str[] = "Test";
Mike
  • 8,055
  • 1
  • 30
  • 44
  • Thanks for your reply . One more clarification . Lets say i initialized it as a string array. So when i pass the same to reverse function it should throw exception as it accepts char *str which again going to be modified later inside the function. – Viku Apr 10 '16 at 09:19
  • do you mean a `char []` by *string array*? – Mike Apr 10 '16 at 09:23
  • Yes . like this . char str[] = "Test"; – Viku Apr 10 '16 at 09:25
  • then you don't have to change your function's prototype as conversion from array to pointer is done automatically for you, see [this](http://stackoverflow.com/questions/4223617/standard-conversions-array-to-pointer-conversion) – Mike Apr 10 '16 at 09:25
  • as I mentioned in the answer, the only thing you need to change in order to make the program run is the line `char *str="Test";` – Mike Apr 10 '16 at 09:27
  • Thanks mate . Now i got the whole thing!! – Viku Apr 10 '16 at 09:29
0

The problem is that the type of string constants by default in C++ is const char *.

In the following line, you assign a non-const char * to a const char *.

char *str="Test";

Thus, when you pass this variable to the reverse function, you are attempting to write to read-only memory. This violates the rules of C++, and leads to unexpected behaviour.

Define your string like this instead to solve the problem:

char str[] = "Test";

This gives you the ability to modify the string, as it isn't a const anymore.

Deus Sum
  • 146
  • 2