-1

I don't know, problem with this error. However, I think I should delete uName[] before I delete the []cpp. How can I do for this?

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string.h>

using namespace std;
class cppUr
{
public:
    void writeName(char nm[]);
    char * readName();

private:
    char uName[80];
};
void cppUr::writeName(char nm[])
{
    strncpy(uName, nm, 79);
}
char * cppUr::readName()
{
    return uName;
}

The main is:

int main()
{
    char name0[100];
    char name1[100];
    char name2[100];
    char name3[100];
    cppUr *cpp = new cppUr[3];
    cout << "Input first name: "<<endl;
    cin.getline(name0, 100);
    cpp[0].writeName(name0);
    cout << "Input second name: " << endl;
    cin.getline(name1, 100);
    cpp[1].writeName(name1);
    cout << "Input third name: " << endl;
    cin.getline(name2, 100);
    cpp[2].writeName(name2);
    cout << "Input fourth name: " << endl;
    cin.getline(name3, 100);
    cpp[3].writeName(name3);
    for (int i = 0; i < 4; i++)
    {
        cpp[i].readName();
        cout << "The "<<i<<" name " << cpp[i].readName() << endl;
    }
    delete[] cpp;
    system("PAUSE");
    return 0;
}

The error is:

HEAP CORRUPTION DETECTED: after Normal block(#148) at 0x0059E1E0. CRT detected that the application wrote to memory after end of heap buffer

Rajeev Atmakuri
  • 888
  • 1
  • 10
  • 22
  • 2
    You allocate 3 `cppUr` but then use 4. – tkausl Nov 13 '18 at 18:06
  • 2
    And you should _not ever_ delete `uName`. It is statically allocated, not dynamically. There is also no reason to dynamically allocate `cpp` and if you are learning C++, you should use `std::string` instead of all the `char[]`, `char*`, `strncpy` and manual dynamic memory management. –  Nov 13 '18 at 18:11
  • 2
    This would be much simpler with `std::string` instead of C-style strings. – Pete Becker Nov 13 '18 at 18:19
  • 1
    Note that `strncpy` is **not** a "safe" replacement for `strcpy`, for any reasonable definition of "safe". Read its [documentation](https://en.cppreference.com/w/cpp/string/byte/strncpy) carefully. – Pete Becker Nov 13 '18 at 18:20

1 Answers1

0

The error is very simple.

You create array from ccpUr with 3 elements, but after use 4 of them.

Of course the c++ is null based index language, but that means the last is not useable. That is why in your for cycle condition must be use the same number in like in array allocation which is the 3.

Think about it, when [3] is enough for 4 element then the [2] is enough for 3 element then [1] is enough for 2 element, and [0] is enough for 1 element? Do you feel it? The zero size array is not enough for nothing.

regards

György Gulyás
  • 1,290
  • 11
  • 37
  • But array is starting from 0? When I create array [3], it should be include [0], [1], [2],[3] and total 4 ? – wong waiman Nov 13 '18 at 20:08
  • no, it is not true. If you have right, the array[0] is still contains one element. when you define array[3] the index3 must be not used. in the for condition you have to use i < 3. It means the 3 will be not used. – György Gulyás Nov 14 '18 at 11:07
  • Thank you, I understand the logic – wong waiman Nov 17 '18 at 21:37