Reputation: 319
typedef struct{
void *val;
}struct_t;
void test(int val)
{
struct_t *s = malloc(sizeof(struct_t));
s->val = malloc(sizeof(int));
*(int*)&s->val = val;
free(s->val);
}
The free generates a sigabrt but I think that's more just a symptom of the way that I'm casting s->val to get the assignment to work. What's the proper way to perform this assignment?
Upvotes: 1
Views: 169
Reputation: 34527
*(int*)&s->val = val;
Says "take the address of the field val in struct s and change write "val" into it. This makes s->val point to address stored in argument of test "val". What you meant is
*(int*)s->val = val;
Upvotes: 7