Andrew Haley wrote:
Robert William Fuller wrote:
Naturally, when I started trying to call this function, I started
getting the type-punned pointer aliasing warnings. The code in
question looked roughly like this:
static opt_param_t *shrtOpts, *longOpts;
rc = reallocItems((void **) &shrtOpts, sizeof(opt_param_t),
&shrtOptAlloc, numShrtOpts, newShrtOpts, shrtOptHint);
Obviously, this breaks the aliasing rules. I read that I could work
around this by casting through a union. I settled on this approach,
but I'm not sure if it is valid, or if I'm merely masking the problem:
typedef union _opt_param_alias_t {
opt_param_t *o;
void *v;
} opt_param_alias_t;
rc = reallocItems(&((opt_param_alias_t *) &shrtOpts)->v,
sizeof(opt_param_t), &shrtOptAlloc, numShrtOpts, newShrtOpts,
shrtOptHint)
It really isn't necessary to do this. The rule is that you mustn't use
a pointer cast to create an lvalue of incompatible type. So, do this:
static opt_param_t *shrtOpts, *longOpts;
void *p1;
rc = reallocItems(&p1, sizeof(opt_param_t),
&shrtOptAlloc, numShrtOpts, newShrtOpts, shrtOptHint);
shrtOpts = p1;
I suspected that might work. I asked so I would not have to look at
gcc's output. I have not looked at (dis)assembly since I worked on
Windows NT in its early days when things were even more closed than they
are now. I am now a totally reformed free software user and programmer.
(Ask yourself if there is a causal relation there.)
I want to make sure I am very clear on this. Are you saying the
following is okay?
p1 = shrtOpts;
rc = reallocItems(&p1, ...)
shrtOpts = p1;
Thank you.
Regards,
Rob