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);
}

Recommended Answers

All 8 Replies

I'll answer your question with another question:

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

Where does thestring point after you call free?

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 *'

thestring is char* whereas malloc is void*.

Try type-casting.

thestring = (char*)malloc(sizeof str +1);

An example of using malloc: Click Here

I had cast it once i saw that error, i got another type of crash.

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 :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.