Sunday, July 27, 2008

C/C++ - Pitfalls - part III





Make your code to work on multiple machines, for as long as possible.

Even though the C-language is well standardized, there's still many pitfalls regarding code portability. First of all, with pure ANSI C it is really hard to create any graphical applications. Real program writing usually relies upon some extensions, Eg. libraries not belonging in standard C. Luckily there's many libraries which are available for multiple platforms.


But even if we put that pr
oblem aside, we will find issues which may limit our code's portability. I will now represent one such thing because this must be noticed when one wishes his code to be working on different machines.

Back when things were simpler, I am
ongst many others used this kind of functions for calculating memory addresses.


void * get next_one(void **ptr, size_t sizeofobj)
{
    int address=(int)(*ptr); //store the address in integer
    address+=(int)sizeofobj;
    return (void *)address;
}

As we see, abowe function simply calculates a specific memory address. However, since computers are not same as they used to be, this nowadays introduces possible hazards... Can you spot the root of the problem?

No comments:

Post a Comment