鱼C论坛

 找回密码
 立即注册
查看: 2439|回复: 8

[已解决]类初始化问题

[复制链接]
发表于 2021-5-18 21:50:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问一下,这个类的构造函数可以这样写吗,用new来为指针a分配空间时,还没有执行capacity=50这个语句,为什么程序还能正常运行
最佳答案
2021-5-19 17:10:19
一世轻尘 发表于 2021-5-19 17:06
就是说,给出了一个未初始化的警告,然后会随机分配了一块内存吗

不是随机分配内存,是这个变量没有初始化,这个变量里面存储的内容是随机的
1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-18 22:16:49 | 显示全部楼层
提问题要贴代码,这个问题碰巧我也不知道,就看着图片抄了一遍,还有就是代码量比较小
  1. $ cat main.cpp
  2. #include <iostream>

  3. template <class T>
  4. class PFArray {
  5. public:
  6.     PFArray() {
  7.         a = new T[capacity];
  8.         capacity = 50;
  9.         used = 0;
  10.     }
  11. private:
  12.     T *a;
  13.     int capacity;
  14.     int used;
  15. };

  16. int main() {
  17.     PFArray<int> pfa;
  18.     return 0;
  19. }
  20. $ g++-debug -o main main.cpp
  21. main.cpp: In constructor ‘PFArray<T>::PFArray() [with T = int]’:
  22. main.cpp:7:19: warning: ‘*<unknown>.PFArray<int>::capacity’ is used uninitialized in this function [-Wuninitialized]
  23.     7 |         a = new T[capacity];
  24.       |                   ^~~~~~~~
  25. $ ./main

  26. =================================================================
  27. ==23309==ERROR: LeakSanitizer: detected memory leaks

  28. Direct leak of 262140 byte(s) in 1 object(s) allocated from:
  29.     #0 0x7f99a3e6f0c1 in operator new[](unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:102
  30.     #1 0x56180b0ee481 in PFArray<int>::PFArray() /home/suhuajun/code/C++/tmp/main.cpp:7
  31.     #2 0x56180b0ee2d5 in main /home/suhuajun/code/C++/tmp/main.cpp:18
  32.     #3 0x7f99a2f6bb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)

  33. SUMMARY: AddressSanitizer: 262140 byte(s) leaked in 1 allocation(s).
  34. $

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-18 22:19:54 | 显示全部楼层
  1. $ cat main.cpp
  2. #include <iostream>

  3. template <class T>
  4. class PFArray {
  5. public:
  6.     PFArray() {
  7.         std::cout << capacity << std::endl;
  8.         a = new T[capacity];
  9.         capacity = 50;
  10.         used = 0;
  11.     }
  12.     ~PFArray() {
  13.         delete[] a;
  14.     }
  15. private:
  16.     T *a;
  17.     int capacity;
  18.     int used;
  19. };

  20. int main() {
  21.     PFArray<int> pfa;
  22.     return 0;
  23. }
  24. $ g++-debug -o main main.cpp
  25. main.cpp: In constructor ‘PFArray<T>::PFArray() [with T = int]’:
  26. main.cpp:7:19: warning: ‘*<unknown>.PFArray<int>::capacity’ is used uninitialized in this function [-Wuninitialized]
  27.     7 |         std::cout << capacity << std::endl;
  28.       |         ~~~~~~~~~~^~~~~~~~~~~
  29. $ ./main
  30. 1349088241
  31. =================================================================
  32. ==23419==ERROR: AddressSanitizer: allocator is out of memory trying to allocate 0x141a5cfc4 bytes
  33.     #0 0x7f06f3ffc0c1 in operator new[](unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:102
  34.     #1 0x55735069772c in PFArray<int>::PFArray() /home/suhuajun/code/C++/tmp/main.cpp:8
  35.     #2 0x55735069732c in main /home/suhuajun/code/C++/tmp/main.cpp:22
  36.     #3 0x7f06f30f8b24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)

  37. ==23419==HINT: if you don't care about these errors you may set allocator_may_return_null=1
  38. SUMMARY: AddressSanitizer: out-of-memory /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cpp:102 in operator new[](unsigned long)
  39. ==23419==ABORTING
  40. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-19 16:38:23 | 显示全部楼层
人造人 发表于 2021-5-18 22:16
提问题要贴代码,这个问题碰巧我也不知道,就看着图片抄了一遍,还有就是代码量比较小

(lll¬ω¬),有点看不懂,大佬能解释一下吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-19 16:49:34 | 显示全部楼层
一世轻尘 发表于 2021-5-19 16:38
(lll¬ω¬),有点看不懂,大佬能解释一下吗

warning: ‘*<unknown>.PFArray<int>::capacity’ is used uninitialized in this function
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-19 16:50:45 | 显示全部楼层
一世轻尘 发表于 2021-5-19 16:38
(lll¬ω¬),有点看不懂,大佬能解释一下吗
  1. $ ./main
  2. 1349088241
复制代码


也就是 a = new T[1349088241];
AddressSanitizer: allocator is out of memory trying to allocate 0x141a5cfc4 bytes
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-19 17:06:39 | 显示全部楼层
人造人 发表于 2021-5-19 16:50
也就是 a = new T[1349088241];
AddressSanitizer: allocator is out of memory trying to allocate ...


就是说,给出了一个未初始化的警告,然后会随机分配了一块内存吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-19 17:10:19 | 显示全部楼层    本楼为最佳答案   
一世轻尘 发表于 2021-5-19 17:06
就是说,给出了一个未初始化的警告,然后会随机分配了一块内存吗

不是随机分配内存,是这个变量没有初始化,这个变量里面存储的内容是随机的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-19 17:12:30 | 显示全部楼层
人造人 发表于 2021-5-19 17:10
不是随机分配内存,是这个变量没有初始化,这个变量里面存储的内容是随机的

哦,明白了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 08:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表