Setting pointer to null!

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hello,

As a rule of thumb, is it okay to declare a local pointer in a function and
set it to null right away like this:

====================
void f1()
{
int a, *x = NULL;
x = &a;

...... do other stuff....
}
====================

Or is it better like this:
===================
void f2()
{
int a, *x;
x = &a;

...... do other stuff....
}
=====================

The thing is that in F1, when we leave the function, x won't exist anymore,
so I don't see the need to set it to null in the first place. I could be
wrong in this assumption though!

Also in another sample code I was returning a pointer that could sometimes
not be assigned an address... this crashed my app. I guess I was returning a
null pointer in that instance.

For example, suppose I do this:
====================
typedef struct g{
int u;
} GSO;

GSO d[1];

GSO *f1(int w, GSO d[])
{
GSO *x = NULL;

if(w>10)
{ x= &d[0]; }

//..... do other stuff....
return x;
}

int main()
{
GSO *k=NULL;
int w = 5;

k = f1(w, d); // If my pointer returned is null.. what happens to k?

//.... other code...
return 0;
}

==================

If, w equals to 5, then the pointer is really not assigned an address and I
was returning it to the calling routine and that's what probably crashed the
app. Obviously I should of done something like this:
==============
if(w>10)
{ x= &d[0];
return x;
}
==============

In anycase, what is the rule of thumb, should we or should we not declare a
local pointer with null? To solve the afforsaid problem I simply took the
null out and there was no more crashes. But am I wrong in doing this?

Sorry for the oversimplified question, its just that I ran into this
particularity today and would like to know if anyone can help clear up this
issue.

Thanks for you feedback!

--
Best regards
Roberto
.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Should io(read|write)(8|16|32)_rep take (const|) volatile u(8|16|32) __iomem *addr?
    ... the destination pointer on user-space ... @src: ... const void *data, int bytelen); ...
    (Linux-Kernel)
  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • Re: Whats the meaning of this code
    ... void * work ... Whats the meaning of this line:- ... The meaning...a function called work that accepts a pointer of type ... pointer value is cast to an int value. ...
    (comp.lang.c)