Pages: [1]   Go Down
Author Topic: Passing a File object as a function parameter  (Read 220 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
Don't eat yellow snow.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Let's say I have a function:
Code:
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:

Code:
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:

Code:
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
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
Don't eat yellow snow.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, thanks guys. This really helps  smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
Don't eat yellow snow.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
Don't eat yellow snow.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

kwltnx
Logged

Pages: [1]   Go Up
Jump to: