473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

recursion + pointer

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
3 2652
* 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
"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
Thanks.
I just forgot about "pointers transffered by value" ;)

Jul 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
8
by: pembed2003 | last post by:
Hi all, As an exercise, I am trying to come up with a function to count the length of a char* string using recursion. I came up with this: int count_length(char* s){ if(s == 0) return 0;...
4
by: Thrillhouse | last post by:
I'm a college computer science major and I'm studying for my final exam by going over previous tests. There's one question that I answered correctly but I cannot, for the life of me, figure out...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
9
by: John Pass | last post by:
I am trying to understand how recursion exactly works. See example (section) below: Say i = 4, then Number = 4 during 1st loop. Is Factorial(number - 1) automatically recognised as a recursive...
8
by: No bother | last post by:
I have a table with two columns, one named master, the other slave. Each column has a set of numbers. A number in one column can appear in the other. I am trying to see if there is any infinite...
10
by: Lamefif | last post by:
can anyone explain the output of this function. im having trouble comprehending it void ret_str(char* s) { if(*s != '\0') //{ cout<<*(s) ; ret_str(s+1); //cout<<*(s) ;
5
by: Newbie | last post by:
Sorry if this appears twice. Input : Hello I am a newbie Output: newbie a am I Hello I am getting a segfault on running the progra.The debugger shows that it happens in the reverse routine...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.