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 )); }
Correct Answer
for ( i=0; i < MAXROW ; i++) free (p[i]); free (p);
Programming problems
Search Results
1. How would you dynamically allocate a 2-D array of integers?
3. 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; };
4. Answer the following Program #define CHARSIZE 8 #define MASK(y) (1 << y % CHARSIZE) #define BITSLOT (y) (y / CHARSIZE) #define SET(x,y) ( x[BITSLOT(y)] = MASK(y) ) #define TEST(x,y) ( x[BITSLOT(y)] & MASK(y) ) #define NUMSLOTS(n) ((n + CHARSIZE - 1) / CHARSIZE) Give the above macros how would you 1. declare an array arr of 50 bits 2. put the 20th bit on 3. test whether the 40th bit is on or off
Correct Answer: Yes, using the realloc() function as shown below: main() { int *p; p = ( int *) malloc (20) ; t = p; t = (int *) realloc ( p, 40); if ( t == NULL ) Printf (" Cannot reallocate, leaves previous allocated region unchanged "); else { if ( p ==t ) ; / * the array expanded at the same region */ else { free ( p ); / * deallocate the original array */ p = t; /* set p to newly allocated region */ } } }
7. 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 ));
8. If I use the following printf() to print a long int why I am not warned about the type mismatch? printf ("%d",num );
Correct Answer: When a function accepts a variable number of arguments , its prototype cannot provide any information about the number of arguments and type of those variable arguments Hence the compiler cannot warn about the mismatches The programmer must make sure that arguments match or must manually insert explicit typecast
9. How would you use qsort() function to sort an array of structures?
Correct Answer: #include "stringh" #include "stdlibh" struct stud { int rollno; int marks; char name[30]; }; int sort_m (struct stud *, struct stud *); int sort_name (struct stud *, struct stud *); int sort_marks (struct stud *, struct stud *); main() { static struct stud ss[] = { { 15, 96, "Akshay" }, { 2, 97, "Madhuri" }, { 8, 85, "Aishvarya" }, { 10, 80, "Sushmita" } }; int x,w; clrscr(); w = sizeof (struct stud); printf ('\nIn order of roll numbers:"); qsort (ss, 4, w, sort_rn); for(x=0; x<4;x++) printf ("\n%d%s%d", ss[x]rollno, ss[x]name,ss[x]marks); printf("\n\nIn order of names:"); qsort(ss, 4, sort_name); for (x=0; x<4;x++) printf("\n%d%s%d",ss[x]rollno, ss[x]name,ss[x]marks); printf("\n\nIn order of marks:"); qsort(ss,4,w,sort_marks); for (x=0;x<4;x++) printf ("\n%d%s%d",ss[x]rollno,ss[x]name,ss[x]marks); } int sort_rn (struct stud *t1, struct stud *t2) { return (t1->rollno-t2->rollno); } int sort_name (struct stud *t1, struct stud *t2) { return (strcmp(t1->name,t2->name)); } int sort_marks (struct stud *t1, struct stud *t2) { return (t2->marks-t1->marks); }
10. 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; }
Correct Answer: Output: 1 1 1 1 1 1 For loop will execute six times Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false