Hinweis: Videofortschritt und Quizantworten werden nur nach Login gespeichert.

Effizientes Debugging

Assertions

assert Man-Page: assert(3)

assert.c

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

int main(int argc, char** argv) {
    if (argc != 2) {
        printf("Usage: %s size\n", argv[0]);
        return 1;
    }

    char* endptr = argv[1];
    long size = strtol(argv[1], &endptr, 10);
    if ((endptr == argv[1]) || (*endptr != '\0')) {
        printf("Argument \"size\" is not a valid integer. Got: %s\n", argv[1]);
        return 1;
    }


    int* array = malloc(size * sizeof(*array));

    printf("printf: array = %p\n", array);
    // NEVER do this!
    assert(array != NULL);


    for (int i = 0; i < size; i++) {
        array[i] = i * i;
    }

    free(array);

    return 0;
}

Material

Creative Commons License Video und Material von Benjamin Rickels et al. lizenziert unter CC BY-NC-SA 4.0.