Pages: [1] 2   Go Down
Author Topic: What does 'returns an instance' mean ?  (Read 1186 times)
0 Members and 1 Guest are viewing this topic.
Wales
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm working my way through a book on Arduino programming and I'm kind of stuck at a bit about IF loops.  The code is for writing data to an SD card, and there is a line earlier on that says 'File file;'.  I'm guessing the command 'File' is a function of the SD.h library ?  But what does that command do ?  The book skims over it.

Later on, there is an IF loop that starts with;

Code:
file = SD.open("data.txt", FILE_WRITE);
if (file) {
while (millis() < (runningTime * msMin)) {
byte value = map(analogRead(sensor), sensorMin, sensorMax, 0, 255);
file.write(value);
delay(interval);
}

What I don't get is the line " if (file) { ".
If file what ?  I know it means "If file (is true)" - but that isn't much help !  I'm used to seeing something like "If (a == 5)".

I have a feeling my confusion is rooted in the phrase 'returns / creates an instance' and I'm testing for that with the 'if (file)' statement.... but I'm not sure !

Any pointers would be greatly appreciated !

Thanks !
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If file what ?

If file is true. True means non zero. If SD.open is successful, a non-zero value is returned. If its not, a 0 is returned which evaluates to false.

Code:
if (18)

always runs the code in the if block because 18 is non zero.

Code:
if (0)

never runs the code in the if block
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm kind of stuck at a bit about IF loops.

That's hardly surprising since there is no such thing as an IF loop.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

Quote
If file is true.

No, as file is an instance of File, it should be read as file is not null.

Mark
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If file is true.

No, as file is an instance of File, it should be read as file is not null.

Mark

Depends on if you're looking at it from the perspective of the File instance or the if statement
Logged

Cincinnati, OH
Online Online
God Member
*****
Karma: 42
Posts: 691
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ive added some comments to your code in an attempt to make it more clear:

Code:
file = SD.open("data.txt", FILE_WRITE);     // Try to open the file named data.txt in the current directory. If the attempt fails (i.e., file doesn't exist or just can't open it)
                                                          // return 0, otherwise return a file handle

if (file) {                          // This is the same as: if (file != 0), which means if we successfully opened the file...

  while (millis() < (runningTime * msMin)) {           // Good practice to indent if and while statements...
      byte value = map(analogRead(sensor), sensorMin, sensorMax, 0, 255);  // read a sensor value as a byte value...
      file.write(value);                      // Write the value to the data.txt file...
      delay(interval);                        // delay processing for a while...
   }                                              // end of while loop statement block
}                                                 // end if statement block

Actually, "returns an instance" is an Object Oriented Programming idiom that usually refers to an object that was defined for a specific (C++) class. Right now, you might just think of it as a variable (e.g., the file variable in your code) that is either usable (i.e., its value is non-zero) or not usable (i.e., its value is zero).
Logged

Cincinnati, OH
Online Online
God Member
*****
Karma: 42
Posts: 691
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To everyone:

In the code fragment:

Code:
 
   while (millis() < (runningTime * msMin)) {           
      byte value = map(analogRead(sensor), sensorMin, sensorMax, 0, 255); 
      file.write(value);                     
      delay(interval);                       
   }

How is the lvalue for value determined? That is, does value get a new (reallocated) memory address on each pass through the loop, or is just one allocation done regardless of the number of passes through the loop. (I'm away from home and don't have access to my development system.)
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How is the lvalue for value determined? That is, does value get a new (reallocated) memory address on each pass through the loop, or is just one allocation done regardless of the number of passes through the loop. (I'm away from home and don't have access to my development system.)

The value is redefined each time the loop is executed.

Whether the compiler actually allocates and releases the memory at the start and end of each loop, and whether the memory happens to be in the same place and happens still to contain the previous value, is a matter for the compiler's code generation algorithm. As far as you and I are concerned, the variable comes into existence when execution reaches the statement that defines it, and goes out of existence when execution reaches the end of the body of the for loop.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Wales
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks to all, esp Econjack.  I understand what it's doing now, though I'm still a little hazy on the 'instance' thing !  The last time I did any coding was in college, many (too many) decades ago, and that was in Borland 'Turbo' Pascal.  smiley
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What I don't get is the line " if (file) { ".
If file what ?  I know it means "If file (is true)" - but that isn't much help !  I'm used to seeing something like "If (a == 5)".

I have a feeling my confusion is rooted in the phrase 'returns / creates an instance' and I'm testing for that with the 'if (file)' statement.... but I'm not sure !

Moving onto facts ...

File is a class, and SD.open returns an instance of that class. For example, if dog is a class, Fido is an instance of the class dog.

If file is true. True means non zero. If SD.open is successful, a non-zero value is returned. If its not, a 0 is returned which evaluates to false.

This is not correct. It returns an instance of File. The instance always exists and is not zero. It is not any number.

No, as file is an instance of File, it should be read as file is not null.

Nor is it NULL or not NULL (it is not a pointer at all).



The answer is here:

Code:
class File : public Stream {
...

  operator bool();
...
};

The class File implements operator bool(). That means if you attempt to treat (an instance of) File as a boolean, for example:

Code:
if (file) ...

Then the operator bool() function is called.

This function is implemented thus (in the standard SD library):

Code:
File::operator bool() {
  if (_file)
    return  _file->isOpen();
  return false;
}

So basically, if the file is successfully opened the private member variable _file is non-NULL. Otherwise it is NULL. The function tests for that (and if non-NULL if it is actually open) and returns true or false.
« Last Edit: June 21, 2013, 10:46:06 pm by Nick Gammon » Logged

Cincinnati, OH
Online Online
God Member
*****
Karma: 42
Posts: 691
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

PeterH:
The value is redefined each time the loop is executed.

That's my understanding, too. My company produced C compilers back in the early 80's and, even though we considered our compiler to generate pretty good code, we did not optimize the allocation away inside a loop like the one shown above. Our feeling was that someone might do this kind of thing for debugging purposes. (The keyword volatile probably was created for situations where there was a reason for such things.) That said, I don't understand why the writer would want to reallocate value on each iteration through the loop. It would be better to define value just before entering the loop.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It is perfectly legitimate to re-initialize a variable every time through a loop. And I'm not sure that volatile is meaningful with auto variables.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure that volatile is meaningful with auto variables.

I think it must be, since it's possible for an interrupt handler to modify the value.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think it must be, since it's possible for an interrupt handler to modify the value.
How? An auto variable is local to one function. The ISR is another function. The ISR should not have access to the auto variable.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How? An auto variable is local to one function. The ISR is another function. The ISR should not have access to the auto variable.

It occupies memory so it is capable of being modified by code in an interrupt handler. You don't need visibility of the symbol in order to modify its value.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1] 2   Go Up
Jump to: