By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

recursion + pointer

P: n/a
HI coders !

Here is a code well commented snippet :

void stop( PROCESS_INFORMATION* p ) // enters in "stop" with "p ==
NULL"
{
static PROCESS_INFORMATION pi;
p = π //now p != NULL and "pi" IS STATIC!!! so when we
leave "stop" func
} // p must still point to static address
of "pi" , so p != NULL when we
// leave stop func
void foo( PROCESS_INFORMATION* p )
{
PROCESS_INFORMATION* pp = NULL;
static int i = 10;
while( i-- )
foo( pp ); // after 10 iterations we pass to function "stop"
stop( pp ); // here enters in "stop" with "pp == NULL"
// pp MUST BE != NULL!!!!!!!!!!!!
assert( pp != NULL ); /////// WHY pp == NULL ??????
}

int main(int argc,char** argv)
{
foo(NULL); // recursion begins
return 1;
}

THE QUESTION:
why the hell when leave "stop" function pp IS STILL == NULL, isn't
'"pi" a static variable ???
Even if I put "PROCESS INFORMATION pi" as global, pp will be NULL when
I leave stop function ,,,,, why ????

Jul 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* Ciur Eugen:
HI coders !
Please see the FAQ on how to post. A /smallest/ possible example,
preferably /portable/ code, that compiles and illustrates the problem.

Here is a code well commented snippet :

void stop( PROCESS_INFORMATION* p ) // enters in "stop" with "p ==
NULL"
{
static PROCESS_INFORMATION pi;
p = π //now p != NULL and "pi" IS STATIC!!! so when we
leave "stop" func
} // p must still point to static address
of "pi" , so p != NULL when we
// leave stop func
void foo( PROCESS_INFORMATION* p )
{
PROCESS_INFORMATION* pp = NULL;
static int i = 10;
while( i-- )
foo( pp ); // after 10 iterations we pass to function "stop"
stop( pp ); // here enters in "stop" with "pp == NULL"
// pp MUST BE != NULL!!!!!!!!!!!!
assert( pp != NULL ); /////// WHY pp == NULL ??????
}

int main(int argc,char** argv)
{
foo(NULL); // recursion begins
return 1;
}

THE QUESTION:
why the hell when leave "stop" function pp IS STILL == NULL, isn't
'"pi" a static variable ???
Even if I put "PROCESS INFORMATION pi" as global, pp will be NULL when
I leave stop function ,,,,, why ????
You're passing 'p' by value. The formal argument 'p' in 'stop' is
initialized with a copy of the actual argument. Changes to 'p' do not
affect the actual argument.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 9 '06 #2

P: n/a
"Ciur Eugen" <ci********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
HI coders !

Here is a code well commented snippet :

void stop( PROCESS_INFORMATION* p ) // enters in "stop" with "p ==
NULL"
{
static PROCESS_INFORMATION pi;
p = &pi; //now p != NULL and "pi" IS STATIC!!! so when we
leave "stop" func
} // p must still point to static address
of "pi" , so p != NULL when we
// leave stop func
void foo( PROCESS_INFORMATION* p )
{
PROCESS_INFORMATION* pp = NULL;
static int i = 10;
while( i-- )
foo( pp ); // after 10 iterations we pass to function "stop"
stop( pp ); // here enters in "stop" with "pp == NULL"
// pp MUST BE != NULL!!!!!!!!!!!!
assert( pp != NULL ); /////// WHY pp == NULL ??????
}

int main(int argc,char** argv)
{
foo(NULL); // recursion begins
return 1;
}

THE QUESTION:
why the hell when leave "stop" function pp IS STILL == NULL, isn't
'"pi" a static variable ???
Even if I put "PROCESS INFORMATION pi" as global, pp will be NULL when
I leave stop function ,,,,, why ????
The parameter p to stop is a pointer, not a reference to a pointer. You're
passing by value, which means that the p you're modifying within the stop
function is a ***local copy*** of whatever you passed in, in this case the
pp in foo. Modifying the local copy doesn't change the original pointer.
Another example:

void f(int i)
{
i = 23;
}

void g()
{
int j = 9;
f(j);
// j still equals 9 here because you modified the local copy
}

Compare this to:

void f(int& i) // now we're using a reference, i inside f refers to the
original int passed in
{
i = 23;
}

void g()
{
int j = 9;
f(j);
// now j equals 23
}

Read up about references in an appropriate book. Note that you can also
achieve the same results with pointers:

void f(int *i)
{
*i = 23; // obviously we're assuming i != NULL here... (it's an
example)
}

void g()
{
int j = 9;
f(&j);
// now j equals 23
}

Hope this helps a bit :)
Stu
Jul 9 '06 #3

P: n/a
Thanks.
I just forgot about "pointers transffered by value" ;)

Jul 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.