我有视觉工作室错误调试断言失败
i have visual studio error debug assertion failed
在Visual Studio程序中崩溃:错误调试断言失败。我的代码出了什么问题?没有语法错误。唯一的警告:删除数组表达式,转换为指针 当我使用 cmd 标准编译器运行时,它工作正常。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#define max_size 100
class my_array{
int* arr[max_size];
public:
my_array() {}
my_array(int size) {
if (size <= 0 || size > max_size) {
exit(1);
}
for (int i = 0; i < size; i++) {
arr[i] = new int;
cout << "Enter element [" << i + 1 << "] = ";
cin >> *arr[i];
}
}
~my_array() {
delete[] arr;
}
void operator[](int n) {
cout << endl;
for (int i = 0; i < n; i++) {
cout << "Enter element [" << i << "] = " << *arr[i] << endl;
}
}
};
int main() {
my_array array(6);
array[5];
return 0;
}
您在此处删除arr
:
delete[] arr;
而arr
从未被new
分配.在您的原始程序中,arr
是一个固定大小的指向int
的指针数组。
你可能想要这个:
class my_array {
int *arr;
public:
my_array() {}
my_array(int size) {
if (size <= 0 || size > max_size) { // BTW this test isn't really necessary, as we
// can allocate as much memory as available
// anyway much more than just 100
exit(1);
}
arr = new int[size]; // allocate array of size size
for (int i = 0; i < size; i++) {
cout << "Enter element [" << i + 1 << "] = ";
cin >> arr[i];
}
}
~my_array() {
delete[] arr; // delete array allocated previously
}
void operator[](int n) {
cout << endl;
for (int i = 0; i < n; i++) {
cout << "Enter element [" << i << "] = " << arr[i] << endl;
}
}
};
您没有固定大小的指向 int 的指针数组,而是有一个 int
的动态数组。
不过,仍有改进的余地。例如my_array()
构造函数在这里毫无意义,使用[]
运算符打印内容很奇怪,[]
运算符中的文本"Enter element ["
也是值得怀疑的。
相关文章:
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 使用扫描的调试断言失败
- 图片不显示,关闭时出错 --> 调试断言失败!表达式:is_block_type_valid(标头>_block_use)
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 调试断言失败的缓冲区!=nullptr
- 获取有关调试断言失败的错误:表达式:"(_Ptr_user &(_BIG_ALLOCATION_ALIGNMENT -1)) == 0" &&0
- 在Win10上使用带有Qt5的OpenCV3.0,调试断言失败
- C++,弹出调试断言失败窗口,我得到矢量迭代器不兼容的错误运行时
- C++ 调试断言失败,无效的空指针
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 调试断言仅在某些情况下失败?C++
- 调试断言失败
- 映射/集迭代器不可取消引用 (C++) - 调试断言失败
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 使用 Microsoft VS 编写程序C++,但我在这里收到调试断言消息.它可以在 cpp.sh 上运行,repl.i
- 字符串抛出矢量下标调试断言
- 使用调试断言时避免 ODR 冲突
- MFC DDX_RADIO会导致调用dodataexchange(dlgdata.cpp行286)时导致调试断言失败
- 无法在谷歌测试中捕获调试断言(死亡测试不满足它)
- 调试断言失败的矢量下标超出C 范围