题目
题目

CIS-278-HYA-CRN40355 Test 2 ( In-class) Friday April 19 : 3:15-5:15 pm

单项选择题

If the heap memory location starts at 9403, what is the value of the integer variable myAge at position 1? #include <iostream> using namespace std; int a = 10; int main() { int b; int* myAge = nullptr; b = 20; myAge = new int; // Position 1 *myAge = 30; delete myAge; return 0; }

查看解析

查看解析

标准答案
Please login to view
思路分析
We start by restating the situation given in the question. The code allocates a single int on the heap with myAge = new int;, and this allocation is described as Position 1. The question then asks for the value of the integer at that position, given that the heap memory location starts at 9403. Step 1: Understand what Position 1 refers to. The comment // Position 1 next to the allocation suggests that Position 1 is the first (and only) dynamic int allocated on the heap. The memory address of that first heap cell, if the heap starts at 9403, would be 9403. Step 2: Distinguish between address and value. The phrase "value of the integer variable myAge at position 1" can b......Login to view full explanation

登录即可查看完整答案

我们收录了全球超50000道考试原题与详细解析,现在登录,立即获得答案。

类似问题

Consider the function call: p = (data_t *)malloc((num - 1) * sizeof(data_t)) The intention of this function call is best described as:

Inside the main() function of "particletest.c", we have now allocated an array of 1024 particles of type Particle. As we would like this number to be flexible in the future, the array is allocated dynamically using malloc(), which is part of the C Standard General Utilities Library. 在"particletest.c"的 main()函数中,我们动态分配了一个包含 1024 个 Particle 类型粒子的数组。为了使这个数字在未来可以灵活调整,我们使用 C 标准通用工具库中的 malloc()函数来动态分配数组。 Our implementation uses the following approach: 我们的实现采用了以下方法: #include <stdio.h> #include <stdlib.h> #define NUM_PARTICLES 1024 ··· int main() { Particle *particles = (Particle *)malloc(NUM_PARTICLES); ··· } Despite the fact that the declaration seems correct, the code produces a segmentation fault exception in some experiments. However, in some other situations, no apparent errors are produced. Do you consider the declaration and allocation to be correct (and, thus, the error located somewhere else in the source code)?

Inside the main() function of "particletest.c", we have now allocated an array of 1024 particles of type Particle. As we would like this number to be flexible in the future, the array is allocated dynamically using malloc(), which is part of the C Standard General Utilities Library. Our implementation uses the following approach: #include <stdio.h> #include <stdlib.h> #define NUM_PARTICLES 1024 ··· int main() { Particle *particles = (Particle *)malloc(NUM_PARTICLES); ··· } Despite the fact that the declaration seems correct, the code produces a segmentation fault exception in some experiments. However, in some other situations, no apparent errors are produced. Do you consider the declaration and allocation to be correct (and, thus, the error located somewhere else in the source code)?

Which header file must be included to use dynamic memory allocation functions like malloc and free?

更多留学生实用工具

加入我们,立即解锁 海量真题独家解析,让复习快人一步!