Passing a File object as a function parameter

Hi

Let's say I have a function:

void doSomething( File file )

What exactly the function does, is not so important, but we may assume that the file object will be used to write data to an SD card, and will again be closed within the function. Basically:

void doSomething( File file )
{
  file = SD.open("FILE.TXT", FILE_WRITE);
  file.print(); // arbitrary data is added
  file.close();
}

Is the file object passed by value or by reference? I am worried about this because I want to avoid memory issues later on. If I can reuse one file object by reference, it is a great plus if I call doSomething( File file ) from a function that already contains a file object, ie:

void bigFunction()
{
  File file;
  file = SD.open("name.txt", FILE_READ);
  file.read(); // arbitrary file operation
  file.close();
  // We are done with file, but it remains in the memory.
  doSomething( file ); // Thus, reusing the same object makes sense.
}

If a copy of the file object is sent as a parameter (aka call by value), I would have another instance of the object sitting in the memory unnecessarily.

If it is sent by value, a little help adapting the syntax to pass it by reference would be appreciated.

I have been googling this, but most of the stuff I read left me uncertain. I also tried some of the RAM usage measurement functions, but their results reported no change in memory usage between the the different File and SD operations, so they also left me confused, as I expected a rise in memory usage with the initialization of the file objects.

To the slightly more experienced C++ programmers out there, this is hopefully a no-brainer.

Thanks

"File" is an object class. Classes (like structures) are passed by reference/address.

UNLESS there is a copy constructor in which case they may be passed by value ... at least according to the doc's.

You could make file global and save passing it at all or you could force the pointer with File* file.

Mark

Ok, thanks guys. This really helps :slight_smile:

Just for interests' sake and future reference: Mark, if one were to force the pointer, how would the second block of code in my original post look?

I think it should be the same the default is after all pass by ref.

Mark

kwltnx