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

The Basics of Disk File I/O

The Basics of Disk File I/O

Now let's focus on how to open and close a disk data file and how to interpret error messages returned by I/O functions.

Files Versus Streams

Files Versus Streams

Exercises : Answer the following Question

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.

Question and Answer

    Q What are the differences between a union and a structure?

Making Structures Flexible

Making Structures Flexible

The second application of unions is to nest a union inside a structure so that the structure can hold different types of values.