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.
Diagnosing This Check
Examples
Non-initialized Pointer Passed to Function
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()
.
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); }
Non-initialized Pointer to Structure
#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.
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; } } }
Non-initialized char*
Pointer Used to Store String
#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.
char
array instead
of char*
pointerOne 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); }
Non-initialized Array of char*
Pointers Used to Store Variable-Size Strings
#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.
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 |
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)