Exercises : Answer the following Question

Submitted by Anonymous (not verified) on Sun, 03/10/2013 - 20:41

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises."
Quiz

    Provided that the char data type is 1 byte, the int data type is 2 bytes, and the float data type is 4 bytes, how many bytes of memory do the following functions try to allocate?
        malloc(100 * sizeof(int))
        calloc(200, sizeof(char))
        realloc(NULL, 50 * sizeof(float))
        realloc(ptr, 0)
    Given an int pointer, ptr, that is pointing to a block of memory that can hold 100 integers, if you want to reallocate the memory block to hold up to 150 integers, which of the two following statements do you use?
        ptr = realloc(ptr, 50 * sizeof(int));
        ptr = realloc(ptr, 150 * sizeof(int));
    After the following statements are executed successfully, what is the final size of the allocated memory block pointed to by the ptr pointer?

    . . .
    ptr = malloc(300 * sizeof(int));
    . . .
    ptr = realloc(ptr, 500 * sizeof(int));
    . . .
    ptr = realloc(ptr, 60 * sizeof(int));

    What is the final size of the allocated memory block pointed to by the ptr pointer, if the following statements are executed successfully?

    . . .
    ptr = calloc(100 * sizeof(char));
    . . .
    free(ptr);
    ptr = realloc(NULL, 200 * sizeof(char));
    . . .
    ptr = realloc(ptr, 0);

Exercises

    Write a program to ask the user to enter the total number of bytes he or she wants to allocate. Then, initialize the allocated memory with consecutive integers, starting from 1. Add all the integers contained by the memory block and print out the final result on the screen.
    Write a program that allocates a block of memory space to hold 100 items of the float data type by calling the calloc() function. Then, reallocate the block of memory in order to hold 50 more items of the float data type.
    Write a program to ask the user to enter the total number of float data. Then use the calloc() and malloc() functions to allocate two memory blocks with the same size specified by the number, and print out the initial values of the two memory blocks.
    Rewrite the program in Listing 17.4. This time, use the two special cases of the realloc() function to replace the malloc() and free() functions.

 

Related Items

More Examples of Disk File I/O

More Examples of Disk File I/O

The following sections show several more examples of disk file I/O, such as reading and writing binary data and redirecting the standard streams. Three more I/O functions, fscanf(), fprintf(), and freopen(), are introduced, too.

Random Access to Disk Files

Random Access to Disk Files

Exercises : Answer the following Question

To help solidify your understanding of this lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q What are the differences between a text stream and a binary stream?

Reading and Writing Disk Files

Reading and Writing Disk Files