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: 4055560"] Hello everyone, I have been playing around with object oriented techniques in C. Specifically, I wanted to implement generic reference counting. This would be implemented by a base class which would always be the first field in all derived classes. The reference counting would then be handled by a universal assignment macro. Below is some trimmed down code demonstrating my issue: gcc (4.4.3) complains about strict-aliasing rules. % gcc -O3 -Wall t1.c t1.c: In function ‘main’: t1.c:28: warning: dereferencing pointer ‘_pdst’ does break strict-aliasing rules t1.c:28: note: initialized from here The output from the program is % ./a.out d1 = {{1},2} d1 = 0x9b53008, d2 = (nil) d1 = {{2},2} d1 = 0x9b53008, d2 = 0x9b53008 The compiled program does as expected. I don't understand exactly what the problem is, can anyone shed some light on this? Or is this a false warning? Thanks Ivan <code> #include <stdio.h> #include <stdlib.h> typedef struct { int count; } base_struct, *base; typedef struct { base_struct b; int deriv_field; } deriv_struct,*deriv; #define UNIVERSAL_ASSIGN(dst,src) \ do { \ base* _pdst=(base*)&(dst); \ base _src=(base)(src); \ if (_src) ++(_src->count); \ if (*_pdst) --((*_pdst)->count); \ *_pdst=_src; \ } while (0) int main(void) { deriv d1=(deriv)malloc(sizeof(deriv_struct)),d2=0; d1->b.count = 1; d1->deriv_field = 2; printf("d1 = {{%d},%d}\n",d1->b.count,d1->deriv_field); printf("d1 = %p, d2 = %p\n",d1,d2); UNIVERSAL_ASSIGN(d2,d1); printf("d1 = {{%d},%d}\n",d1->b.count,d1->deriv_field); printf("d1 = %p, d2 = %p\n",d1,d2); return 0; } </code> [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
strict-aliasing??
Top