Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
strict-aliasing??
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Ivan Riis Nielsen, post: 4055679"] Hmm, you think I should do this in C++? You're probably right, it would certainly be a lot easier... Yes, I tried this, and it does not give the strict-aliasing warning. However, this is not quite generic... If the class hierarchy had more levels, ie. a class 'deriv2' could have a 'deriv_struct' as its first field, which in turn has a 'base_struct' as its first field. This one does it and still does not give the strict-aliasing warning: #define UNIVERSAL_ASSIGN2(dst,src) \ do { \ if (src) ++(((base)(src))->count); \ if (dst) --(((base)(dst))->count); \ (dst) = (src); \ } while (0) However, this has another problem: Both src and dst are evaluated multiple times. This was why I used the local variables of type 'base' (struct pointer) and 'base*' (pointer to struct pointer). This next one is identical to my original UNIVERSAL_ASSIGN, except that the actual derived type name is passed in. It solves the problem but is of course a bit less generic. #define UNIVERSAL_ASSIGN3(Type,dst,src) \ do { \ Type * _pdst=&(dst); \ base _src=(base)(src); \ if (_src) ++(_src->count); \ if (*_pdst) --((((base)(*_pdst)))->count); \ *_pdst=(Type)_src; \ } while (0) Yes, that's true, the programmer would have to UNREF() object reference variables when exiting their scope. Maybe the assignment macro is too clumsy, and simple REF() and UNREF() macros would be more suitable. Or one could go to C++ to make it prettier. Thank you for your inputs, Ivan [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
strict-aliasing??
Top