Back to SEGV on unknown address
Here is the code for this example (source):
// Created for COMP2521 sanitiser guide
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
int main(void) {
struct node *n = NULL;
n->value = 69;
n->next = NULL;
}
Here is the error message:
struct node
NULL pointer, and that we tried to write (store) an int
n->value = 69;
The pointer accessed on line 7 is n
, since we try to write to one of its fields using ->
. Since the issue is accessing a NULL pointer, this tells us that n
is NULL (which we knew already).
Avoid this error by adding an if statement checking whether n
is NULL before trying to access it, or try figure out why n
is NULL and fix that.