COMP1511 18s1 (webcms)
Code Examples from Lectures on malloc
COMP1511 18s1 (flask)
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(int argc, char *argv[]) {

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

    int *numbers = malloc(n * sizeof (int));
    if (numbers == NULL) {
        fprintf(stderr, "%s: malloc failed\n", argv[0]);
        return 1;
    }

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

    printf("Numbers reversed are:\n");
    for (int 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 needed
Read n numbers and print them in reverse order
    #include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int n = 0;
    int arraySize = 4;
    int *array = malloc(arraySize * sizeof (int));
    if (array == NULL) {
        fprintf(stderr, "%s: malloc failed\n", argv[0]);
        return 1;
    }

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

            // allocate larger array
            int newArraySize = 2 * arraySize;
            int *newArray = malloc(newArraySize * sizeof (int));
            if (newArray == NULL) {
                fprintf(stderr, "%s: malloc failed\n", argv[0]);
                return 1;
            }

            // copy contents of old array to new array
            for (int 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 (int 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(int argc, char *argv[]) {
    int n = 0;
    int arraySize = 4;
    int *array = malloc(arraySize * sizeof (int));
    if (array == NULL) {
        fprintf(stderr, "%s: malloc failed\n", argv[0]);
        return 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) {
                fprintf(stderr, "%s: realloc failed\n", argv[0]);
                return 1;
            }
            printf("Array size increased to %d\n", arraySize);
        }
    }


    printf("Numbers reversed are:\n");
    for (int 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;
}