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

Permanently starves cpu-bound programs, Q. Presume that a scheduling algori...

Q. Presume that a scheduling algorithm at the level of short-term CPU scheduling favours those processes that have used the least processor time in the recent past. Why this algori

Explain fifo page replacement, Explain FIFO page replacement A FIFO rep...

Explain FIFO page replacement A FIFO replacement algorithm associates with every page the time when that page was brought into memory. When a page must be changed, the oldest p

Tree directory structure, please help us to write a code in c for tree dire...

please help us to write a code in c for tree directory structures.

Thread, Explain thread in details

Explain thread in details

What are the methods for handling deadlocks, What are the methods for handl...

What are the methods for handling deadlocks? The deadlock problem can be dealt with in one of the three ways: a. Use a protocol to prevent or avoid deadlocks, ensuring that

Explain the procedure for handling the page fault, The procedure for handli...

The procedure for handling the page fault is as follows 1. We check the internal table to verify whether the reference was valid or invalid. 2. If the reference was invalid,

Medium term scheduler, Medium term scheduling is form of the swapping ope...

Medium term scheduling is form of the swapping operation. This attaches to processes that are in a suspended or blocked state. They are swapped out of real-memory storage until t

Why interrupt can be used, Why interrupt can be used? The majority of t...

Why interrupt can be used? The majority of the different parts of the PC need to send information to and from the processor and they expect to be able to get the processor's at

Explain the concept of reentrancy, Concept of Reentrancy   It is a ...

Concept of Reentrancy   It is a useful, memory-saving method for multiprogrammed timesharing machines. A Reentrant method is one in which multiple clients can share a singl

CPU, TURNAROUND TIME

TURNAROUND TIME

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