简体   繁体   中英

C++ 'C String' implementation has a memory allocation issue

I've been struggling with this for quite a while, it should be straight forward. It's the start of a simple 'C String' implementation, which are all over the internet. But, mine doesn't work... When it reaches ~CString() and calls delete[] _data; it crashes. I don't know why.

Here's CString.h

#ifndef CSTRING_H
#define CSTRING_H

#include <cstring> // strlen

class CString
{

    char* _data;
    unsigned int _size;
    unsigned int _length;

public:
    CString();
    CString(const char* data_);
    CString(const char* data_, unsigned int size_);
    CString(const CString& other_);

    ~CString();

    const char* get_data() const;
    unsigned int get_size() const;
    unsigned int get_length() const;

    void assign();
    void assign(const char* data_);
    void assign(const char* data_, unsigned int size_);

};

#endif // CSTRING_H

Here is CString.cpp

#include "./CString.h"

CString::CString()
{
    _data = 0;
    _size = 0;
    _length = 0;
}

CString::CString(const char* data_)
{
    _size = strlen(data_) + 1;
    _length = strlen(data_);
    _data = new char(_size);
    strncpy(_data, data_, _size);
}

CString::CString(const char* data_, unsigned int size_)
{
    _size = size_;
    _length = size_;
    _data = new char(_size);
    strncpy(_data, data_, _size);
}

CString::CString(const CString& other_)
{
    _size = other_._size;
    _length = other_._length;
    _data = new char(_size);
    strncpy(_data, other_._data, _size);
}

CString::~CString()
{
    if (_data)
    {
        delete[] _data;
    }
}

const char* CString::get_data() const
{
    return _data;
}

unsigned int CString::get_size() const
{
    return _size;
}

unsigned int CString::get_length() const
{
    return _length;
}

void CString::assign()
{
    if (_data)
    {
        delete[] _data;
    }
    _size = 0;
    _length = 0;
    _data = 0;
}

void CString::assign(const char* data_)
{
    if (_data)
    {
        delete[] _data;
    }
    _size = strlen(data_) + 1;
    _length = strlen(data_);
    _data = new char(_size);
    strncpy(_data, data_, _size);
}

void CString::assign(const char* data_, unsigned int size_)
{
    if (_data)
    {
        delete[] _data;
    }
    _size = size_;
    _length = size_;
    _data = new char(_size);
    strncpy(_data, data_, _size);
}

And finally, here is all main.cpp is doing:

int main(int argc_, char** argv_)
{
    CString info_message("Hello world!");
}

This seems like such a simple task, but I've been stuck on it for hours! Maybe it's a puzzle to someone else, or maybe it's really a simple fix. I don't know. Thanks for any help in advance.

_data = new char(_size);

This is creating a single character, whose value is _size .

_data = new char[_size];

This is probably what you want. It constructs an array of characters of total length _size .

Have you tried replacing

_data = new char(_size);

with

_data = new char[_size];

?

If I'm not mistaken, [] is normally used when initializing an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM