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 goto Statement

The goto Statement

The continue Statement

The continue Statement

The break Statement

The break Statement

You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.

The switch Statement

The switch Statement

Nested if Statements

Nested if Statements

As you saw in the previous sections, one if statement enables a program to make one decision. In many cases, a program has to make a series of decisions. To enable it to do so, you can use nested if statements.