user3868762
user3868762

Reputation: 15

Access Violation Pointer Error

I am implementing my version of the basic String class, however I am running into an issue that I have never seen before and have no idea how to properly debug. My code is pasted below. All functions have their header counterparts. My test is simply creating one object using the convert constructor.

A4String obj1("this");

My problem is I get an Access violation reading location exception thrown. My research has indicated that I may be trying to access memory outside of Visual Studio's allotment. I'm having trouble finding where this pointer error exists though. I have placed breakpoints through every step of the convert constructor and subsequent function calls within however my program doesn't throw the exception until it returns to main, seemingly after my program has executed completely.

#include "A4String.h"



A4String::A4String() {
    data = new char[5];
    data[0] = '\0';
    capacity = 5;

}

A4String::~A4String() {
    if (capacity != 0)
        delete[] data;

}
//Copy Constructor
A4String::A4String(const A4String &right) {
    cout << "copy" << endl;

    data = new char[right.capacity + 1];
    strcpy(data, right.data, capacity);
    capacity = right.capacity;

}
//Convert Constructor 
A4String::A4String(const char *sptr) {
    cout << "convert" << endl;
    capacity = (strlen(sptr)) + 1;
    data = new char[capacity + 1];
    strcpy(sptr, data, capacity);

}
//Assignment
A4String& A4String::operator = (const A4String & right) {
    //if (capacity != 0) delete[] data;
    data = new char[right.capacity + 1];
    strcpy(data, right.data, capacity);
    capacity = right.capacity;
    return *this;
}
//Equivalence
bool A4String::operator == (const A4String &right) const {
    return (strcmp(data, right.data)) == 0;

}

int A4String::length() const {
    return capacity;
}

void A4String::addChar(char) {
    //Not implemented yet
}

string A4String::toString() {
    string str = "";
    int i = 0;
    while (data[i] != '\0') {
        str += data[i];
        i++;
    }
    return str; 
}

void A4String::strcpy(const char *source, char* destination, int size)
{
    for (int i = 0; i < 20; i++)
        destination[i] = '\0';
    int  index = 0;
    while (source[index] != '\0')
        {
        destination[index] = source[index];
        index++;
        }
    destination[index] = '\0';


}

int A4String::strcmp(char *str1,  char *str2)
{

    if (*str1 < *str2)
        return -1;

    if (*str1 > *str2)
        return 1;

    if (*str1 == '\0')
        return 0;

    return strcmp(str1 + 1, str2 + 1);

     return 0;
     }

int A4String::strlen( char *s)
{
    char *start;
    start = s;
    while (*s != 0)
    {
        ++s;
    }
    return s - start;
}

Upvotes: 0

Views: 66

Answers (2)

R Sahu
R Sahu

Reputation: 206577

Use of the hard code number 20 in the A4String::strcpy is not right. I suggest changing it to size.

void A4String::strcpy(const char *source, char* destination, int size)
{
    // for (int i = 0; i < 20; i++)
    for (int i = 0; i < size; i++)
        destination[i] = '\0';
    int  index = 0;

    // Add an additional check here also.
    // while (source[index] != '\0' )
    while (source[index] != '\0' && index < size)
        {
        destination[index] = source[index];
        index++;
        }
    destination[index] = '\0';
}

Disclaimer Fixing the above function may not fix your crashing problem even though the use of 20 is most likely crashing your program. In other words, there might be other problems in your code too.

Upvotes: 0

ead
ead

Reputation: 34326

The problem is your A4String::strcpy, the line

for (int i = 0; i < 20; i++)
    destination[i] = '\0';

The destination has less than 20 characters, so it crashes.

Upvotes: 2

Related Questions