Should I prefer functions that transform data instead of creating data? [closed]

asked 2015-09-02 07:12:03 -0600

chr0x gravatar image

I noticed in many functions from OpenCV library (C++ API) that there's a pattern regarding to the parameters passed to a function. I see that almost always there's a &src Mat and a &dst Mat that is the result (the data modified) being the return type of that function a void type. When I came from java to start in C++ I was writing all of my functions with a return type instead of the &dst Mat idea. Something like this:

cv::Mat doStuff(cv::Mat &src) {

  cv::Mat modifiedStuff;
  ... // do stuff
  return modifiedStuff;

I think that way sometimes make things more clear, I understand that I'm generating an extra matrix inside the method everytime, and it can be bad. However, I'm curious how much bad it could be. Sometimes for example, I'm using the return concept instead of the &dst concept coz I need that the matrix have a specific proportion for example, so I have to create it inside the method to be sure that method will always work with that proportion.

cv::Mat doStuffWith1x132Matrix(cv::Mat &src) {

  cv::Mat modifiedStuff(1,132,CV_64F); 
  ... // do stuff 
  return modifiedStuff;

There's some consense in the community regarding to where should I use the &dst matrix idea instead of returning a new matrix (created inside the method) or should I take it as a question of preferences?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by chr0x
close date 2015-09-03 22:08:14.170708

Comments

OpenCV is software that runs on many platforms. If you are running stuff on mobile devices and embedded platforms you want to avoid creating abundant memory consuming structures as much as possible. That is the reason why it is being done this way in OpenCV. For a normal pc it indeed does not matter one big thing.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-09-02 08:40:59 -0600 )edit