-11

Good day, Please can someone assist? I am trying to reverse a member that is a pointer array.

string::string(char *c)
{
    int i =0;
    while ((c[i] != '\0'))
    {
        i++;
    }
    _lenght= i;
    _aString=*&c;
}

void string::reverse() //Requirements specify to have this argument
{
          for(int i=0; i<_lenght/2; i++)
    {
        std::swap(_aString[i], _aString[_lenght-i-1]);
    }

}

I get a runtime error on this.

This is my main function

   int main(){
       string a;
       std::cout << "a is " << a << "\n";
       string b("12345");
       string c("12345",3);
       std::cout << "c is " << c << "\n";
       c = a;
        a = b;
       std::cout << "a is " << a << "\n";
       b.reverse();
       std::cout << "a is " << a << "\n";
       return 0;
   }

Error I'm getting is

Unhandled exception at 0x00fd6710 in UnisaLesson1.exe: 0xC0000005: Access violation writing location 0x00fdfd08.

Sorry, I'm still a newb.

  • 1
    `std::reverse(_aString, _astring + length);`, where length does not include the null terminator. – juanchopanza Sep 22 '14 at 14:35
  • 1
    `_aString` was set (in a bizarre way) to be a copy of the pointer `c`, and probably points to either something that's been destroyed, or a (constant) string literal. But without seeing what `c` pointed to when the `string` was created, we can only guess. Try to write a minimal, but complete, test case to demonstrate the problem. – Mike Seymour Sep 22 '14 at 14:36
  • You need to allocate memory in the constructor and copy the character array you've been passed. Don't forget to deallocate in the destructor. – n. m. could be an AI Sep 22 '14 at 14:38
  • @juanchopanza, you are correct. I also think calling a function string is totally unacceptable, but I did not create the assignment – Leonie Kruger Sep 22 '14 at 14:46
  • @LeonieKruger, what is the definition of `_aString`? – Niall Sep 22 '14 at 14:49
  • I suspect it is your memory allocation for `_aString` (not being shown). It will need a `new char[size]` where `size` will be big enough to hold the string (and reallocated) if it is not big enough – Niall Sep 22 '14 at 14:53

1 Answers1

1

The problem is

_aString=*&c;

which is a weird way of writing

_aString=c;

so that _aString is simply pointing to whatever the function argument was; this class doesn't manage a the memory for a string (as std::string does), but refers to an external string managed elsewhere.

In your test case, that string is a string literal - which isn't modifiable. So attempting to modify it, as reverse does, gives undefined behaviour; if you're lucky, an access violation; if you're unlucky, data corruption or some other unwanted runtime behaviour.

If your class is supposed to manage the memory, then it will need to allocate it in the constructor. Assuming this assignment is an exercise in memory management (otherwise, just use std::string), you'd want something like

_aString = new char[_lenght];
std::copy(c, c+_lenght, _aString);

and don't forget the Rule of Three:

  • a destructor to release the memory with delete [] _aString
  • copy constructor, either deleted to prevent copying, or implemented to allocate a new buffer;
  • copy-assignment operator, either deleted to prevent copying, or implemented to copy into the existing buffer after resizing if necessary.

You might also consider fixing the spelling of _lenght to _length.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644