I have these structures:
struct generic_attribute{
int current_value;
int previous_value;
};
union union_attribute{
struct complex_attribute *complex;
struct generic_attribute *generic;
};
struct tagged_attribute{
enum{GENERIC_ATTRIBUTE, COMPLEX_ATTRIBUTE} code;
union union_attribute *attribute;
};
I keep getting segmentation fault errors because I am not allocating memory properly when creating an object of type tagged_attribute.
struct tagged_attribute* construct_tagged_attribute(int num_args, int *args){
struct tagged_attribute *ta_ptr;
ta_ptr = malloc (sizeof(struct tagged_attribute));
ta_ptr->code = GENERIC_ATTRIBUTE;
//the problem is here:
ta_ptr->attribute->generic = malloc (sizeof(struct generic_attribute));
ta_ptr->attribute->generic = construct_generic_attribute(args[0]);
return ta_ptr;
}
Construct_generic_attribute returns a pointer to a generic_attribute object. I want ta_ptr->attribute->generic to contain a pointer to a generic_attribute object. This pointer to a generic_attribute object is output by the construct_generic_attribute function.
What would be the proper way to do this?
Aucun commentaire:
Enregistrer un commentaire