Main Content

Non-initialized pointer

Pointer is not initialized before being read

Description

This check occurs for every pointer read. It determines whether the pointer being read is initialized.

Examples

expand all

int assignValueToAddress(int *ptr) {
  *ptr = 0;
}

void main() {
 int* newPtr;
 assignValueToAddress(newPtr);
}

In this example, newPtr is not initialized before it is passed to assignValueToAddress().

Correction — Initialize pointer before passing to function

One possible correction is to assign newPtr an address before passing to assignValueToAddress().

int assignValueToAddress(int *ptr) {
  *ptr = 0;
}

void main() {
 int val;
 int* newPtr = &val;
 assignValueToAddress(newPtr);
}
#include <stdlib.h>
#define stackSize 25

typedef struct stackElement {
  int value;
  int *prev;
}stackElement;

int input();

void main() {
 stackElement *stackTop;
 
 for (int count = 0; count < stackSize; count++) {
    if(stackTop!=NULL) {
       stackTop -> value = input();
       stackTop -> prev = (int*)stackTop;
    }
    stackTop = (stackElement*)malloc(sizeof(stackElement));
 }
}

In this example, in the first run of the for loop, stackTop is not initialized and does not point to a valid address. Therefore, the Non-initialized pointer check on stackTop!=NULL returns a red error.

Correction — Initialize pointer before dereference

One possible correction is to initialize stackTop through malloc() before the check stackTop!=NULL.

#include <stdlib.h>
#define stackSize 25

typedef struct stackElement {
  int value;
  int *prev;
}stackElement;

int input();

void main() {
 stackElement *stackTop;
 
 for (int count = 0; count < stackSize; count++) {
    stackTop = (stackElement*)malloc(sizeof(stackElement));
    if(stackTop!=NULL) {
       stackTop->value = input();
       stackTop->prev = (int*)stackTop;
    }
 }
}
#include <stdio.h>

void main() {
 char *str;
 scanf("%s",str);
}

In this example, str does not point to a valid address. Therefore, when the scanf function reads a string from the standard input to str, the Non-initialized pointer check returns a red error.

Correction — Use char array instead of char* pointer

One possible correction is to declare str as a char array. This declaration assigns an address to the char* pointer associated with the array name str. You can then use the pointer as input to scanf.

#include <stdio.h>

void main() {
 char str[10];
 scanf("%s",str);
}
#include <stdio.h>

void assignDataBaseElement(char** str) {
 scanf("%s",*str);
}

void main() {
 char *dataBase[20];

 for(int count = 1; count < 20 ; count++) {
    assignDataBaseElement(&dataBase[count]); 
    printf("Database element %d : %s",count,dataBase[count]);
 }
}

In this example, dataBase is an array of char* pointers. In each run of the for loop, an element of dataBase is passed via pointers to the function assignDataBaseElement(). The element passed is not initialized and does not contain a valid address. Therefore, when the element is used to store a string from standard input, the Non-initialized pointer check returns a red error.

Correction — Initialize char* pointers through calloc

One possible correction is to initialize each element of dataBase through the calloc() function before passing it to assignDataBaseElement(). The initialization through calloc() allows the char pointers in dataBase to point to strings of varying size.

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

void assignDataBaseElement(char** str) {
 scanf("%s",*str);
}
int inputSize();

void main() {
 char *dataBase[20];

 for(int count = 1; count < 20 ; count++) {
    dataBase[count] = (char*)calloc(inputSize(),sizeof(char));
    assignDataBaseElement(&dataBase[count]); 
    printf("Database element %d : %s",count,dataBase[count]);
 }
}

Check Information

Group: Data flow
Language: C | C++
Acronym: NIP