void test() { int*_ptr=new int(1); if(_ptr) { throw 1; } delete _ptr; }
时间: 2023-12-13 19:03:53 浏览: 21
这段代码中,当 _ptr 指针成功申请了内存后,如果在后续的操作中出现了异常,会执行 throw 语句,这时候会跳过后续的 delete 操作,但是因为 _ptr 已经被定义了,所以需要在 catch 块中手动释放内存。如果 _ptr 指针没有成功申请内存,则不需要进行释放操作,因为没有成功申请内存的指针不需要进行释放。
相关问题
#include <iostream> using namespace std; class IndexError{}; template class ARRAY { size_t m_size; T *m_ptr; public: ARRAY(size_t size) : m_size(size) { m_ptr = new T[size]; memset(m_ptr, 0, size*sizeof(int)); } ~ARRAY() { delete[] m_ptr; } T& at(int index); }; template <typename T> ::at(int index) { if(index<0|| ) { IndexError(); } return m_ptr[index]; } int main() { ARRAY<int> a(50); int i; cin >> i; { for(int j=0;j<i;j++) a.at(i) = j; } catch(IndexError e) { return 0; } return 0; }
这段代码有一些问题,我来帮您指出:
1. 在模板类 `ARRAY` 的函数 `at` 中,没有指定返回类型。
2. 在 `at` 函数中,如果下标越界,应该抛出异常,但是现在抛出异常的方式是通过调用一个函数 `IndexError()`,这是错误的。正确的方式应该是使用 `throw` 抛出一个异常对象。
3. 在 `main` 函数中,`for` 循环中的下标应该从 0 开始,而不是从 `i` 开始,这样才能正确地给数组赋值。
下面是修改后的代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class IndexError{};
template <typename T>
class ARRAY {
size_t m_size;
T *m_ptr;
public:
ARRAY(size_t size) : m_size(size) {
m_ptr = new T[size];
memset(m_ptr, 0, size*sizeof(T));
}
~ARRAY() {
delete[] m_ptr;
}
T& at(int index) {
if (index < 0 || index >= m_size) {
throw IndexError();
}
return m_ptr[index];
}
};
int main() {
int i;
cin >> i;
try {
ARRAY<int> a(i);
for (int j = 0; j < i; j++) {
a.at(j) = j;
}
}
catch (IndexError e) {
return 0;
}
return 0;
}
```
这样就可以正确地创建一个 `ARRAY` 对象并设置其中的元素了。如果下标越界,会抛出 `IndexError` 异常。
linux qt 打包程序报错,/home/lxy/anaconda3/lib/libQt5Widgets.so:对‘std::pmr::monotonic_buffer_resource::~monotonic_buffer_resource()@GLIBCXX_3.4.28’未定义的引用 /home/lxy/anaconda3/lib/libQt5Core.so:对‘std::__exception_ptr::exception_ptr::_M_release()@CXXABI_1.3.13’未定义的引用 /home/lxy/anaconda3/lib/libQt5Widgets.so:对‘vtable for std::pmr::monotonic_buffer_resource@GLIBCXX_3.4.28’未定义的引用 /home/lxy/anaconda3/lib/libQt5Widgets.so:对‘std::pmr::get_default_resource()@GLIBCXX_3.4.26’未定义的引用 /home/lxy/anaconda3/lib/libQt5Widgets.so:对‘std::__throw_bad_array_new_length()@GLIBCXX_3.4.29’未定义的引用 /home/lxy/anaconda3/lib/libQt5Core.so:对‘std::__exception_ptr::exception_ptr::_M_addref()@CXXABI_1.3.13’未定义的引用 collect2: error: ld returned 1 exit status
这个错误通常是由于链接器无法找到所需的库文件或库文件版本不兼容导致的。根据错误信息,似乎是缺少对 libstdc++.so.6 库的链接。你可以尝试添加以下参数来链接该库:
```
-L/usr/lib64 -lstdc++
```
如果你的系统是 32 位的,则将 -L/usr/lib64 替换为 -L/usr/lib。如果还是无法解决问题,你可以尝试更新 libstdc++ 库,或者重新编译 Qt 库。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)