Poul K. Sørensen
Poul K. Sørensen

Reputation: 17560

How is the memory layed out in Eigen?

I have written this little parser that reads in some numbers from text file.

    data.resize(7,datapoints); //Eigen::Matrix<float,7,-1> & data
    dst = data.data();

    while( fgets(buf,255,fp) != 0 && i/7 < datapoints)
    {

        int n = sscanf(buf,"%f \t%f \t%f \t%f \t%f \t%f \t%f",dst+i++, dst+i++,dst+i++,dst+i++,dst+i++,dst+i++,dst+i++);
            i = i - 7 * (n<=0);
    }
    fclose(fp);
    return !(datapoints == i/7);

The thing is, when i do a std::cout on the data it flipped.

Data in:

0   4   0.35763609  0.64077979  0   0   1
0   4   0.36267641  0.68243247  1   0   2
0   4   0.37477320  0.72945964  2   1   3

data.col(3) is

0.64077979  
0.68243247  
0.72945964 

and data.col(4) is

0.35763609  
0.36267641  
0.37477320 

I cant see the logic why it have flipped the data horizontal?

Upvotes: 0

Views: 136

Answers (2)

Michael Wild
Michael Wild

Reputation: 26381

To illustrate the problem:

#include <cstdio>

void f(int i, int j, int k)
{
  printf("i = %d\tj = %d\tk = %d\n", i, j, k);
}

int main()
{
  int i=0;
  f(i++, i++, i++);
}

Executing this, returns here (g++ 4.3.4 on Cygwin):

i = 2   j = 1   k = 0

The execution order of the i++ calls inside the function call is entirely implementation defined (i.e. arbitrary).

Upvotes: 6

qPCR4vir
qPCR4vir

Reputation: 3571

Are you sure than?

int i=0;
sscanf(buf,"%f \t%f \t%f \t%f \t%f \t%f \t%f",dst+i++, dst+i++,dst+i++,dst+i++,dst+i++,dst+i++,dst+i++);

is equal to:

sscanf(buf,"%f \t%f \t%f \t%f \t%f \t%f \t%f",dst+0,dst+1,dst+2,dst+3,dst+4,dst+5,dst+6 );

I think the variable list arg is geting evaluated back in this case, and as @Christian Rau comment in general is Undefined the order of evaluation. Oft it is not a good idea to realy on side -effect order.

Upvotes: 3

Related Questions