I am trying to create a simple string class, but i am getting a run time error. What i want to accomplish is to store a const char pointer to a char pointer. I am trying to use the strncpy function, i also tried the memcpy, but both gave the apparently the same error. The src argument is getting a value, however dst isn't, it says it's a bad pointer. I also tried using strcpy, but i can't allocate memory with malloc, it says that i can't assing void pointer to char pointer.

This is my header:

class MyString{
private:
    char* thestring;
public:
    MyString();
    MyString(const MyString& str);
    MyString(const char * str);
    ~MyString();
    MyString& operator=(const MyString& str);
    MyString& operator=(const char * str);
};

This is my cpp:

MyString::MyString(){
    thestring = '\0';
}

MyString::MyString(const MyString& str){
    thestring = strupr(str.thestring);
}

MyString::MyString(const char * str){
    memcpy(thestring,str,strlen(str) + 1);
    //strncpy(thestring,str,sizeof *str + 1);
}

MyString& MyString::operator=(const MyString& str){
    if (this != &str){
        free(thestring);
        thestring = strupr(str.thestring);
    }
    return *this;
}

MyString& MyString::operator=(const char * str){
    if (&this->thestring != &str){
        //strncpy(thestring,str,sizeof *str + 1);
        memcpy(thestring,str,strlen(str + 1);
    }
    return *this;
}

MyString::~MyString(){
    free(thestring);
}

Edited 3 Years Ago by daniel.s.mattos: i'm stupid

I'll answer your question with another question:

free(thestring);
memcpy(thestring,str,strlen(str) + 1);

Where does thestring point after you call free?

Edited 3 Years Ago by deceptikon

My fault, I should've remove that part. I had that call there because i tried to use allocate memory with malloc, which compiler is complaning, even tho i saw samples like that and it seemed like it wouldn't give me a error. But the result is the same with it there or not.

Post your code using malloc, because that's what you need. The process is as follows:

  1. For new objects, call malloc to allocate new memory.
  2. For existing objects, call malloc to allocate new memory, copy the data, then free the old memory.
  3. Always call free to release memory in the destructor.

strupr typically doesn't allocate memory for you, so that doesn't count.

i tried this line;

thestring = malloc(sizeof str + 1);

But i get this compiler error:

Error   2   error C2440: '=' : cannot convert from 'void *' to 'char *'

I got it working, i mean not to crash. I was using the = operator that i had over-written. I noticed that once i changed the value of str, it would work once and then crash after the first run, which i assumed that it wasn't deallocating the memory and doing the other steps. I remove the if statement and now it seems that it stop complaning.

This is what I modified:

MyString::MyString(const char * str){
    thestring = NULL;
    free(thestring);
    thestring = (char*)malloc(sizeof str + 1);
    memcpy(thestring,str,strlen(str) + 1);
}

MyString& MyString::operator=(const char * str){
    thestring = NULL;
    free(thestring);
    thestring = (char*)malloc(sizeof str + 1);
    memcpy(thestring,str,strlen(str) + 1);
    return *this;
}

What is complaning? Do you mean complaining? I have never heard of complaning :/

Edited 3 Years Ago by kal_crazy

This article has been dead for over six months. Start a new discussion instead.