3i Infotech Papers: Sample Questions 1212 - 1213 of 1245

Examrace Placement Series prepares you for the toughest placement exams to top companies.

Question number: 1212

» Basic CS » Operating System

Short Answer Question▾

Write in Short

Which function should be used to free the memory allocated by calloc ()?


  • free () is a function used to free the memory allocated dynamically, by both malloc and calloc functions.

  • free (ptr);

  • ptr is a pointer to a memory block which has already been created by malloc or calloc.

  • The memory we give back by calling free () is immediately available to other parts of program.

  • When we program exits, any allocated memory not freed is automatically released by the operating system.

  • Used data is prepended to the allocated block to manage the heap. If memory pointed to was not allocated by a heap allocation function as malloc () or calloc (), then the data preceding the block will be management data.

Question number: 1213

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

How would you use qsort () function to sort an array of structures?


  • qsort () is an standard C library function, for sorting an array.

  • It uses two argument one of which contains logic to decide order in sorted output.

  • This provides flexibility so qsort () to use many types and can be used to obtain any desired order.

  • The compare function takes two pointers as arguments and defines the order.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct emp_list
  4. {
  5.     int id;
  6.     char name[10];
  7. }
  8. EMP;
  9. int sorting_mechconst void *ele1 , const void *ele2)
  10. {
  11.     int a,b;
  12.     a = *((int *)ele1);
  13.     b = *((int *)ele2);
  14.     ifa > b)
  15.     {
  16.         return 1;
  17.     }
  18.     if(a < b )
  19.     {
  20.         return -1;
  21.     }
  22.     return 0;
  23. }
  24. qsort(&emp_list_array, &sorting_mech);

The function sorting_mech will be called by the qsort to sort order employees whenever there is need.