COMP1511 17s1 Code Examples from Lectures on malloc Introduction to Programming
simple examples of using sizeof

#include <stdio.h>

int main(void) {

    printf("sizeof (char) = %d\n", (int)sizeof (char));
    printf("sizeof (int) = %d\n", (int)sizeof (int));
    printf("sizeof (double) = %d\n", (int)sizeof (double));
    printf("sizeof int[10] = %d\n", (int)sizeof (int[10]));
    printf("sizeof int * = %d\n", (int)sizeof (int *));
    printf("sizeof \"hello\" = %d\n", (int)sizeof "hello");

    return 0;
}

simple example of using malloc to create an array

Read n numbers and print them in reverse order

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *numbers;
    int i, n;

    printf("Read how many numbers? ");
    scanf("%d", &n);

    numbers = malloc(n * sizeof (int));
    if (numbers == NULL) {
        perror("");
        exit(1);
    }

    for (i = 0; i < n; i = i + 1) {
        scanf("%d", &numbers[i]);
    }

    printf("Numbers reversed are:\n");
    for (i = n - 1; i >= 0; i = i - 1) {
        printf("%d\n", numbers[i]);
    }

    // free the allocated storage
    // this would happen on program exit anyway
    free(numbers);
    return 0;
}

example of using malloc to progressively allocate larger array as needer

Read n numbers and print them in reverse order

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *array;
    int *newArray;
    int arraySize, newArraySize;
    int i, n;

    n = 0;
    arraySize = 4;
    array = malloc(arraySize * sizeof (int));
    if (array == NULL) {
        perror("");
        exit(1);
    }

    while (scanf("%d", &array[n]) == 1) {
        n = n + 1;
        if (n == arraySize) {

            // allocate larger array
            newArraySize = 2 * arraySize;
            newArray = malloc(newArraySize * sizeof (int));
            if (newArray == NULL) {
                perror("");
                exit(1);
            }

            // copy contents of old array to new array
            for (i = 0; i < arraySize; i = i + 1) {
                newArray[i] = array[i];
            }

            // deallocate old array
            free(array);

            // change pointer to new array
            array = newArray;
            arraySize = newArraySize;
            printf("Array size increased to %d\n", arraySize);
        }
    }


    printf("Numbers reversed are:\n");
    for (i = n - 1; i >= 0; i = i - 1) {
        printf("%d\n", array[i]);
    }

    // free the allocated storage
    // this would happen on program exit anyway
    free(array);
    return 0;
}

example of using realloc to progressively allocate larger array as needed

Read n numbers and print them in reverse order

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *array;
    int arraySize;
    int i, n;

    n = 0;
    arraySize = 4;
    array = malloc(arraySize * sizeof (int));
    if (array == NULL) {
        perror("");
        exit(1);
    }

    while (scanf("%d", &array[n]) == 1) {
        n = n + 1;
        if (n == arraySize) {
            // allocate larger array
            arraySize = 2 * arraySize;
            array = realloc(array, arraySize * sizeof (int));
            if (array == NULL) {
                perror("");
                exit(1);
            }
            printf("Array size increased to %d\n", arraySize);
        }
    }


    printf("Numbers reversed are:\n");
    for (i = n - 1; i >= 0; i = i - 1) {
        printf("%d\n", array[i]);
    }

    // free the allocated storage
    // this would happen on program exit anyway
    free(array);
    return 0;
}