0
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

class MyString
{
    char* str;
    int size;
public:
    MyString()
    {
        str = '\0';
        size = 1;
    }
    MyString(const char* const s)
        : str(new char[strlen(s)])
    {
        size = strlen(s) + 1;
        strcpy(str, s);
    }
    MyString(const MyString& another)
        : str(new char[another.size])
    {
        size = another.size;
        for (int i = 0; i < size; i++)
            str[i] = another.str[i];
    }
    ~MyString()
    {
        delete[] str;
    }
    void set(const char* st)
    {
        size = (int)strlen(st) + 1;
        str = new char[size];
        for (int i = 0; i < size; i++)
            str[i] = st[i];
    }
    bool isEqual(const MyString& other) const
    {
        if (size != other.size)
            return false;
        if (strcmp(str, other.str) == 0)
            return true;
        else
            return false;
    }
    void print() const
    {
        for (unsigned int i = 0; i < size; i++)
            cout << str[i];
        cout << endl;
    }
};

int main() {
    MyString strs[] = {
        MyString("C"),
        MyString(),
        MyString("Java")
    };
    strs[1].set("C++");

    const int arraySize = sizeof(strs) / sizeof(MyString);

    const MyString target("Java");
    for (int i = 0; i < arraySize; i++) {
        const MyString str(strs[i]); // copy constructor
        if (str.isEqual(target)) {
            cout << "[" << i << "]: ";
            str.print();
            break;
        }
    }
    for (const MyString& str : strs) {
        str.print();
    }
}

In dev c++ it's working and there was no caution, but in visual studio 2019, catution like "heap corruption detected after normal block crt detected that the application" is occured. I don't what is problem.

When I debuged my code, it successfully worked befor the main fuction finished. When destructor worked, the caution was appeared.

Please help me :)

iamianpark
  • 21
  • 3

2 Answers2

1

Your default constructor should not compile with recent GCC compilers, you can only assign NULL or nullptr to pointer

  MyString() {
    str = nullptr;
    size = 0;
  }

This constructor needs one extra 1 byte to allocate memory, then the buffer overflow can be avoided.

  MyString(const char* const s) : str(new char[strlen(s) + 1]) {
    size = strlen(s) + 1;
    strncpy(str, s, size);
  }

strncpy is a safer replacement for strcpy, I suggest you use this one.

You also need to add implementation for operator=, or the compiler-generated one will cause a memory leak once you use operator= to copy an object.

  MyString& operator=(const MyString& another) {
    if (&another == this) return *this;
    delete[] str;
    str = new char[another.size];
    size = another.size;
    // better to use strncpy here
    for (int i = 0; i < size; i++) str[i] = another.str[i];
    return *this;
  }

A similar string implementation is discussed in this question, you may also reference the code there.

Demo

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
  • ahhhhhhh yayyyy thanks a lot. I corrected my code at MyString(const char* const s) constructor with initializer str(new char[strlen(s) + 1]). This was an simple error... Thanks a lot and have a nice day! -From South Korea Student :) – iamianpark Apr 15 '21 at 11:40
  • @iamianpark Glad to hear that! – prehistoricpenguin Apr 15 '21 at 13:05
0

One problem is the initialization of the members in the default constructor:

str = '\0';
size = 1;

The character literal '\0 is equal to zero, which is equal to a null pointer. That assignment is equivalent to:

str = nullptr;

And yet you still say that the size of the string is 1, which is wrong since there is no string and the size should be zero.


Another (and the likely culprint of your problem) problem is that in your constructor MyString(const char* const s) you use strlen to get the length of a C-style null-terminated string for the allocation, but you forget that strlen doesn't count the null-terminator.

This means the allocation will be too small, and when you use strcpy to copy the string it will write the null-terminator out of bounds of your allocated memory.


Furthermore, you in other places count with the null-terminator, and actually set the size to include the null-terminator (even in the MyString(const char* const s) constructor). This goes against the normal C++ semantics of strings where the size doesn't include the terminator.

This could then lead to further problems, like in your print function where you actually print the null-terminator.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks a lot! I also corrected my code at constructor MyString(const char* const s) : str(new char[strlen(s) + 1]) like this! This initializer was main problem i think. Thanks for your good comment and have a nice day! -From South Korea – iamianpark Apr 15 '21 at 11:42