Hi i'm new here and would like to ask for your help. I have a running program which looks like this:
Code:
typedef struct{
double value;
double seqs[10];
} SEQRANK;
typedef struct{
...
double h[2*10];
} CCDATA;
int arrn[2];
int calculateCC(CCDATA ccd[], int AorB){
int i,err=0;
...
back:
return err;
}
int findBestSeqs(SEQRANK sqr[], CCDATA ccd[], int AorB){
int i,err=0;
double TBuf[10];
double nBuf[10];
...
back:
return err;
}
int main(void){
int i,err=0;
CCDATA CC[2];
SEQRANK SR[2*5];
...
for(i=0;i<2;i++){
err = calculateCC(CC,i);
if (err == 1){
printf("error in calculateCC\n");
goto back;
}
err = findBestSeqs(SR, CC, i);
if (err == 1){
printf("error in findBestSeqs\n");
goto back;
}
}
back:
return err;
}
As you see, the program is does everything two times (case A and B). A int is passed to the subfunction, indicating if its case A (=0) or case B (=1). I have a structure SEQRANK that consists of a sequence of doubles and a value, evaluating the the sequence. Accoarding to this value the sequence will end up in the corresponding place of SR[2*5]. First five slots are for the best sequences of case A, second five slots for case B. CC is also a structure array (CCDATA) with two datasets for the two cases. Right now the structures include arrays with a fixed length. But this length is not always the same for A and B and I find a dynamic solution more elegant, but having trouble to implement it. I have come up with this:
Code:
typedef struct{
double value;
double *seqs;
} SEQRANK;
typedef struct{
...
double *h;
} CCDATA;
int arrn[2];
int calculateCC(CCDATA ccd[], int AorB){
int i,err=0;
ccd[AorB].h = malloc(2*arrn[AorB] * sizeof(double));
if (!ccd[AorB].h){
printf("Out of memory: ccd.h\n");
err = 1;
goto back;
}
...
back:
return err;
}
int mallocSeqRank(SEQRANK sqr[], int AorB){
int i,err=0;
for(i=0;i<5;i++){
sqr[AorB*5+i].seqs = malloc(arrn[AorB] * sizeof(double));
if (!sqr[AorB*5+i].seqs){
printf("Out of memory: sqr->seqs\n");
err = 1;
goto back;
}
}
back:
return err;
}
int freeSTRUCTS(SEQRANK sqr[], CCDATA ccd[]){
int i,j,err=0;
if(sqr){
for(i=0;i<2;i++){
for(j=0;j<5;j++){
free(sqr[i*5+j].seqs);
}
}
}
if(ccd){
for(i=0;i<2;i++){
free(ccd[i].h);
}
}
return err;
}
int findBestSeqs(SEQRANK sqr[], CCDATA ccd[], int AorB){
int i,err=0;
double *TBuf;
double *nBuf;
TBuf = malloc(arrn[AorB] * sizeof(double));
if (!TBuf){
printf("Out of memory: TBuf\n");
err = 1;
goto back;
}
nBuf = malloc(arrn[AorB] * sizeof(double));
if (!nBuf){
printf("Out of memory: nBuf\n");
err = 1;
goto back;
}
...
back:
free(TBuf);
free(nBuf);
return err;
}
int main(void){
int i,err=0;
CCDATA CC[2];
SEQRANK SR[2*5];
...
for(i=0;i<2;i++){
err = mallocSeqRank(SR,i);
if (err == 1){
printf("error in mallocSeqRank\n");
goto back;
}
}
...
for(i=0;i<2;i++){
err = calculateCC(CC,i);
if (err == 1){
printf("error in calculateCC\n");
goto back;
}
err = findBestSeqs(SR, CC, i);
if (err == 1){
printf("error in findBestSeqs\n");
goto back;
}
}
back:
err = freeSTRUCTS(SR, CC);
return err;
}
Allocating the memory works but freeing wont. freeSTRUCT doesnt work.
Furthermore, what I find very strange, the two buffers i need in findBestSeq TBuf and nBuf are allocate and freed the first time findBestSeq is called. But when its called the second time, to calculate case B, malloc wont allocate memory.
Any help would be appreciated.