Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

arrays - Triggering a breakpoint while deleting memory C++

In my task I need to input some data about, let's say, depositor and then increase his amount of money by 15% but that`s not the point. The main problem is that it always "triggers a breakpoint" and transfers me to this page enter image description here

Function where I allocate memory:

void allocateMemory(char*** szData, const int rows, const int words, const int max) { 
    for (int i = 0; i < rows; i++)
    {
        szData[i] = new char* [words]; //how many words in every row
        for (int j = 0; j < words; j++)
        {
            szData[i][j] = new char[max]; //maximum quantity of symbols in every word
        }
    }
}

Function where I assign some values:

void assignFirst(char*** szData, const int rows, const int max) { 

    char* s1 = new char[max];  
    char* s2 = new char[max];
    char* s3 = new char[max];
    char* s4 = new char[max];
    char* s5 = new char[max];

    cin >> s1 >> s2 >> s3 >> s4 >> s5;

    for (int i = 0; i < rows; i++)
    {
        szData[i][0] = s1;
        szData[i][1] = s2;
        szData[i][2] = s3;
        szData[i][3] = s4;
        szData[i][4] = s5;
    }
}

Function where I delete memory:

void freeMemory(char*** szData, const int rows, const int words) { 
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < words; j++)
        {
            delete[] szData[i][j];
        }
    }

    for (int i = 0; i < rows; i++)
    {
        delete[] szData[i];
    }

    delete[] szData;
}

How it looks in the console and where it breaks enter image description here How can I solve it? P.S. cannot do it using string (must be an array of characters) or vector as it is the condition of my task I was given. And yes, I know that new is sort of outdated

Full code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <conio.h> 

using namespace std;

void allocateMemory(char*** szData, const int rows, const int words, const int max);
void assignFirst(char*** szData, const int rows, const int max);
void print(char*** szData, const int rows, const int words);
void freeMemory(char*** szData, const int rows, const int words);

void main() {
    const int rowCount = 2;
    const int wordCount = 5; //number of words in every row
    const int maxWordLength = 10; 

    char*** szData = new char** [rowCount];

    allocateMemory(szData, rowCount, wordCount, maxWordLength);

    assignFirst(szData, rowCount, maxWordLength);
    print(szData, rowCount, wordCount);

    freeMemory(szData, rowCount, wordCount);
}

void allocateMemory(char*** szData, const int rows, const int words, const int max) { 
    for (int i = 0; i < rows; i++)
    {
        szData[i] = new char* [words]; //how many words in every row
        for (int j = 0; j < words; j++)
        {
            szData[i][j] = new char[max]; //maximum quantity of symbols in every word
        }
    }
}

void assignFirst(char*** szData, const int rows, const int max) { 

    char* s1 = new char[max];  
    char* s2 = new char[max];
    char* s3 = new char[max];
    char* s4 = new char[max];
    char* s5 = new char[max];

    cin >> s1 >> s2 >> s3 >> s4 >> s5;

    for (int i = 0; i < rows; i++)
    {
        szData[i][0] = s1;
        szData[i][1] = s2;
        szData[i][2] = s3;
        szData[i][3] = s4;
        szData[i][4] = s5;
    }
}

void print(char*** szData, const int rows, const int words) { 
    for (int i = 0; i < rows - 1; i++) 
    {
        for (int j = 0; j < words; j++)
        {
            if (j == 3) //change fourth element
            {
                double a = atoi(szData[i][3]); //convert fourth element into double
                a = a * 1.15; //add 15% to it
                cout << a << " ";
            }
            else
                cout << szData[i][j] << " ";
        }
        cout << endl;
    }
}

void freeMemory(char*** szData, const int rows, const int words) { 
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < words; j++)
        {
            delete[] szData[i][j];
        }
    }

    for (int i = 0; i < rows; i++)
    {
        delete[] szData[i];
    }

    delete[] szData;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The bug is here

void assignFirst(char*** szData, const int rows, const int max) { 

    char* s1 = new char[max];  
    ...
    for (int i = 0; i < rows; i++)
    {
        szData[i][0] = s1;
        ...
    }
}

You assign the same pointer to multiple entries of szData. But here

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < words; j++)
    {
        delete[] szData[i][j];
    }
}

you delete those pointers as if they are all separately allocated.

In other words you are deleteing the same pointer multiple times, and that explains the error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...