Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Safe Handling of Pointers | Writing Safe and Predictable Code
C Defensive Programming and Error Handling

bookSafe Handling of Pointers

Pointers are variables that store the memory address of another variable. They are powerful in C because they allow you to directly access and manipulate memory. However, pointers are also risky: improper use can lead to bugs such as crashes, memory corruption, or unpredictable program behavior. One common source of errors is dereferencing a pointer that does not point to a valid memory location.

main.c

main.c

copy
12345678910
#include <stdio.h> int main() { int *unsafe_ptr = NULL; // This pointer is NULL // Unsafe: Dereferencing a NULL pointer (will likely crash) printf("%d\n", *unsafe_ptr); // Causes undefined behavior return 0; }

This first example shows what happens when you dereference a NULL pointer — it leads to undefined behavior and often crashes the program. Look at a safer version that includes proper checks before using pointers.

main.c

main.c

copy
123456789101112131415161718192021
#include <stdio.h> int main() { int value = 42; int *safe_ptr = &value; // Valid pointer int *unsafe_ptr = NULL; // NULL pointer // Safe: Check for NULL before dereferencing safe_ptr if (safe_ptr != NULL) printf("Safe pointer value: %d\n", *safe_ptr); else printf("safe_ptr is NULL\n"); // Safe: Check for NULL before dereferencing unsafe_ptr if (unsafe_ptr != NULL) printf("Unsafe pointer value: %d\n", *unsafe_ptr); else printf("unsafe_ptr is NULL\n"); return 0; }
Note
Note

Always check whether a pointer is NULL before dereferencing it. This simple defensive check prevents crashes and helps you identify logic errors early, keeping your programs more stable and predictable.

question mark

Why should you check if a pointer is NULL before dereferencing it?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 12.5

bookSafe Handling of Pointers

Swipe um das Menü anzuzeigen

Pointers are variables that store the memory address of another variable. They are powerful in C because they allow you to directly access and manipulate memory. However, pointers are also risky: improper use can lead to bugs such as crashes, memory corruption, or unpredictable program behavior. One common source of errors is dereferencing a pointer that does not point to a valid memory location.

main.c

main.c

copy
12345678910
#include <stdio.h> int main() { int *unsafe_ptr = NULL; // This pointer is NULL // Unsafe: Dereferencing a NULL pointer (will likely crash) printf("%d\n", *unsafe_ptr); // Causes undefined behavior return 0; }

This first example shows what happens when you dereference a NULL pointer — it leads to undefined behavior and often crashes the program. Look at a safer version that includes proper checks before using pointers.

main.c

main.c

copy
123456789101112131415161718192021
#include <stdio.h> int main() { int value = 42; int *safe_ptr = &value; // Valid pointer int *unsafe_ptr = NULL; // NULL pointer // Safe: Check for NULL before dereferencing safe_ptr if (safe_ptr != NULL) printf("Safe pointer value: %d\n", *safe_ptr); else printf("safe_ptr is NULL\n"); // Safe: Check for NULL before dereferencing unsafe_ptr if (unsafe_ptr != NULL) printf("Unsafe pointer value: %d\n", *unsafe_ptr); else printf("unsafe_ptr is NULL\n"); return 0; }
Note
Note

Always check whether a pointer is NULL before dereferencing it. This simple defensive check prevents crashes and helps you identify logic errors early, keeping your programs more stable and predictable.

question mark

Why should you check if a pointer is NULL before dereferencing it?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3
some-alt