# *********************************************************************** ************* MEMORY ALLOCATION

******************

*********************************************************************** 1] What would be the output of the follwoing program? main() { char *s; char *fun(); s =s fun(); printf("%s",s); } char *fun() { char buffer[30]; strcpy(buffer,"RAM - Rarely Adequate Memory"); return(buffer); } 2] 3] 4] 5] What is the solution for the problem in program 17.1 above? Does there exist any other solution for the problem 17.1? How would you dynamically allocate a 1-D array of integers? How would you dynamically allocate a 2-D array of integers?

6] How would you dynamically allocate a 2-D array of integers such that we are able to access any element using 2 subscripts, as in arr[i][j]? 7] How would you dynamically allocate a 2-D array of integers such that we are able to access any element using 2 subscripts, as in arr[i][j]? Al so the rows of the array should be stored in adjacent memory locations. 8] How many bytes would be allocated by the following code? #include "alloc.h" #define MAXROW 3 #define MAXCOL 4 main() { int(*p)[MAXCOL]; p = (int(*)[MAXCOL]malloc(MAXROW * sizeof(*p)); }

} 10] In the following code p is a pointer to an array of MAXCOL elements .sizeof(*p)).sizeof(p).h" main() { int *p. Also.h" program? . } 14] How would you free the memory allocated by the follwoing #include"alloc. if(p == NULL) printf("Allocation failed"). Would these arra ys be stored in adjacent locations? How would you access the elements of these array using p? #define MAXROW 3 #define MAXCOL 4 main() { int i. } 12] 14] How would you dynamically allocate a 3-D array of integers? How many bytes of memory would the following code reserve? #include "alloc.h" #define MAXROW 3 #define MAXCOL 4 main() { int(*p)[MAXCOL][MAXROW].9] What would be the output of the following program? #include"alloc. int (*p)[MAXCOL]. p = (int(*)[MAXCOL])malloc(MAXROW * sizeof (*p)). } 11] How many bytes would be allocated by the following code? #include "alloc. [ = (int(*)[MAXCOL])malloc(sizeof(*p)). p = (int *)malloc(256 * 256). malloc() allocates memory for MAXROW such arrays.j. printf("%d %d".h" #define MAXROW 3 #define MAXCOL 4 main() { int(*p)[MAXCOL]. p = (int(*)[MAXCOL])malloc(MAXROW * sizeof(*p)).

h" main() { int *p. } 18] What ould be the output of the second printf() in the following program? #include "alloc.i. i < MAXROWl i++) p[i] = p[0] + MAXCOL.s1). printf("%s". for(i = 0.j.s2). for(i = 0. printf("%s". p = (int*)malloc(20). char *s2 = "Punk". i < MAXROW.ptr).i. } 16] Would the following code work at all times? main() { char *ptr. gets(ptr). strcat(s1. p = (int **)malloc(MAXROW * sizeof(int*)). } 15] How would you free the memory allocated by the following #include"alloc.h" #define MAXROW 3 #define MAXCOL 4 main() { int **p.#define MAXROW 3 #define MAXCOL 4 main() { int **p. d you improve it? main() { char *s1 = "Cyber". } 17] The following code is improper though it may work some times. p[0] = (int *)malloc(MAXROW * MAXCOL * sizeof(int)). p = (int **)malloc(MAXROW * sizeof(int *)). i++) p[i] = (int *)malloc(MAXCOL * sizeof(int)). How woul program? .

printf("\n %s". Can you point #include "alloc.printf("%u". } 20] To free() we only pass the pointer to the block of memory which we want to deallocate. free(ptr). free(p). p = (struct*) malloc(sizeof(struct ex)). printf("%u". *ptr = (char*) maolloc(30). float j.h" main() { struct ex { int i.p). char *s. p -> (char *) malloc(20)."RAM.p). } it out? *********************************************************************** ****************** ANSWERS *********************** *********************************************************************** . } struct ex *p. strcpy(ptr.ptr). /* Suoopse this prints 1314 */ free(p). } 19] Something is logically wrong with this program.Rarely Adequate Memory"). Then how does free() know how many bytes it shoul d deallocate? 21] What would be the output of the following program? main() { char *ptr.

} char *fun() { static char bufer[30].1] The output is unpredictable since buffer is an auto array and would di e when the control goes back to main(). char *fun(). } char *fun() { char *ptr.h" main() { char *s."RAM . return(ptr). 2] main() { char *s. } } #include "alloc. for(i = 0. static(buffer.s).Rarely Adequate Memory"). return(bufer). s = fun()."RAM .p[i]).i < MAX. char *fun(). free(s). Thus s would be pointing to an a rray which no longer exists. i++) { p[i] = i. } 3] #include "alloc.h" #define MAXROW 3 #define MAXCOL 4 5] .Rarely Adequate Memory"). s = fun(). } 4] #include "alloc. printf("%d".s). printf("%s".h" #define MAX 10 main() { int *p. strcpy(ptr. p = (int *)malloc(MAX * sizeof(int)). printf("%s".i. ptr = (char *)malloc(30).

