Pointer and Genetic Programming gurus this way
I am still working on that GP demo, and I almost got it nailed...but there is one problem.
Okay, suppose there is this function, Crossover, with this declaration:
Crossover(Node *p1, Node *p2, Node **c1, Node **c2)
and the the population of trees is declared as follows:
Node** current_pop; //the population of trees
The crossover function is called like this:
Crossover(current_pop, current_pop[i+1], ¤t_pop[i+2], ¤t_pop[i+3])
Now in the crossover function, c1 and c2 is referred to as (*c1) and (*c2) respectively, but I am not entirely sure what this means.
Anyway, whenever the program gets to lines using c1 and c2 the program crashes. Sometimes it works fine, sometimes it doesn''t.
So, my question is this - based on what i have mentioned so far, what exactly does this (*c1) mean, and what could cause the program crash? I''m guessing it''s something to do with accessing memory that shouldn''t be accessed, but the thing is, it should be.
Thanks.
"16 days?? that's almost two weeks!!" Tucker
In C when you want to be able to modify a parameter, you actually pass the address of the parameter instead, and treat it as a pointer inside the function (as it should be).
Since c1 is a Node**, that is, the address of a pointer to a node. And c1 actually is a pointer to a Node. You could possibly call your function that way :
Or, alternatively
Since c1 is a Node**, that is, the address of a pointer to a node. And c1 actually is a pointer to a Node. You could possibly call your function that way :
Crossover(current_pop, current_pop[i+1], ¤t_pop[i+2], ¤t_pop[i+3] );
Or, alternatively
Crossover(current_pop, current_pop[i+1], current_pop+(i+2), current_pop+(i+3) ); </pre> <br><br>But, however sure you are the memory is valid, be sure to check the boundary conditions : the fact that <pre>current_pop </pre> exists doesn''t mean that <pre>current_pop[i+3] </pre> does.<br> </i>
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by FrunyCrossover(current_pop, current_pop[i+1], ¤t_pop[i+2], ¤t_pop[i+3] );
sorry, those t_pops are supposed to be current_pop[i+2], current_pop[i+3]. dunno how that happened.
Anyway, what are those symbols next to the last 2 parms? ampersands? Cos that''s what I used.
In any case, the compiler throws up errors if i try anything else.
Also, forgive me if this seems stupid, but how do you check boundary collisions? I''ve already taken steps to make sure certain variables exist before progressing eg:
while(!whatever)
{
//keep trying
}
anyway, thanks for replying.
"16 days?? that's almost two weeks!!" Tucker
November 27, 2001 04:04 PM
Whenever a function needs to change the value of a pointer being passed in, you need to pass in the address of the pointer (ie. passing in Node**). When you want to modify the pointer or access the pointer, you need to dereference it.
Example
void Foo(CBar** lpBar)
{
*lpBar = new CBar();
}
CBar* pBar = NULL;
Foo(&pBar);
Example
void Foo(CBar** lpBar)
{
*lpBar = new CBar();
}
CBar* pBar = NULL;
Foo(&pBar);
quote: Original post by Anonymous Poster
Whenever a function needs to change the value of a pointer being passed in, you need to pass in the address of the pointer (ie. passing in Node**). When you want to modify the pointer or access the pointer, you need to dereference it.
Example
void Foo(CBar** lpBar)
{
*lpBar = new CBar();
}
CBar* pBar = NULL;
Foo(&pBar);
Yeah, I understand all that, so, could you tell me how would you access the attributes and functions of lpBar within Foo? In my program it would be (*lpBar)->whatever(). Or is it something different? The brackets have to be round it, else it won't work.
Edited by - Mister Stewart on November 27, 2001 5:35:00 PM
"16 days?? that's almost two weeks!!" Tucker
While I appreciate that it is not always obvious which forum a particular question should be sent to, this is very clearly a programming question and would have been best served by submitting it to General Programming Forum.
Regards,
Timkin
Regards,
Timkin
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement