c - Freeing array of struct -
c - Freeing array of struct -
i've done research , couldn't find reply problem.
i'm having problems freeing struct.
this how create struct:
struct construction * newstructure(int n) { struct construction * structure; int i; construction = (struct construction * ) malloc(n * sizeof(struct structure)); (i = 0; < n; i++) { structure[i].i_number = (int * ) malloc(sizeof(int)); structure[i].c_char = (char * ) malloc(sizeof(char)); structure[i].c_char[0] = '\0'; structure[i].d_float = (double * ) malloc(sizeof(double)); } homecoming structure; }
everything works point. later fill every variable random values not empty.
i phone call freememory
function freememory(structure, amountofstructures);
, here freememory function itself:
void freememory (struct structure* structure, int n) { int i; for( i=0 ; i<n ; i++ ) { if (structure[i].i_number!=null) free(structure[i].i_number); if (structure[i].c_char!=null) free(structure[i].c_char); if (structure[i].d_float!=null) free(structure[i].d_float); } free(structure); }
the free(structure) part works fine. there problems loop , have no thought i'm doing wrong here.
@edit i'm adding struct declaration:
struct structure{ int *i_number; char *c_char; double *d_float; };
@edit2 that's function initializes struct:
struct structure* randomizing (int n) { struct structure* construction = newstructure(n); int i; srand(time(null)); (i = 0; < n; i++) { int _i; char _c; double _d; _i = rand()%1000000; _c = "abcdefghijklmnopqrstuvwxyz" [rand () % 26]; _d = 0; setstructurenumber(structure, i,(int*) _i); setstructurechar(structure, i, (char*) _c); setstructuredouble(structure, i, &_d);
// i've commented out mutators above , error not show anymore, theres wrong them } homecoming structure; }
and im calling this:
struct structure* structure; construction = randomizing(amountofstructures);
the mutators used:
// mutators void setstructurenumber (struct structure* structure, int p, int* num) { if (structure[p].i_number != null) free(structure[p].i_number); structure[p].i_number = (int*) malloc (sizeof(int)); structure[p].i_number = num; } void setstructurechar (struct structure* structure, int p, char* str) { if (structure[p].c_char != null) free(structure[p].c_char); structure[p].c_char = (char*) malloc (sizeof(char)); structure[p].c_char = str; } void setstructuredouble (struct structure* structure, int p, double* dou) { if (structure[p].d_float != null) free(structure[p].d_float); structure[p].d_float = (double*) malloc (sizeof(double)); structure[p].d_float = dou; }
the reason somewhere in code go out of bounds of memory allocated , destroy integrity of heap. encountered practical manifestation of such undefined behavior failure @ free
, when library detects problem heap.
inside allocation cycle allocate 1 object of each respective type each field of struct object. example, allocate 1 character c_char
field , initialize \0
. might suggest c_char
intended hold string (is it?). if so, memory allocated sufficient empty string only. if not reallocate memory later, attempts place longer string memory break integrity of heap , trigger undefined behavior.
the same applies other fields well. however, without explanations not possible whether right or wrong. @ least, have provide definition of struct structure
. , have explain intent. why dynamically allocating single-object memory struct fields instead of making these objects immediate members of struct?
the additional code posted , utterly broken.
firstly calling mutators as
setstructurenumber(structure, i,(int*) _i); setstructurechar(structure, i, (char*) _c); setstructuredouble(structure, i, &_d);
this not create sense. why trying convert integer value _i
pointer type??? if want obtain pointer _i
, done &_i
. correctly in lastly call, pass &_d
. why first 2 calls different lastly one? logic behind this?
secondly, within mutator functions
void setstructurenumber (struct structure* structure, int p, int* num) { if (structure[p].i_number != null) free(structure[p].i_number); structure[p].i_number = (int*) malloc (sizeof(int)); structure[p].i_number = num; }
you freeing old memory , allocating new memory. why? why don't reuse old memory store new value? (btw, there's no need check pointer null before calling free
, because free
check internally anyway.)
thirdly, after allocating new memory leak overriding pointer value returned malloc
pointer value passed outside
structure[p].i_number = num;
again, not create sense. causes crash on free
- pointers pass outside either meaningless random values (like (int *) _i
or (char *) _c
)) or point local variable (like &_d
).
there's no way "correct" code without knowing trying in first place. there many unnecessary memory allocations , reallocations , other illogical things. rewrite mutator functions as
void setstructurenumber (struct structure* structure, int p, int num) { *structure[p].i_number = num; }
note - no memory reallocations , argument passed value.
the functions called as
setstructurenumber(structure, i, _i); setstructurechar(structure, i, _c); setstructuredouble(structure, i, _d);
but again, vastly different have don't know whether need.
c memory struct free alloc
Comments
Post a Comment