82762

Pointer vs Reference difference when passing Eigen objects as arguments

If a have a function that takes a Eigen matrix as an argument, what would be the difference between:

void foo(Eigen::MatrixXd& container){ for(i=0;i<container.rows();i++){ for(j=0;j<container.cols();j++){ container(i,j)=47; } } }

and

void foo(Eigen::MatrixXd* container){ for(i=0;i<container->rows();i++){ for(j=0;j<container->cols();j++){ container->coeffRef(i,j)=47; } } }

In Eigen documentation, they only present the first method - does that mean that there are any advantages to that approach? And what are the drawbacks of not using const when passing the Matrix reference in the first case?

Answer1:

References are nice because there is no such thing as a null reference, so using a reference parameter reduces the risk of someone calling your function with an invalid value.

On the other hand some coding standards recommend making parameters you intend to modify pointers instead of non-const references. This forces the caller to explicitly take the address of any value they pass in making it more obvious the value will be modified. The choice of pointer vs. non-const reference is up to you.

However, if you do not intend to modify the parameter then making it a const reference is definitely the way to go. It avoids the problem of passing invalid pointers, allows you to pass in temporaries, and the caller doesn't care if the parameter is taken by reference since it isn't going to be modified.

Answer2:

With C++ code, there is the expectation that if a parameter is passed as pointer rather than reference, then the null pointer is a valid argument.

That is, by default you should use reference parameters. Only use pointers if the parameter is in some way "optional" and you want the caller to be able to pass the null pointer to signify "no value".

Answer3:

see the line:

container(i,j)=47.

That's not a constant operation, so you're not going to be able to set it to const.

One way a reference is different than a pointer is that your container reference can't be null. Pass by reference is a good way to avoid some errors while getting the benefits of not copying.

Recommend

  • Can any reasonable CPU implementation give foo = 2 in this case?
  • Is there a way to psexec into my own virtual machine (windows xp mode)?
  • How to reserve IP when provisioning Azure VM (resource manager)
  • get ethernet name, class, description vendor, subsystem, etc using c/c++ on linux based systems
  • Git pushing to non-bare repo, save current worktree
  • difference between keeping database in app_data and connecting to an external database
  • What is the best way to include data in an HTML page?
  • What is the advantage of the ASP.NET precompilation?
  • Avoiding virtual functions on embedded target
  • Does anyone have recommendations for a good (and lightweight!) PHP mailer library?
  • Are there any advantages to using the VB.NET Like Operator vs a RegEx?
  • storing drop-down values, what is a good approach
  • What is Closure Compiler?
  • 3D Math: Calculate Bank (Roll) angle from Look and Up orthogonal vectors
  • LNK1104: cannot open file 'kernel32.lib'
  • How do I check if System::Collections:ArrayList is empty / nullptr / null?
  • Creating a C++ function that calls other Lua function
  • Not able to display correct data in table -AngularJS
  • Can my PDF ping my server when it is opened?
  • How to use function wrapper in mustache.php?
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • quiver not drawing arrows just lots of blue, matlab
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • What is the purpose of TaskExecutor in spring?
  • C: Incompatible pointer type initializing
  • Create DicomImage from scratch using Dcmtk
  • How to do unit test for HttpContext.Current.Server.MapPath
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Statically linking a C++ library to a C# process using CLI or any other way
  • How to make Safari send if-modified-since header?
  • How do I use the BLAS library provided by MATLAB?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How to pass list parameters for each object using Spring MVC?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • How does Linux kernel interrupt the application?
  • JaxB to read class hierarchy