On Mon, Apr 28, 2014 at 1:20 PM, Thomas Schmitt <scdbac...@gmx.net> wrote:

> Hi,
>
> > I really need a disassembly and to be able to probe the runtime
> It's the job of a C union to provide a common hull around objects
> of different size. One may dispute whether using union is a good
> idea (like overloading in the OO paradigm). But unions are part of C
> since K&R and they are supposed to be safe.
>
> http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Size-of-Unions


No, it's plain wrong. Unions are fine, if used properly. You aren't using
them properly. Let me show you how by a more extreme example:

#include <stdlib.h>

struct small {
   int a;
};

struct big {
   int b[1024*1024];
};

union both {
   struct small imSmall;
   struct big imBig;
};


void copy(union both* b, void* data) {

    *b = *(union both*)data; //copies 4MB of data.
}

void main() {
    struct small smallThing;
    union both* bothThings = malloc(sizeof(union both));
    copy(bothThings, &smallThing); // NOT OK. struct small cannot NOT be
converted to union both.
}

figgles@ghost:~$ ./big
Segmentation fault
----
The problem is that union can convert to a member (by accessing the field),
but a member CANNOT convert to a union. add_worker() takes a member and
tries to convert it to a union. This is WRONG. Period.

Reply via email to