Geeksquiz | Dynamic Memory Allocation
来源:互联网 发布:java写游戏 编辑:程序博客网 时间:2024/05/22 13:08
dynamic memory allocation
-----------------
Dynamic Memory Allocation
Question 1
CORRECT
The most appropriate matching for the following pairs (GATE CS 2000)
X: m=malloc(5); m= NULL; 1: using dangling pointersY: free(n); n->value=5; 2: using uninitialized pointersZ: char *p; *p = ’a’; 3. lost memory is:
X—1 Y—3 Z-2
(X—2 Y—1 Z-3
X—3 Y—2 Z-1
X—3 Y—1 Z-2
Discuss it
Question 1 Explanation:
X -> A pointer is assigned to NULL without freeing memory so a clear example of memory leak Y -> Trying to retrieve value after freeing it so dangling pointer. Z -> Using uninitialized pointers
Question 2
WRONG
Consider the following three C functions :Which of the above three functions are likely to cause problems with pointers? (GATE 2001)
[PI]
int
* g (
void
)
{
int
x= 10;
return
(&x);
}
[P2]
int
* g (
void
)
{
int
* px;
*px= 10;
return
px;
}
[P3]
int
*g (
void
)
{
int
*px;
px = (
int
*)
malloc
(
sizeof
(
int
));
*px= 10;
return
px;
}
Only P3
Only P1 and P3
Only P1 and P2
P1, P2 and P3
Discuss it
Question 2 Explanation:
In P1, pointer variable x is a local variable to g(), and g() returns pointer to this variable. x may vanish after g() has returned as x exists on stack. So, &x may become invalid. In P2, pointer variable px is being assigned a value without allocating memory to it. P3 works perfectly fine. Memory is allocated to pointer variable px using malloc(). So, px exists on heap, it’s existence will remain in memory even after return of g() as it is on heap.
Question 3
CORRECT
Output?
# include<stdio.h>
# include<stdlib.h>
void
fun(
int
*a)
{
a = (
int
*)
malloc
(
sizeof
(
int
));
}
int
main()
{
int
*p;
fun(p);
*p = 6;
printf
(
"%d\n"
,*p);
return
(0);
}
May not work
Works and prints 6
Discuss it
Question 3 Explanation:
The program is not valid. Try replacing “int *p;” with “int *p = NULL;” and it will try to dereference a null pointer. This is because fun() makes a copy of the pointer, so when malloc() is called, it is setting the copied pointer to the memory location, not p. p is pointing to random memory before and after the call to fun(), and when you dereference it, it will crash. If you want to add memory to a pointer from a function, you need to pass the address of the pointer (ie. double pointer).
Question 4
WRONG
Which of the following is/are true
calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data.
malloc() and memset() can be used to get the same effect as calloc().
calloc() takes two arguments, but malloc takes only 1 argument.
Both malloc() and calloc() return 'void *' pointer.
All of the above
Discuss it
Question 5
CORRECT
What is the return type of malloc() or calloc()
void *
Pointer of allocated memory type
void **
int *
Discuss it
Question 5 Explanation:
malloc() and calloc() return void *. We may get warning in C if we don't type cast the return type to appropriate pointer.
Question 6
WRONG
Which of the following is true?
"ptr = calloc(m, n)" is equivalent to following
ptr = malloc(m * n);
ptr = malloc(m * n);
"ptr = calloc(m, n)" is equivalent to following
ptr = malloc(m * n); memset(ptr, 0, m * n);
ptr = malloc(m * n); memset(ptr, 0, m * n);
"ptr = calloc(m, n)" is equivalent to following
ptr = malloc(m); memset(ptr, 0, m);
ptr = malloc(m); memset(ptr, 0, m);
"ptr = calloc(m, n)" is equivalent to following
ptr = malloc(n); memset(ptr, 0, n);
ptr = malloc(n); memset(ptr, 0, n);
Discuss it
Question 6 Explanation:
See calloc() versus malloc() for details.
Question 7
WRONG
What is the problem with following code?
#include<stdio.h>
int
main()
{
int
*p = (
int
*)
malloc
(
sizeof
(
int
));
p = NULL;
free
(p);
}
Compiler Error: free can't be applied on NULL pointer
Memory Leak
Dangling Pointer
The program may crash as free() is called for NULL pointer.
Discuss it
Question 7 Explanation:
free() can be called for NULL pointer, so no problem with free function call. The problem is memory leak, p is allocated some memory which is not freed, but the pointer is assigned as NULL. The correct sequence should be following:
free(p); p = NULL;
Question 8
CORRECT
Consider the following program, where are i, j and k are stored in memory?
int
i;
int
main()
{
int
j;
int
*k = (
int
*)
malloc
(
sizeof
(
int
));
}
i, j and k are stored in stack segment
i and j are stored in stack segment. k is stored on heap.
i is stored in BSS part of data segment, j is stored in stack segment. k is stored on heap.
j is stored in BSS part of data segment, i is stored in stack segment. k is stored on heap.
Discuss it
Question 8 Explanation:
i is global variable and it is uninitialized so it is stored on BSS part of Data Segment (http://en.wikipedia.org/wiki/.bss) j is local in main() so it is stored in stack frame (http://en.wikipedia.org/wiki/Call_stack) k is dynamically allocated so it is stored on Heap Segment. See following article for more details. Memory Layout of C Programs
You have completed 8/8 questions .
Your score is 50%.
Your score is 50%.
0 0
- Geeksquiz | Dynamic Memory Allocation
- Dynamic memory allocation example
- dynamic memory allocation
- dynamic memory allocation
- [Tutorial] DMA Dynamic Memory Allocation
- Understanding virtual memory and dynamic allocation
- Advanced Memory Management: Dynamic Allocation, Part 1
- UE4 Garbage Collection & Dynamic Memory Allocation
- [转载]Dynamic Memory Allocation and Fragmentation in C and C++
- Scalable lock-free dynamic memory allocation简要观感
- XZ_iOS之动态内存分配(Dynamic memory allocation)
- Pointers on C——11 Dynamic Memory Allocation.1
- Pointers on C——11 Dynamic Memory Allocation.2
- Pointers on C——11 Dynamic Memory Allocation.3
- Pointers on C——11 Dynamic Memory Allocation.4
- Pointers on C——11 Dynamic Memory Allocation.5
- Pointers on C——11 Dynamic Memory Allocation.6
- Memory Allocation
- 代理大全
- 算法导论第八章__实现计数排序
- 最能毁掉程序员健康的几件事
- 律师你好,公司搬离本市区,需要向职工支付
- linux redhat 6 安装中文支持
- Geeksquiz | Dynamic Memory Allocation
- [设计模式]-装饰器模式(Decorator)
- 让DuiLib支持Icon图片显示
- 判定时间是否有效的函数
- 【Leetcode长征系列】Unique Paths
- git学习-git status, git diff, git rm, git mv等文件操作命令解释。
- webcrt编译错误集锦
- windows2008 定时任务
- linux下设置开机自动挂载其他主机的samba共享目录