local pointer need to initialize or not?
-
Hi , I read a block of code, there is a function like:
void funcA( unsigned int * dtc, unsigned int* status)
{
int rval;
unsigned int* lstatus;
rval = funcB(dtc, lstatus);
.....
}int funcB(unsigned int *idtc, unsinged int* istatus ) {
*istatus = XXX;
}I understood, lstatus is not initialized, is that good way to use a local pointer like the above?
-
Hi , I read a block of code, there is a function like:
void funcA( unsigned int * dtc, unsigned int* status)
{
int rval;
unsigned int* lstatus;
rval = funcB(dtc, lstatus);
.....
}int funcB(unsigned int *idtc, unsinged int* istatus ) {
*istatus = XXX;
}I understood, lstatus is not initialized, is that good way to use a local pointer like the above?
That code will not work. You are correct to question it. If the code actually runs it is only because of the way that the compiler lays down the stack and/or what the exact code did just before the first method was called. In the method the pointer is considered to be non-initialized but because it is a pointer that was created on the stack it will have a value, whatever was in the memory that the stack is using at that point. Thus correctly stated 'garbage'.
-
Hi , I read a block of code, there is a function like:
void funcA( unsigned int * dtc, unsigned int* status)
{
int rval;
unsigned int* lstatus;
rval = funcB(dtc, lstatus);
.....
}int funcB(unsigned int *idtc, unsinged int* istatus ) {
*istatus = XXX;
}I understood, lstatus is not initialized, is that good way to use a local pointer like the above?
-
That code will not work. You are correct to question it. If the code actually runs it is only because of the way that the compiler lays down the stack and/or what the exact code did just before the first method was called. In the method the pointer is considered to be non-initialized but because it is a pointer that was created on the stack it will have a value, whatever was in the memory that the stack is using at that point. Thus correctly stated 'garbage'.