I have the following problem, the following function is called with conflict initialised to NULL.
At the end of foo, conflict takes the correct values. In the case of this example, *conflict should contain the values 4, 8 and 2. Which it does, according to the fprintfs.
However, when I test again in the function calling foo (see second code excerpt), the conflict array hasn’t been modified. I’m not sure why, since I’m passing a pointer to the array, especially since this technique works well for the multiDB and recursive pointers. Any help would be appreciated. (this is not the full code by the way, I’ve only shown the relevant parts). Thanks!
int foo(
/*==================*/
uchar* buf,
uint* multiDB,
uint* recursive,
uint** conflict) {
select_consistent= conflict;
bool finished;
if (((start_of_scan == 1) && (*multiDB != 1)) || ((start_of_scan== 0) && (select_consistent == NULL))) {
fprintf(stderr, "Not doing select consistent \n ");
finished = TRUE;
}
else {
*multiDB=0;
fprintf(stderr, "Doing select consistent \n ");
finished = FALSE;
uint n;
int i = 0 ;
if (select_consistent == NULL) { /*This is the first round */
next_round = FALSE;
fp = popen("./algorithm '[t(1,[t(2,||),t(3,[t(8,||),t(10,||)])]).t(1,[t(4,||),t(6,||)]).t(1,[t(2,||),t(7,||)])]'", "r"); /* Issue the command. */
finished = FALSE;
}
if (next_round == TRUE ) {
goto parse_records;
}
fscanf(fp, "%lu", &n);
uint* conflict_ ;
if (n!=0) conflict_ = (uint*) malloc(sizeof (uint) * n);
conflict = &conflict_;
next_round = TRUE;
int j= 0 ;
while (fscanf(fp, "%lu", &n) != EOF) {
if (n == 0) {
select_consistent=conflict;
goto parse_records;
}
else {
(*conflict)[j] = n;
}
i++;
j++;
}
finished = TRUE;
}
parse_records:;
int error;
.... [other code] foo2(multiDB, recursive);
fprintf(stderr, "Array states %lu %lu \n ", (*conflict)[0], (*conflict)[1]);
fprintf(stderr, "Array states %lu %lu \n ", (*select_consistent)[0], (*select_consistent)[1]);
return error
}
The function calling foo does it like this:
uint** conflict = NULL ;
error = foo(buf, multiDB, recursive, conflict);
fprintf(stderr, "value is %lu \n", (*conflict)[0]); //This is still unitialised.
It appears that the code has some pointer indirection problems. It seems that
conflictis meant to be a single dimensional array ofuint. If so, then the initial declaration should be:Then pass it like this:
Then in
fooallocate it as follows:Or if you still want to use
conflict_, assign it like this: