Programming in C, autumn 2010


Exercise 4

Sep. 30 2010

  1. A program can get its memory from the run-time stack and the heap. Explain using examples when a program gets it memory from run-time stack and when from the heap.

  2. Write a program to read a file containing data in the following format:
    integer followed by doubles
    The program reads first integer value from the file, say n, and then allocates memory to store n double values, read these values from the file. Next, it sorts all values, and writes them back to the file. Include full error checking.

    Exercises 3-6. Write a menu-driven program, which supports the following commands:

     
    	  i n       where n is an integer value
    	  r fname    where fname is a filename 
    	  w
    	  s r       where r is a double
    	  d 
    	  h 
    	  q  
    The above commands have the following meaning:
        i    allocates a bolck of memory to store n double values; if a block of memory has previously 
              been allocated, that block is deallocated
        r    reads double values from the file whose name is fname; if fails if memory has not been 
              allocated. It stops reading if either the number of values read is equal to the current 
              size of the memory block, or no more double values can be read from the file (because 
              end-of-file has been encounted or a reading error has been occured)
        w	 shows all values that are currently stored in the memory block
        s    searches the block of memory looking for a given value and informs the user whether or 
              not this value is in the block; it fails if the block has not been allocated.
        d    is only available when the program is compiled using debugging mode. It turns additional
              debugging message on/off; i.e. when it is executed for the first time, it turns debugging 
              on, and then shows additional information about commands being executed. When the d command 
              is executed for a second time, these messages are not shown anymore. Subsequent commands 
              using the d option toggle debugging on and off
        h    shows alls available commands
        q    quits the program (but first, it closes any files that have been opened, and deallocates 
              any memory that have been allocated)
    
  3. Write a function, which implements command i.

  4. Write a function, which implements command r. Write a function, which implements command w.

  5. Write a main program, which reads and checks input and performs required command. Commands can be given using both lowercase and uppercase letters. In main program is the code required by commands h and q. If the commands d is given the main program can print that the command d has not been implemented. If the input is not valid, the main program will write help about commands.

  6. Correct the documentation of the functions so that it conforms to the standard introduced in Müldners book (chapter 7).

.

Things you should study or start:
  1. Buffer overflow - avoid this!
  2. Bit operations (Example. 13.1)
  3. Start projekt work