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 problem 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 amongst 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
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?
|All examples||No related posts|
|Explode function in C||ANSI C explode|
|Atomic Operations||(Finnish!) Atomiset Operaatiot (säikeet II)|
|Packed Array||C - optimize memory usage|
|Bitset in C||C - optimize memory usage|
|Trim/Rtrim (examples extended beyond post)||Trim/Rtrim|
|Linked list||No blog posts|
|Lottery machine||You can do OOP in C|