djhanove / autorally

Software for the AutoRally platform
http://autorally.github.io
0 stars 0 forks source link

Avoid Raw Pointers #6

Open juicedatom opened 4 years ago

juicedatom commented 4 years ago

You should be avoiding raw pointers at all cost. Use a smart pointer equivalent.

https://github.com/djhanove/autorally/blob/b09af86ebc8b6a8b934a53a1532bee135e0bb31d/autorally_control/src/LTVMPC/LTVMPC.h#L47

djhanove commented 4 years ago

In this case, I am using a raw pointer to unpack the data stored in the Eigen matrix via:

x_out_ptr = x0.data(); // get address to object via Eigen built-in function
for(size_t i = 0; i < m_nX; i++) {  
    params.x_0[i] = *(x_out_ptr + i); // loop through matrix dimensions, and unpack into flat C-array for CVXGEN solver
}

Where x0.data returns a pointer to the first element of the Eigen matrix. Is the proper way to do this to instantiate the Eigen object within a shared pointer right from the beginning?

djhanove commented 4 years ago

Nevermind, the extra raw pointer is redundant. Can just dereference the pointer returned from the Eigen.data() call directly and let Eigen handle everything.


for(size_t i = 0; i < m_nX; i++) {  
    params.x_0[i] = *(x0.data()+ i); // loop through matrix dimensions, and unpack into flat C-array for CVXGEN solver
}