类初始化问题
问一下,这个类的构造函数可以这样写吗,用new来为指针a分配空间时,还没有执行capacity=50这个语句,为什么程序还能正常运行 提问题要贴代码,这个问题碰巧我也不知道,就看着图片抄了一遍,还有就是代码量比较小$ cat main.cpp
#include <iostream>
template <class T>
class PFArray {
public:
PFArray() {
a = new T;
capacity = 50;
used = 0;
}
private:
T *a;
int capacity;
int used;
};
int main() {
PFArray<int> pfa;
return 0;
}
$ g++-debug -o main main.cpp
main.cpp: In constructor ‘PFArray<T>::PFArray() ’:
main.cpp:7:19: warning: ‘*<unknown>.PFArray<int>::capacity’ is used uninitialized in this function [-Wuninitialized]
7 | a = new T;
| ^~~~~~~~
$ ./main
=================================================================
==23309==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 262140 byte(s) in 1 object(s) allocated from:
#0 0x7f99a3e6f0c1 in operator new[](unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:102
#1 0x56180b0ee481 in PFArray<int>::PFArray() /home/suhuajun/code/C++/tmp/main.cpp:7
#2 0x56180b0ee2d5 in main /home/suhuajun/code/C++/tmp/main.cpp:18
#3 0x7f99a2f6bb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
SUMMARY: AddressSanitizer: 262140 byte(s) leaked in 1 allocation(s).
$
$ cat main.cpp
#include <iostream>
template <class T>
class PFArray {
public:
PFArray() {
std::cout << capacity << std::endl;
a = new T;
capacity = 50;
used = 0;
}
~PFArray() {
delete[] a;
}
private:
T *a;
int capacity;
int used;
};
int main() {
PFArray<int> pfa;
return 0;
}
$ g++-debug -o main main.cpp
main.cpp: In constructor ‘PFArray<T>::PFArray() ’:
main.cpp:7:19: warning: ‘*<unknown>.PFArray<int>::capacity’ is used uninitialized in this function [-Wuninitialized]
7 | std::cout << capacity << std::endl;
| ~~~~~~~~~~^~~~~~~~~~~
$ ./main
1349088241
=================================================================
==23419==ERROR: AddressSanitizer: allocator is out of memory trying to allocate 0x141a5cfc4 bytes
#0 0x7f06f3ffc0c1 in operator new[](unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:102
#1 0x55735069772c in PFArray<int>::PFArray() /home/suhuajun/code/C++/tmp/main.cpp:8
#2 0x55735069732c in main /home/suhuajun/code/C++/tmp/main.cpp:22
#3 0x7f06f30f8b24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
==23419==HINT: if you don't care about these errors you may set allocator_may_return_null=1
SUMMARY: AddressSanitizer: out-of-memory /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:102 in operator new[](unsigned long)
==23419==ABORTING
$
人造人 发表于 2021-5-18 22:16
提问题要贴代码,这个问题碰巧我也不知道,就看着图片抄了一遍,还有就是代码量比较小
(lll¬ω¬),有点看不懂,大佬能解释一下吗 一世轻尘 发表于 2021-5-19 16:38
(lll¬ω¬),有点看不懂,大佬能解释一下吗
warning: ‘*<unknown>.PFArray<int>::capacity’ is used uninitialized in this function 一世轻尘 发表于 2021-5-19 16:38
(lll¬ω¬),有点看不懂,大佬能解释一下吗
$ ./main
1349088241
也就是 a = new T;
AddressSanitizer: allocator is out of memory trying to allocate 0x141a5cfc4 bytes 人造人 发表于 2021-5-19 16:50
也就是 a = new T;
AddressSanitizer: allocator is out of memory trying to allocate ...
就是说,给出了一个未初始化的警告,然后会随机分配了一块内存吗 一世轻尘 发表于 2021-5-19 17:06
就是说,给出了一个未初始化的警告,然后会随机分配了一块内存吗
不是随机分配内存,是这个变量没有初始化,这个变量里面存储的内容是随机的
人造人 发表于 2021-5-19 17:10
不是随机分配内存,是这个变量没有初始化,这个变量里面存储的内容是随机的
哦,明白了{:10_275:}
页:
[1]