C
Conor F
(Trying this again as the velocityreviews site doesn't seem to forward
to NNTP - hope this doesn't appear twice!
Back to this old topic again. Sorry about this but I'm just not sure
if aliasing applies in this case. Question 2.6 in the FAQ describes
the case of using one malloc and piggy backing a char * onto it. It's
a pretty common idiom I would have thought, but I'm now having my
doubts:
struct name {
int namelen;
char *namep;
};
struct name *makename(char *newname)
{
char *buf = malloc(sizeof(struct name) + strlen(newname) + 1);
struct name *ret = (struct name *)buf;
ret->namelen = strlen(newname);
ret->namep = buf + sizeof(struct name);
strcpy(ret->namep, newname);
return ret;
}
I don't really believe there are aliasing issues here due to a char *
being reassigned to a struct name *.
But. If you did this instead:
struct name *ret = malloc(sizeof(struct name) + strlen(newname) +
1);
char *buf = (char *)(&ret[1]);
which also seems a perfectly reasonable way of going about it, and
avoids the sizeof(struct name) addition which can be a little tricky
in the cases where you have several leading structs. Ok, it's not
terrible but I always though the above was clearer.
Anyway - you've now taken an object of type struct name and converted
to a different type pointing to the same memory.
Isn't that an aliasing issue?
And if char * is a special case then what if I had used a wchar_t
instead? Or another type?
It's all a bit subtle for me.
Conor.
(Hum. Google won't let me post with my hotmail address any more. How
annoying...)
to NNTP - hope this doesn't appear twice!
Back to this old topic again. Sorry about this but I'm just not sure
if aliasing applies in this case. Question 2.6 in the FAQ describes
the case of using one malloc and piggy backing a char * onto it. It's
a pretty common idiom I would have thought, but I'm now having my
doubts:
struct name {
int namelen;
char *namep;
};
struct name *makename(char *newname)
{
char *buf = malloc(sizeof(struct name) + strlen(newname) + 1);
struct name *ret = (struct name *)buf;
ret->namelen = strlen(newname);
ret->namep = buf + sizeof(struct name);
strcpy(ret->namep, newname);
return ret;
}
I don't really believe there are aliasing issues here due to a char *
being reassigned to a struct name *.
But. If you did this instead:
struct name *ret = malloc(sizeof(struct name) + strlen(newname) +
1);
char *buf = (char *)(&ret[1]);
which also seems a perfectly reasonable way of going about it, and
avoids the sizeof(struct name) addition which can be a little tricky
in the cases where you have several leading structs. Ok, it's not
terrible but I always though the above was clearer.
Anyway - you've now taken an object of type struct name and converted
to a different type pointing to the same memory.
Isn't that an aliasing issue?
And if char * is a special case then what if I had used a wchar_t
instead? Or another type?
It's all a bit subtle for me.
Conor.
(Hum. Google won't let me post with my hotmail address any more. How
annoying...)