You need to indent your code properly, e.g.,
Code:
int i=0;
for (co_b=0; co_b < aaa->no_b; co_b++) {
for (co_x=0; co_x < aaa->bbb[co_b].no_x; co_x++) {
if ((aaa->bbb[co_b].ce = (double *)malloc(sizeof(double)*2)) == NULL) {
fprintf(stdout, "/nOut of Memory");
return EXIT_FAILURE;
}
aaa->bbb[co_b].ce[i]=5*x;
aaa->bbb[co_b].ce[i+1]=10*x;
i=i+2;
// if I add this part below I get an error saying:
// size realloc(): invalid next size:
// and then later core dumped
if ((aaa->bbb[co_b].ce = (double *)realloc(aaa->bbb[co_b].ce, sizeof(double) * (i+1)) ) == NULL) {
fprintf(stdout, "\nOut of Memory!");
exit (EXIT_FAILURE);
}
}
}
The problem appears to be that you are always allocating space for aaa->bbb[co_b].ce within the inner loop using malloc. Hence, your realloc is pointless: you will overwrite it on subsequent iterations. What you probably should do is something like this:
Code:
int i=0;
for (co_b=0; co_b < aaa->no_b; co_b++) {
if ((aaa->bbb[co_b].ce = (double *)malloc(sizeof(double)*2)) == NULL) {
fprintf(stdout, "/nOut of Memory");
return EXIT_FAILURE;
}
for (co_x=0; co_x < aaa->bbb[co_b].no_x; co_x++) {
aaa->bbb[co_b].ce[i]=5*x;
aaa->bbb[co_b].ce[i+1]=10*x;
i=i+2;
// if I add this part below I get an error saying:
// size realloc(): invalid next size:
// and then later core dumped
if ((aaa->bbb[co_b].ce = (double *)realloc(aaa->bbb[co_b].ce, sizeof(double) * (i+1)) ) == NULL) {
fprintf(stdout, "\nOut of Memory!");
exit (EXIT_FAILURE);
}
}
}