07-26-2023, 07:10 AM
Using pointers without initializing them or dereferencing NULL pointers can lead to crashes.
// Incorrect
int *ptr; // Uninitialized pointer
*ptr = 10; // Dereferencing NULL pointer
// Correct
int *ptr = NULL;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);
// Incorrect
int *ptr; // Uninitialized pointer
*ptr = 10; // Dereferencing NULL pointer
// Correct
int *ptr = NULL;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);