删除内存C++时触发断点
Triggering a breakpoint while deleting memory C++
在我的任务中,我需要输入一些关于存款人的数据,然后将他的金额增加 15%,但这不是重点。主要问题是它总是“触发断点”并将我转到此页面
分配内存的函数:
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 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;
}
它在控制台中的外观以及出现问题的地方
我该如何解决?
P.S。不能使用 string
(必须是字符数组)或 vector
来完成,因为这是我的任务条件。是的,我知道 new
有点过时了
完整代码:
#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;
}
错误在这里
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;
...
}
}
您将同一个指针分配给 szData
的多个条目。但是这里
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < words; j++)
{
delete[] szData[i][j];
}
}
你删除那些指针,就好像它们都是单独分配的一样。
换句话说,您多次删除同一个指针,这就解释了错误。
在我的任务中,我需要输入一些关于存款人的数据,然后将他的金额增加 15%,但这不是重点。主要问题是它总是“触发断点”并将我转到此页面
分配内存的函数:
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 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;
}
它在控制台中的外观以及出现问题的地方
string
(必须是字符数组)或 vector
来完成,因为这是我的任务条件。是的,我知道 new
有点过时了
完整代码:
#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;
}
错误在这里
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;
...
}
}
您将同一个指针分配给 szData
的多个条目。但是这里
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < words; j++)
{
delete[] szData[i][j];
}
}
你删除那些指针,就好像它们都是单独分配的一样。
换句话说,您多次删除同一个指针,这就解释了错误。