【发布时间】:2021-01-05 20:57:34
【问题描述】:
在我的任务中,我需要输入一些关于存款人的数据,然后将他的金额增加 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;
}
【问题讨论】:
-
这意味着您的程序有错误,您正在破坏堆,并且在释放内存时会检测到。
-
可能不是您想听到的,但是如果您使用标准 C++ 类(例如
std::vector和std::string)而不是尝试进行手动内存分配,您会发现编写起来要容易得多无错误代码。 -
好的,这不是你的选择,但你知道this吗?我不得不质疑你是如何被教导的。这在 C 中已经够糟糕了,但据说你正在学习 C++
-
是的,不幸的是,我已经多次被告知。虽然不知道那个链接。我还认为我不能只学习
vector和所有这些,直到我学会如何手动使用“动态”东西或类似的东西 -
动态的东西很难。你应该先学习简单的东西,这是常识,编程本身就够难了。但出于某种原因,许多教师更喜欢跟随 C++ 语言的历史发展,而不是常识。