logo

CuriousTab

CuriousTab

Discussion


Home Technical Questions Programming Comments

  • Question
  • What is the difference between malloc() and calloc() functions?


  • Correct Answer
  • As against malloc(), calloc() needs two arguments, the number of elements to be allocated and the size of each element For example, p = (int *) calloc (10, sizeof (int)); would allocate space for a 10- integer array Additionally, calloc() would also set each of this element with a value 0 Thus the above call to calloc() is equivalent to: p = (int *) malloc (10 * sizeof (int)); memset (p, 0, 10 * sizeof( int )); 


  • Programming problems


    Search Results


    • 1. Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?
    • Discuss
    • 2. How would you free the memory allocated by the following program? #include "alloc.h" #define MAXROW 3 #define MAXCOL 4 main() { int **p, i; p = (int **) malloc (MAXROW * sizeof (int *)); for ( i = 0; i < MAXROW ; i++) p[i] = (int *) malloc (MAXCOL * sizeof (int )); }
    • Discuss
    • 3. How would you dynamically allocate a 2-D array of integers?
    • Discuss
    • 4. Improve the following code using typedef. struct node { int data1; float data2; struct node *left; struct node *right; }; struct node *ptr; ptr = (struct node *) malloc (sizeof (struct node) );
    • Discuss
    • 5. In the following code can we declare a new typedef name emp even though struct employee has not been completely defined while using typedef? < Yes / No> typedef struct employee *ptr; struct employee { char name[20]; int age; ptr next; };
    • Discuss
    • 6. If I use the following printf() to print a long int why I am not warned about the type mismatch? printf ("%d",num );
    • Discuss
    • 7. How would you use qsort() function to sort an array of structures?
    • Discuss
    • 8. What will be output of following c code? #include int main() { int i; for(i=10;i<=15;i++){ while(i){ do{ printf("%d ",1); if(i>1) continue; }while(0); break; } } return 0; }
    • Discuss
    • 9. What will be output of following c code? void main() { struct bitfield { unsigned a:5; unsigned c:5; unsigned b:6; }bit; char *p; struct bitfield *ptr,bit1={1,3,3}; p=&bit1; p++; clrscr(); printf("%d",*p); getch(); }
    • Discuss
    • 10. What will be output when you will execute following c code? #include enum actor { SeanPenn=5, AlPacino=-2, GaryOldman, EdNorton }; void main() { enum actor a=0; switch(a) { case SeanPenn: printf("Kevin Spacey"); break; case AlPacino: printf("Paul Giamatti"); break; case GaryOldman:printf("Donald Shuterland"); break; case EdNorton: printf("Johnny Depp"); } }

    • Options
    • A. Kevin Spacey
    • B. Paul Giamatti
    • C. Donald Shuterland
    • D. Johnny Depp
    • Discuss


    Comments

    There are no comments.

Enter a new Comment