Announcement

Collapse
No announcement yet.

Possible bug in evaluation of ptr variables.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Possible bug in evaluation of ptr variables.

    This works:
    t1 := itemPtrs[ itemIdx];
    *t1 := *t1 + 1;
    This doesn't;
    *itemPtrs[itemIdx]++;

  • #2
    No, that is not a bug. 4DGL parser works pretty much the same as C.

    Your first case works, second case will not work.

    possibly the best working example would be:-
    p := itemPtrs[itemIdx];
    (*p)++;

    Here is an example that was compiled with WINLCC to show C behaviour is the same.
    int itemPtrs[20]; // our array of pointers
    int mytestvar = 1234; // the test variable
    int itemIdx = 5; // an abitrary index

    itemPtrs[itemIdx] = (int) &mytestvar; // ok put a pointer to the test var in the array

    fprintf(stdout, "%08X\n", itemPtrs[itemIdx]); // show the pointer value
    (int *) itemPtrs[itemIdx]++; // this wont increment our variable, bug? no, its just the way C
    // (and similar languages work) the pointer at itemPtrs[itemIdx] is incremented, not mytestvar

    fprintf(stdout, "%d\n", mytestvar); // so, mytestvar has note changed

    fprintf(stdout, "%08X\n", itemPtrs[itemIdx]); // (proof that just the pointer is incremented)

    itemPtrs[itemIdx] = (int) &mytestvar; // ok, that didnt work, now restore things for another try with a pointer.....

    int *p = (int *) itemPtrs[itemIdx]; // now, we know for sure we have the pointer to mytestvar

    fprintf(stdout, "%08X\n", (int) p); // print the value of the pointer to make sure.....

    *p++; // and you may think this would increment mytestvar,
    // but deceivingly, its just a pointer, not a pointer to a pointer
    // (the '*' is superfluous in this context)
    // any of these will work though

    *p = *p + 1;

    (*p)++;

    *p += 1;


    Regards,
    Dave

    Comment

    Working...
    X