Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Rapper Functions for Memory Allocation and Freeing in C, Exercises of Computer Fundamentals

This lab practice for computer fundamentals course focuses on writing two rapper functions in c for malloc and free to check successful memory allocation and returning memory, and explaining the functionality of given code examples.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

karthik
karthik 🇮🇳

4.6

(16)

95 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Fundamental BEE2-AB
Lab practice 11th January 2011
Q. Write program
During the program execution when we ask memory from the system, it is
good practices to check whether memory is successfully allocated or not. It is
also good practice, in case you return memory back to the system, to check
that you are returning proper memory and not NULL. To make your program
more efficient write two rapper functions for already available malloc and free
as describe below. (rapper function uses original function but add more
feature to it).
a Write a function to gets memory from the system and has int as parameter,
which tells how many bytes to get from the system and return a void pointer to
newly, allocated memory. The most important feature of this function is in
case memory is not available or system fails to allocate memory it should
display an appropriate error message i.e. “Memory not available” and should
exit the program.
b Write a function to free memory allocated by the system and has void
pointer as parameter, which tells location of the memory to be freed and
return an int with value 0 in case of memory is returned, 1 in case it is not
possible. The most important feature of this function is to identify if user of this
function has mistakenly send in the pointer that is pointing to NULL.
Q2. Explain what does the following code do.
1 // Ex. 8.21: ex08_21.cpp
2 // What does this program do?
3 #include <stdio.h>
4 #include <stdlib.h>
5
6
7
8 void mystery1( char *, const char * ); // prototype
9
10 int main()
11 {
12 char *string1;
13 char *string2;
14
15 printf("Enter two strings: ");
16 scanf("%d,%d",&string1,&string2);
17 mystery1 (string1, string2);
18 printf("%s",string1);
19 return 0; // indicates successful termination
20 } // end main
21
docsity.com
pf3

Partial preview of the text

Download Rapper Functions for Memory Allocation and Freeing in C and more Exercises Computer Fundamentals in PDF only on Docsity!

Lab practice 11th^ January 2011

Q. Write program

During the program execution when we ask memory from the system, it is

good practices to check whether memory is successfully allocated or not. It is

also good practice, in case you return memory back to the system, to check

that you are returning proper memory and not NULL. To make your program

more efficient write two rapper functions for already available malloc and free

as describe below. (rapper function uses original function but add more

feature to it).

a Write a function to gets memory from the system and has int as parameter,

which tells how many bytes to get from the system and return a void pointer to

newly, allocated memory. The most important feature of this function is in

case memory is not available or system fails to allocate memory it should

display an appropriate error message i.e. “Memory not available” and should

exit the program.

b Write a function to free memory allocated by the system and has void

pointer as parameter, which tells location of the memory to be freed and

return an int with value 0 in case of memory is returned, 1 in case it is not

possible. The most important feature of this function is to identify if user of this

function has mistakenly send in the pointer that is pointing to NULL.

Q2. Explain what does the following code do.

1 // Ex. 8.21: ex08_21.cpp 2 // What does this program do? 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 7 8 void mystery1( char *, const char * ); // prototype 9 10 int main() 11 { 12 char *string1; 13 char *string2; 14 15 printf("Enter two strings: "); 16 scanf("%d,%d",&string1,&string2); 17 mystery1 (string1, string2); 18 printf("%s",string1); 19 return 0; // indicates successful termination 20 } // end main 21

Lab practice 11th^ January 2011

22 // What does this function do? 23 void mystery1( char *s1, const char *s2 ) 24 { 25 while ( *s1 != '\0' ) 26 ++s1; 27 28 for ( ; *s1 = *s2; s1++, s2++ ); // empty statement 30 } // end function mystery 1 // Ex. 8.21: ex08_29.cpp 2 // What does this program do? 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 7 8 int mystery3( const char *, const char * ); // prototype 9 10 int main() 11 { 12 char *string1, *string2; 13 14 printf("Enter two strings: "); 15 scanf("%d,%d",&string1,&string2); 16 printf( "The result is %d\n",mystery3( string1, string2 ); 17 return 0; // indicates successful termination 18 } // end main 19 20 // What does this function do? 21 int mystery3( const char *s1, const char *s2 ) 22 { 23 for ( ; *s1 != '\0' && *s2 != '\0'; s1++, s2++ ) 24 25 if ( *s1 != *s2 ) 26 return 0; 27 28 return 1; 29 } // end function mystery