Geeksquiz | Dynamic Memory Allocation

來源:互聯網
上載者:User

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:
A X—1 Y—3 Z-2
B (X—2 Y—1 Z-3
C 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 :
[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; }
Which of the above three functions are likely to cause problems with pointers? (GATE 2001)
A Only P3
B 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
B 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
A 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().
C calloc() takes two arguments, but malloc takes only 1 argument.
D 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 *
B Pointer of allocated memory type

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.