i.h" #define MAXROW 3 #define MAXCOL 4 main() { int **p.main() { int *p. p[0] = (int *)malloc(MAXROW * MAXCOL * sizeof(int)). printf("%d". for(i = 0. printf("%d". } } 7] #include "alloc. for(i = 0. } printf("/n"). i < MAXROW. i < MAXROW. j < MAXCOL. i < MAXROW.p[i * MAXCOL + j]). for(i = 0. i++) { for(j = 0.j. } } . j++) { p[i * MAXCOL + j] = i. j < MAXCOL. } } 6] #include "alloc. i++) p[i] = p[0] + i * MAXCOL.p[i][j]).i.j. i++) { for(j = 0. p = (int **)malloc(MAXROW * sizeof(int)).j. i++) p[i] = (int *)malloc(MAXCOL * sizeof(int)). } printf("/n").i.h" #define MAXROW 3 #define MAXCOL 4 main() { int **p. i++) { for(j = 0. for(i = 0. for(i = 0.p[i][j]). j < MAXCOL. p = (int *)malloc(MAXROW * MAXCOL * sizeof(int)). i < MAXROW. i < MAXROW. j++) { p[i][j] = i. } printf("/n"). j++) { p[i][j] = i. plrintf("%d". p = (int **)malloc(MAXROW * sizeof(int)).

} printf("/n").k. i< MAXX. k++) { for(i = 0.8] 9] 14 bytes 2 8 this by 10] The arrays are stored in adjacent locations. for(i = 0. j < MAXY. } for(k = 0. 13] It would fail to allocate any memory because 256 * 256 id 65536 which when passed to malloc() would become a negative number since the formal argument of malloc().i.j. To access the array elements we can use the following set of for(i = 0. for(i = 0. i++) { for(j = 0. j++) { p[i][j][k] = i + j + k. an unsigned int. p = (int ***)malloc(MAXX * sizeof(int**)). j++) printf("%d". k < MAXZ. } } loops. } 11] 12] 14 bytes #include "alloc.p[i][j]). i < MAXROW. i++) lprintf("%d". i < MAXROW. i++) { p[i] = (int **)malloc(MAXY * sizeof(int *)). int i. lprintf("%d". } printf("/n"). You can confirm printing their addresses using the following loop. can accomodate numbers only upto 655 .p[i][j][k]). i < MAXX. for(j = 0.p[i]). i++) { for(j = 0.h" #define MAXY 3 #define MAXY 4 #define MAXZ 5 main() { int ***p. j < MAXCOL. j < MAXY. j++) p[i][j] = (int *)malloc(MAXZ * sizeof(int)).

Since ptr is an uninitialised pointer it must be pointing at some unknown location in memory. printf("%s". free(p[0]). 14] for(i = 0. i < MAXROW. free(p). strcat(s1.s2). } 18] 1314 19] The memory chunk at which 's' is pointing has not been freed. . Yes. p = (int *)malloc(20). Just fre eing the memory pointed to by the struct pointer p is not enough. free(p). free(p). Hence it is simple for free( ) to know how many bytes ato deallocate.s1).35. 21] 22] 23] 2 No. using the realloc() function as shown below: #include "alloc. char *s2 = "Punk".h" main() { int *p. In short when we allocate structures containing pointers to other dynamically allocated objects before freeing the structure we have to first free each pointer in the structure. Ideally we s hould have used: free(p->s). i++) free(p[i]). 17] main() { char s1[25] = "Cyber". 15] 16] No. 20] In most implimentations of malloc() the number o fbytes allocted is stored adjacent to the allocated block. The string that we type in would get stored at the location to which ptr is pointing thereby overwriting whatever is present at that location.

For example. If the first is successful it returns the same pointer that you passed to it oth erwise a different pointer for the newly allocated space. /* the array expanded at the same region */ else { free(p). 27] 28] 29] The same that we use with mallloc(). p = (int *)calloc(10. 64 KB. } else { if(p == t) . . If the first strategy fails then it adopts the second. i."). the number nts to be allocted and othe size of each element. 26] As against malloc(). would also set each of this element with a value 0. Using interrupt 0x67. /* deallocate the original array */ p = t. if(t == NULL) { printf("Cannot reallocate. printf("region unchanged. t = (int *)realloc(p.sizeof(int)). 32] While assigning the address returned by malloc() we should use ptr and not *ptr. the details of which are beyond the scope of this book.t = p. would allocate space for a 10 integer array.40). calloc() needs two arguments. nctions are specific to DOS. free(). 31] Yes. However.e. Additionally. leaves previous"). True. if it allocates a n ew region somewhere else the programmer has to readjust the other pointers. 25] If realloc() expands allocated memory at the same place then there is no need of readjustment of other pointers. /* ste p to newly allocated region */ } } } 24] Both. These fu calloc() of eleme 30] Use the standard library functions farmalloc() and farfree().