1

I have filled a dynamic allocated float multi array in a function.

A second function has to get the values of the array exploiting the pointer to the first element of the array defined in the former function.

The second function do not access to the correct memory location so it doesn't work but it does if the multy array is defined in a static way.

Does somebody know why?

eval_cell should get values defined in div_int

float f_imp(float x, float y){
     return pow(x,2)+pow(y,2)-1;
}


int eval_cell(float* p){
    int s[4];
    s[0] = f_imp(*p, *(p+1)) <= 0;
    printf("%f %f\n",*p, *(p+1));

    s[1] = f_imp(*(p+3), *(p+4)) <= 0;
    printf("%f %f\n",*(p+3), *(p+4));

    s[2] = f_imp(*(p+9), *(p+10)) <= 0;
    printf("%f %f\n",*(p+9), *(p+10));

    s[3] = f_imp(*(p+6), *(p+7))  <= 0;
    printf("%f %f\n",*(p+6), *(p+7));

    printf("%d%d%d%d\n",s[0],s[1],s[2],s[3]);
    return s[0];
}

void div_int(float* x1, float* y1,float* x2,float* y2,
             float* f0, float* f2,float* f6,float* f8){
    int i,j,m;
    float* p;
    float** a_cell; // array 9x3 contente coordinate vertici e valore funzione
    *a_cell = (float**) malloc(9*sizeof(float*));
    for (i=0;i<9;i++){
       a_cell[i] = (float*) malloc(3*sizeof(float));
    }
    a_cell[0][0] = *x1;
    a_cell[0][1] = *y1;
    a_cell[0][2] = *f0;
    a_cell[2][0] = *x2;
    a_cell[2][1] = *y1;
    a_cell[2][2] = *f2;
    a_cell[6][0] = *x1;
    a_cell[6][1] = *y2;
    a_cell[6][2] = *f6;
    a_cell[8][0] = *x2;
    a_cell[8][1] = *y2;
    a_cell[8][2] = *f8;
/***   calcolo dei valori incogniti di a_cell            ***/
    a_cell[1][0] = (*x1+*x2)/2;
    a_cell[1][1] = *y1;
    a_cell[1][2] = f_imp(a_cell[1][0], a_cell[1][1]);
    a_cell[3][0] = *x1;
    a_cell[3][1] = (*y1+*y2)/2;
    a_cell[3][2] = f_imp(a_cell[3][0], a_cell[3][1]);;
    a_cell[4][0] = (*x2+*x1)/2;
    a_cell[4][1] = (*y2+*y1)/2;
    a_cell[4][2] = f_imp(a_cell[4][0], a_cell[4][1]);
    a_cell[5][0] = *x2;
    a_cell[5][1] = (*y2+*y1)/2;
    a_cell[5][2] = f_imp(a_cell[5][0], a_cell[5][1]);
    a_cell[7][0] = (*x1+*x2)/2;
    a_cell[7][1] = *y2;
    a_cell[7][2] = f_imp(a_cell[7][0], a_cell[7][1]);

    for (j=0;j<2;j++){
       m = j*3;
       for(i=0;i<2;i++){
       m += i;
       eval_cell(&a_cell[m][0]);
       }
    }
    p = *a_cell;
    for (i=0;i<9;i++){
        for (j=0;j<3;j++){
          printf("%f \n",*(p+3*i+j));
          printf("%f \n",a_cell[i][j]);
      printf("\n");
       }
    }
   free(a_cell);
   return;
}
2
  • 2
    Ci serve il codice ;) (show us your code please) Commented Nov 14, 2011 at 13:34
  • Please, specify a source code. I guess it's because you using pointer in incorrect way, by just use your pointer as pointer to fload, while it's not a pointer to a float, but pointer to pointer ... to pointer to a float. See my answer on stackoverflow.com/questions/8122225/… Commented Nov 14, 2011 at 13:36

2 Answers 2

2

It's because you using pointer in incorrect way: See a_cell is pointer to dynamic array of 9 pointers to dynamic array of 3 floats. So when you do eval_cell(&a_cell[m][0]) (or just eval_cell(a_cell[m]) this is actually the same) you actually get pointer to array of 3 floats. And after that you do:

int eval_cell(float* p){

...
s[2] = f_imp(*(p+9), *(p+10)) <= 0;

*(p+9) will get 9th element in array of 3 floats, so this is incorrect.

It works in static way, because static multi dimension array in memory is just one dimension array for which you was given multi indexing (by compiler). That's why in static you will probably address valid memory area.

See picture for more explanation: enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

So a dynamic multi dimensional array is not allocated as one single array. I didn't know that. Very useful. Thanks.
Of course. Btw, you actually allocate it yourself using malloc, and in most cases malloc will not give you consecutive memory blocks.
:) Accepted your answer. Thanks.
0

If you want a completely dynamic matrix (2d array), you have to make your own element access function:

double *
make_array (unsigned int rows, unsigned int cols)
{
  return malloc (rows * cols * sizeof (double));
}

double *
array_element (double *a, unsigned int cols, unsigned int i, unsigned int j)
{
  return a + i * cols + j;
}

#define A(i,j) (*array_element ((a), (cols), (i), (j)))

double *a;
unsigned int rows, cols;

a = make_array (rows, cols);
A(3,4) = 3.14;
printf ("%f\n:" A(3,4));

EDIT:

In your program

*a_cell = (float**) malloc(9*sizeof(float*));

should be

a_cell = (float**) malloc(9*sizeof(float*));

And likewise for

p = *a_cell;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.