Fields of a struct gets corrupted while in an ADT module
i am try to make an ADT of a set that contains no more then 1 of the same variable, now when i try to send struct to it memebers of the sturct gets corrupted, im guessing there is a problem whith the line s->array = (element*)malloc(setSize*sizeof(element)) ;
but im not really sure about it.
this is my main
int main()
{
student st1;
st1.age=10;
st1.id=123;
st1.name="Erik";
student st2;
st2.age=12;
st1.id=181;
st1.name="Loren";
Set studset=setCreate(99,copystud,freestud,compstud);
if(studset==NULL)
{
printf("error");
exit(-1);
}
element* elm;
Insert(studset,(element)&st1);
Insert(studset,(element)&st1);
Insert(studset,(element)&st2);
printstudents(studset);
return 1;
}
these are the function i send to setCreate
element copystud(element s)
{
student* new_s;
new_s=(student*)malloc(sizeof(student));
((student*)new_s)->name = (char*) malloc (strlen(((student*)s)->name) + 1);
strcpy(((student*)new_s)->name,((student*)s)->name);
new_s->age=((student*)s)->age;
new_s->id=((student*)s)->id;
return new_s;
}
void freestud(element s)
{
free(((student*)s)->name);
free(s);
return;
}
Comparison compstud(element a, element b)
{
{
if((((student*)a)->id)<(((student*)b)->id))
return Smaller;
if((((student*)a)->id)>(((student*)b)->id))
return Bigger;
return Equal;
}
this is my header:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
typedef enum {Fail, Success} Result ;
typedef enum {Smaller,Equal,Bigger} Comparison;
typedef void* element;
typedef struct Set* Set;
struct student
{
char* name;
int age;
int id;
};
typedef struct student student;
typedef element (*cpy_func)(element);
typedef void (*free_func)(element);
typedef Comparison (*comp_func)(element,element);
Set setCreate(int setSize,cpy_func copy_elm,free_func free_elm,comp_func comp_elm);
Result Insert(Set s, element elm);
Result removeByIndex (Set mySet, element index); //remove the item at specified index from set
element getByIndex(Set s, element index); //get the value at the current Index, if invalid returns null.
Result pop(Set s);
void setDestroy(Set s);
//
void printstrset(Set s);
void printdoubleset(Set s);
void printstudents(Set s);
void printstud(element s);
#endif // HEADER_H_INCLUDED
and these are the create and insert functions
Set setCreate(int setSize,cpy_func copy_elm,free_func free_elm,comp_func comp_elm)
{
Set s ;
if ( setSize <=0 || free_elm==NULL || copy_elm == NULL || comp_elm ==NULL)
return NULL ;
s = malloc (sizeof(Set)) ;
if (s == NULL)
return NULL;
s->array = (element*)malloc(setSize*sizeof(element)) ;
if (s->array == NULL)
{
free(s) ;
return NULL ;
}
s->top = 0;
s->maxCapacity = setSize ;
s->cp_elm = copy_elm ;
s->fr_elm = free_elm;
s->cmp_elm= comp_elm;
return s ;
}
Result Insert(Set s, element elm)
{
element tmp = NULL;
int i;
if ( s == NULL || elm == NULL || s->top >= s->maxCapacity )
{
return Fail;
}
for(i=0; i<s->top; i++)
{
if(s->cmp_elm(s->array[i],elm) == Equal)
return Fail;
}
//printstud(elm);
tmp = s->cp_elm(elm);
if (tmp == NULL)
{
return Fail;
}
s->array[s->top] = tmp;
s->top++ ;
return Success;
}