c++ memory test

Question #1 When will this line fail to compile: new myObj[100];
a) Never
b) When myObj is too large to fit into memory
c) When myObj has no default constructor

Question #2 Assuming that myObj is less than 1000 bytes, is there anything wrong with this code?
char x[1000];
 myObj *obj = reinterpret_cast(x);
new (obj) myObj;
 a) Nope, it works fine
b) Yes, there could be byte alignment issues
c) Yes, the syntax for calling new is incorrect

Question #3 What is the functional difference between
myObj *x = new myObj[100]; delete x; and myObj *x = new myObj[100]; delete [] x;
 a) There is none; they both work as expected
b) They both do nothing.
c) The first will not invoke all myObj destructors

Question #4 What is wrong with the following code? int * x = (int *) malloc(100 * sizeof(int)); x = realloc(x, sizeof(int) * 200);
a) If realloc fails, then the original memory is lost
b) Nothing, realloc is guaranteed to succeed (by returning the original pointer)
c) Nothing, realloc frees the original memory passed to it 

Question#5 What is one possible symptom of having called free on the same block of memory twice?
a) Nothing, calling free twice on one block of memory always works fine
b) Malloc might always return the same block of memory
c) You cannot free any memory in the future 

Question#6 What's wrong with this line of code? int *x = NULL; delete x;
a) It causes a segmentation fault when delete tries to access NULL
b) Nothing
c) It is undefined behavior

Question #7 Assuming this code were being compiled using a standards-conforming C++, what is wrong with it? int * x = malloc(100 * sizeof(int)); x = realloc(x, sizeof(int) * 200);
a) malloc is undefined in C++, you can only allocate memory using new
b) Invalid cast from a void* to an int*
c) Nothing is wrong with this code

Question #8 What is the default behavior of the normal new operator failing?
 a) This is left up to the implementation to decide
b) It returns NULL
c) It throws an exception

Question #9 What is a difficulty that arises with handling out of memory errors?
a) Many cleanup operations require extra memory
b) Running out of memory rarely happens in systems that interact with users
c) Once you've run out of memory, your program cannot continue

 Question #10 What is the result of this code? myObj *foo = operator new(sizeof(foo));
a) That's not legal syntax!
b) foo has memory allocated for it, but is not constructed
c) foo has memory allocated for it, and is fully constructed
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Valgrind 是一个用于检测内存泄漏、越界访问和其他内存错误的工具集。在 C++ 中,由于动态内存分配和释放是一项常见的操作,因此可能会出现许多内存错误。使用 Valgrind 可以帮助你找到这些错误并进行修复。 使用 Valgrind 检测内存错误通常需要进行以下步骤: 1. 编译程序时添加 -g 选项,以便能够进行调试。 2. 运行程序时添加 --tool=memcheck 选项,以使用 Memcheck 工具进行内存检测。 3. 分析输出结果,找出内存错误的位置和原因。 以下是一个简单的示例: 1. 编写一个简单的 C++ 程序,例如: ``` #include <iostream> using namespace std; int main() { int* ptr = new int; *ptr = 10; cout << *ptr << endl; return 0; } ``` 2. 编译程序时添加 -g 选项: ``` g++ -g -o test test.cpp ``` 3. 运行程序时添加 --tool=memcheck 选项: ``` valgrind --tool=memcheck ./test ``` 4. 分析输出结果,可以看到 Valgrind 检测到了一个内存泄漏: ``` ==12345== Memcheck, a memory error detector ==12345== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==12345== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==12345== Command: ./test ==12345== ==12345== ==12345== HEAP SUMMARY: ==12345== in use at exit: 4 bytes in 1 blocks ==12345== total heap usage: 1 allocs, 0 frees, 4 bytes allocated ==12345== ==12345== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==12345== at 0x4C2E3F8: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==12345== by 0x400634: main (test.cpp:5) ==12345== ==12345== LEAK SUMMARY: ==12345== definitely lost: 4 bytes in 1 blocks ==12345== indirectly lost: 0 bytes in 0 blocks ==12345== possibly lost: 0 bytes in 0 blocks ==12345== still reachable: 0 bytes in 0 blocks ==12345== suppressed: 0 bytes in 0 blocks ==12345== ==12345== For counts of detected and suppressed errors, rerun with: -v ==12345== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) ``` 可以看到,Valgrind 检测到了一个内存泄漏,其原因在于第 5 行的 new 操作没有被匹配的 delete 操作释放。通过这个信息,可以修改程序以避免内存泄漏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值