hello
I wrote a test program for learning usage of realloc() and I thin I did everything right but I get this error exactly with my 4th time that while tries to reallocate memory in MakeDSt function for DSt->SArray:
Quote:
realloc(): invalid next size
|
this is my complete code :
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct DRA
{
int addr;
} DRA;
typedef struct {
int index;
DRA *RArray;
} DR;
typedef struct {
int start;
} DSA;
typedef struct {
int index;
DSA *SArray;
} DS;
int MakeDRg(DR *DRg)
{
unsigned int Current_Row=0;
DRg->index=5;
DRg->RArray=(DRA *)malloc(DRg->index*sizeof(DRA));
while(Current_Row<DRg->index)
{
DRg->RArray[Current_Row].addr=Current_Row;
Current_Row++;
}
return 1;
}
int MakeDSt(DS *DSt,DR *DRg)
{
unsigned int Current_Row=1;
DSt->SArray=(DSA*)malloc(sizeof(DSA));
DSt->SArray[0].start=DRg->RArray[0].addr+1;
while(Current_Row < DRg->index)
{
DSt->SArray=(DSA*)realloc(DSt->SArray,sizeof(DSA));
DSt->SArray[Current_Row].start=DRg->RArray[Current_Row].addr+1;
Current_Row++;
DSt->index=Current_Row;
}
return 1;
}
int main()
{
DR DRg;
DS DSt;
unsigned int Current_Row=0 ;
while(1)
{
MakeDRg(&DRg);
MakeDSt(&DSt,&DRg);
while(Current_Row<DSt.SArray)
{
printf("arr:: %i\n",DSt.SArray[Current_Row].start);
Current_Row++;
}
free(DSt.SArray);
free(DRg.RArray);
}
return 0;
}
what did I do wrong?
Thanks for any help.