-2

2D array initialization:

....
int main (...) {
  ....
  double **hes = allocArray (2, 2);
  // function (....) returning double
  hes[0][0] = function (3, &_X, &_Y, _usrvar_a, _usrvar_b);
  hes[0][1] = function (4, &_X, &_Y, _usrvar_a, _usrvar_b);
  hes[1][0] = function (4, &_X, &_Y, _usrvar_a, _usrvar_b);
  hes[1][1] = function (5, &_X, &_Y, _usrvar_a, _usrvar_b);
  ....
  return 0;
 }
double **allocArray (int row, int col) {
  double **ptr = new double*[row];
  for (int i = 0; i < row; i++)
    {
      ptr[i] = new double[col];
    }
  return ptr;
 }

Values of 2d double type array is:

12 2 2 14

I know that because I have crossed it with iterators (i, j)

void otherFunc (double **h, ....) {
 ....
   for (int i = 0; i < 2; i++)
     for (int j = 0; j < 2; j++)
       std::cout << " " << h[i][j];
 ....
 }

Output is

12 2 2 14

(I do not need to separate the rows of 2D array in output, do not write about that)

I want to cross it with pointer:

void otherFunc (double **h, ....) {
 .... 
   for (double *ptr = h[0]; ptr <= &h[1][1]; ptr++)
     std::cout << " " << *ptr;
 ....
 }

Output is:

12 2 0 1.63042e-322 2 14

Why 0 and 1.63042e-322 appeared here?

djanoris
  • 11
  • 1
  • 6
    please provide [mcve] – CinCout Dec 10 '19 at 09:39
  • 2
    `12 2 2 14` this doesn't look like a 2D array to me. The structure of your data is unclear, if you post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) we are more likely to be able to help you. Generally the problem is most likely that your read is going out of bounds somewhere, probably bcause you're trying to traverse a 2D array with a 1D loop. – Blaze Dec 10 '19 at 09:41
  • @Blaze I can traverse 2D array with a 1 loop if I do it with pointer, because the values are stored consecutively in memory (first of all is the values of the first row, after this is the values of the second row. That's why the pointer 'ptr' is incremented. – djanoris Dec 10 '19 at 10:02
  • _"I can traverse 2D array with a 1 loop if I do it with pointer, because the values are stored consecutively in memory"_ that depends on the array. Without declaration we can't know. It's possible that the elements are not consecutively in memory. Independent of if it works it's undefined behavior: https://stackoverflow.com/questions/6015080/c-c-is-this-undefined-behavior-2d-arrays. See C++ Standard: http://eel.is/c++draft/expr.add#4 – Thomas Sablik Dec 10 '19 at 10:58
  • In your example `double *ptr = h[0]; ((ptr++)++)++;` causes undefined behavior. Also `double *ptr = h[0]; (ptr++)++; *ptr;` causes undefined behavior. – Thomas Sablik Dec 10 '19 at 11:06

1 Answers1

0

h[0] and h[1] in your run are not one just after the other: h[1] in your specific run happens to be four numbers after h[0].

This behavior probably is random meaning that (as far as we know from your question) probably you didn't explicitly specify the relative positions of h[0] and h[1]. If this is the case the next time you run your code h[1] could even be smaller than h[0] this results in undefined behavior.

What you probably want is something of this kind: allocate four doubles and assign the address of the first to a pointer double* hh = malloc(4 * sizeof(double)); And then for the variable h which is a pointer to pointer double* h[2]; you want to assign the pointers as follows:

h[0] = hh; 
h[1] = hh+2; 

of course there are safer ways to do this. But this could be a good start.

jimifiki
  • 5,377
  • 2
  • 34
  • 60