Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Operating System Expert

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);
}

Operating System, Computer Science

  • Category:- Operating System
  • Reference No.:- M9620385

Have any Question?


Related Questions in Operating System

Taskyour job in this assignment is to create two virtual

Task Your job in this assignment is to create two Virtual machines each running a different but the latest distribution of Linux e.g. Ubuntu Server and CentOS. Each of these VM's is to offer services to a user base. The ...

Question what do you see as the major differences between

Question : What do you see as the major differences between UNIX/Linux and other operating systems, such as Windows and Mac OS X? The response must be typed, single spaced, must be in times new roman font (size 12) and m ...

Research types of operating systems that are currently

Research types of operating systems that are currently available and provide a scenario in which the operating system you chose would be appropriate to be used in this situation. Explain why you think the choice you made ...

Foundation of information technologyresearch types of

Foundation of Information Technology Research types of operating systems that are currently available and provide a scenario in which the operating system you chose would be appropriate to be used in this situation. Expl ...

Discussion question this research assignment will give

Discussion Question : This research assignment will give further information on the nature and workings of multi-tasking and multi-processing operating systems. All information reported in this assignment is to be in the ...

Question note apa format 250 words and three reference

Question: Note: APA format 250 words and three reference without plagarism Computerized Operating Systems (OS) are almost everywhere. We encounter them when we use out laptop or desktop computer. We use them when we use ...

State the required answer precisely and then provide proper

State the required answer precisely and then provide proper explanation. It is not enough to provide one- word or one-line answers. Consider a computer embedded in the control of a manned spacecraft. Would it require an ...

Show all stepssuppose that the following processes arrive

Show all steps Suppose that the following processes arrive for execution at the times indicated. Each process will run the listed amount of time. in answering the questions, use non-preemptive scheduling and base all dec ...

Question topic computerized operating systems os are almost

Question: Topic: Computerized Operating Systems (OS) are almost everywhere. We encounter them when we use out laptop or desktop computer. We use them when we use our phone or tablet. Find articles that describes the diff ...

Question students working at individual pcs in a computer

Question : Students working at individual PC's in a computer laboratory send their files to be printed by a server that spools the files on its hard disk. Under what conditions may a deadlock occur if the disk space for ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As