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 #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

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 Why is random access to a disk file necessary?