I know that there are similar posts and that a pointer is initialized to NULL to avoid deferring a random address, but if a programmer declares a pointer is because he will use it later and assign it a valid address. So imagine this code:
int main() {
int *p;
p = (int *)malloc(sizeof(int));
}
So why should I initialize a pointer to NULL
if I am going to assign a valid address to the pointer later on?
CodePudding user response:
Depending on your compiler flags, you may get a warning whenever a variable is declared without an initialized value. It is considered good practice to always initialize a variable since it prevents using uninitialized memory. In the best case it's just random data, but worst case it could contain data leftover in memory that could be exploited.
In this case, you can simply initialize the pointer to the result of malloc. But in the event that you wanted to leave the memory unallocated, the best practice is to assign it to NULL (which is just 0). This also allows you to test if the value has at some point been initialized.
You can check for initialization is as follows:
if (p) {
printf("The value of p is %d", *p);
}
else {
printf("p is null");
}
CodePudding user response:
Why indeed. The answer is: You shouldn't.
You shouldn't even declare the variable without assigning it a value. So your code should be:
int main() {
int *p = (int *)malloc(sizeof(int));
... // check return value, jada jada jada
}
There may be a reason to first declare the variable and then later assign it if the assignment is in a different scope.
In such cases there are 2 situations where assigning nullptr
makes sense:
the assignment is conditional and you later have to check if something was assigned at all.
The compiler can't figure out the variable does get assigned something and then later warns that
p
might be used uninitialized. After carefully checking the compiler isn't actually right one might initializedp
tonullptr
to remove the warning.
CodePudding user response:
There is no need to initialize a pointer to NULL in its declaration if you are going to assign to it a value at once after the declaration.
That is such a code
int main( void )
{
int *p = NULL;
p = (int*)malloc(sizeof(int));
//...
}
does not make a great sense.
Just declare pointers in minimal scopes where they are used. For example
int main( void )
{
int *p = malloc(sizeof(int));
//...
}
Consider another example
void f( void )
{
unsigned int x;
printf( "Enter a positive value: " );
if ( scanf( "%u", &x ) == 1 && x > 0 )
{
// do something
}
else
{
// issue an error
}
}
There is no sense to initialize the variable x
by some value. It is declared and used at once.
Of course you could write
unsigned int x = 0;
but this does not make your code more clear and error prone and moreover will only confuse the reader because to imagine the meaning of the initialization he in any case need to read further to see the if statement.
The initialization will have a meaning if to rewrite the function the following way
void f( void )
{
unsigned int x = 0;
printf( "Enter a positive value: " );
scanf( "%u", &x );
if ( x == 0 )
{
// issue an error
}
else
{
// do something
}
}
Sometimes you even are unable to initialize an object in its declaration.
Let's assume that you have a structure with many data members and their initializations depend on many conditions. So you have to write
struct S s;
if ( /* some condition */ )
{
// initialize data members according to the condition
}
else if ( /* another condition */ )
{
// initialize data members according to this condition
}
else
{
// initialize data members some other way
}
The main principle is declare variables in minimum scopes where they are used and use them at once. Redundant initializations can only confuse readers of the code because they need to think about why you initialized a variable this way and not another way.
Problems arise when between the declaration of a pointer and its usage there are many lines of code. In such a case it is desirable to initialize a pointer to NULL. But you should avoid such a situation. Usually such situations took place when the old C allowed to declare variables only in the beginning of a block and when a reader of the code looking to numerous declarations of variables in the beginning of a block can not imagine where and how the variables are used..