Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 218 - 219 of 354

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 218

C & C Plus Plus
Edit

Describe in Detail

Essay▾

When can you tell that a memory leak will occur?

Explanation

  • A memory leak occurs when a program cannot free a block of dynamically allocated memory.
  • When program creates memory using constructor but fails to call destructor for release or free- causing memory leakage
  • Programmers allocate memory in heap and forget to delete it. Memory on stack is automatically managed.
  • Memory leaks are serious for programs like deamons and servers which never terminate.
Given the Image is Function with Memory Leak

Question 219

C & C Plus Plus
Edit

Describe in Detail

Essay▾

What is the output of the following program?

  1. voidmain()
  2. {
  3.     void ⚹v;
  4.     intinteger=2;
  5.     int ⚹i=&integer;
  6.     v=i; printf("%d",(int⚹)⚹v);
  7. }

Explanation

  • Compiler Error: invalid use of void expression
  • In the program
Table Shows the Program
void ⚹ v;
  • Define a void pointer v.
int integer = 2;
  • Define the int variable integer = 2.
int ⚹ i = &integer;
  • Int pointer i stores the address of integer variable
v = i;
  • Stores the pointer ‘i’ value in void pointer v.
  • But, void pointer is a generic pointer type.
  • No pointer arithmetic can be done on it.
  • Void pointers are normally used for,

    1. Passing generic pointers to functions and returning such pointers.

    2. As a intermediate pointer type.

    3. Used when the exact pointer type will be known at a later point of time.

  • So the answer is compiler error: we cannot apply indirection on type void ⚹ .

Developed by: