Malloc and calloc function, Operating System

Assignment Help:

Note that the parameter for scanf doesn't need the address operators & because name is an address. However the variable name has no defined space. This can cause problems within C and we really should define the space .Within C we can allocate space by means of character array or system functions namely malloc, free and calloc.If we declare a character array, it also reserves the space required for that string (After all a string is a collection i.e. array of characters). Therefore the program becomes

    #include stdio.h
    void main()
    {
    char prompt;
    /*Author : Mr James Mc Carren  
    Company: Staffordshire University  
     Date: 26th August 2012  
    Version 1.0  
    Function : String storing using static storage       
     Modifications:   none*/
    char name[20];
    scanf("%s",name);
    printf("your name is %s \n\r",name);  
    printf("Press and key to exit \n\r");
    scanf("\n%c",&prompt);
    }
This allocates 20 spaces to the string name, however if we wish to input a 50 character  string , we would need to re-compile the program , an alternative is to use Malloc/Free .Malloc checks during run time whether there is enough  space left and if there is it allocated it while Free  releases the space allocated to a variable .The syntax for malloc/Free is  
 
     variable = ( char *)malloc(char size);
     free(variable);
 
Therefore the program becomes
 
    #include stdio.h
    #include stdlib.h
    void main()
    {
    char prompt;
   
     Date: 26th August 2012 
    Version 1.0 
    Function : String storing using dynamic allocation       
     Modifications:   none*/
      char *name;
      name = (char *)malloc(20);
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
      scanf("%s",name);
      printf("your name is %s \n\r",name); 

      free(name);  
    printf("Press and key to exit \n\r");
    scanf("\n%c",&prompt);
    }

  Because Malloc allocates memory dynamically, we must place a trap routine to detect when the microprocessor cannot allocate memory (i.e. memory full), in this case malloc returns a NULL character back as the address of the variable i.e. This routine would detect the NULL character and exit the program .Note that malloced space is similar to a variable in that its life span is automatically terminated when a free command is used or the function where it was allocated ends no matter what you make read - this is not the case in C++  
 
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
 
Therefore we could allocate a scratch pad of memory using malloc and we can poke around in that
area e.g.
 
  #include
    #include
    void main()
    { 
    char prompt;  
  
     Date: 26th August 2012
    Version 1.0 
Function : Dynamic allocation  of windows pointr area i.e segmented memory     
     Modifications:   none*/
 
      char *name;
      unsigned long int address;
      unsigned char *add;
      unsigned char data;
      name = (char *)malloc(0x4000);
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
printf("Your segment area is %08lx to %08lx\n\r",name,name+(unsigned long int)0x4000);
printf("Please enter in the address  you want to read in the segment area\n\r");
scanf("%lx",&address);
add = (unsigned char *)address;
data = *add;
printf("The data at the address %08lx is %02x\n\r",address,data); 
printf("Press and key to exit \n\r");
scanf("\n%c",&prompt);
}


Related Discussions:- Malloc and calloc function

Explain lru approximation page replacement, LRU approximation page replacem...

LRU approximation page replacement In this we are able to use the reference bit associated with the page entry to choose a page to be removed. The various algorithms used for

Long term scheduler, Long term scheduler calculates which processes are a...

Long term scheduler calculates which processes are admitted to the machine for processing. It accepts the degree of multiprogramming. Once accepted, a job converts a process.

Clocks in distributed systems, Clocks in distributed systems : a. Why ar...

Clocks in distributed systems : a. Why are clocks difficult to synchronize in distributed systems? b. What features in a system assume that the clocks are (reasonably) synchr

Palm os provides no signifies of concurrent processing, Q. Palm OS provides...

Q. Palm OS provides no signifies of concurrent processing. Discuss three major complications that concurrent processing adds to an operating system. Answer: a. A meth

What is the purpose of the command interpreter, What is the purpose of the ...

What is the purpose of the command interpreter? Why is it usually separate from the kernel? It reads commands from the user or from a file of commands and executes them, usuall

Perform alpha-beta pruning on the following tree, Perform alpha-beta prunin...

Perform alpha-beta pruning on the following tree. Put an "X" over each node that is pruned. Put the final value next to all other nodes. Indicate which action MAX should take: to B

Role of discovery and prototype, Discuss the role of discovery and developm...

Discuss the role of discovery and developmental prototypes in defining requirements for this project.Are throwaway prototypes appropriate, or should the project team quickly gather

Explain next fit algorithm, NEXT FIT ALGORITHM Here scanning starts fro...

NEXT FIT ALGORITHM Here scanning starts from the first fit position and then it finds the next position which is large sufficient to hold the process. Thus the name next fit.

Define deadlock, Define deadlock. A process requests resources; if the ...

Define deadlock. A process requests resources; if the resources are not available at that time, the process enters a wait state. Waiting processes might be never again change s

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd