Go Down

Topic: Tutorial on the basics of using the "buffer" instruction? (Read 2 times) previous topic - next topic

Zoandar


Quote
... this rather cryptic instruction is setting ALL THREE of the array elements to ZERO in ONE STATEMENT. Correct?


Yes, setting the entire array to zero. More specifically, everything beyond the stated initializers. So to be pedantic, you requested the first position to be initialized to zero, and the compiler initialized the rest to zero for you, as you have stated that the array is to be initialized in the first place (by using "= ...").



However, it also seems that he could just as simply used
Code: [Select]


int buffer_pos[NUM_AXES] ;  // start with all zero

OR

int buffer_pos[NUM_AXES] = {};  // start with all zero




Your first example won't work with "auto" variables. That is, if they are defined inside a function. So it is dangerous to do that, because you might copy and paste them (from a statically defined place to inside a function). Also it relies upon you remembering that static variables are automatically initialized.

Your second example works but personally I think it is a bit cryptic.

I prefer:

Code: [Select]
int buffer_pos[NUM_AXES] = { 0 };  // start with all zero




Having read this reply and that of PaulS, I agree your preference leaves the best 'bread crumbs', which I usually find to my liking. :)

Thanks!
Zoandar
  ---v/\/\/\v---

Zoandar


Quote
Your second example works but personally I think it is a bit cryptic.

I prefer:

Code: [Select]
int buffer_pos[NUM_AXES] = { 0 };  // start with all zero

The important thing to keep in mind here is still that the number of explicitly valued positions in the array is limited to the number of explicit initializers provided. The rest of the positions in the array may or may not be valued with 0.

Prove this to yourself by changing where this statement is positioned (global, in a function, with and without the static keyword, etc.) and change the initial value to something other than 0, like 37. Then, use a for loop to print the index and value for each position in the array.

When the number of explicit initializers is less than the size of the array, the array values will NOT all be whatever (single) initializer is provided.


Being a beginner, I find that when one relies on something happening 'automatically' and, worse, with little or no understanding of the same, it opens the door to the possibility of long and frustrating sessions trying to figure out why some end result isn't happening. Black box technology is, for me, a frustrating thing which is never fully trusted or understood. This happens to be the actual motivation I had toward Arduino. The first description of it I read spelled it out as a better alternative to a beginner approach in the world of electronics, as opposed to attemtping to learn enough about the world of discrete component interaction to be able to design and build in a hobby environment. Having traveled both paths, I find this to be quite true, although the programming involved 'can' be just as steep a learning curve, depending mostly upon the teacher being pursued. :) 

That said, I wanted to learn enough about this particular issue so that I can instantly recognize the next time I see it. And knowing what I do now, thanks to you good folks, I see his use of { 0 } as likely the best choice considering the other options just discussed. I did read in that array initialization article about the cautions you presented. Given that this sketch has only a 3 element array in this scope, might it not have been better overall to just use { 0, 0, 0 }, which I would instantly have understood?

Still, learning about using the line he did was a worthwhile lesson.

Thanks! 
Zoandar
  ---v/\/\/\v---

PaulS

Quote
Given that this sketch has only a 3 element array in this scope, might it not have been better overall to just use { 0, 0, 0 }, which I would instantly have understood?

Yes. Explicitly providing the wrong number of initializers is not a good practice.

Zoandar

Thanks very much for helping clarify this. 24 hours ago I had no clue what that line of code meant. Now I understand it well. I was thinking this morning that back in the 70s before we had the internet, when I first took an interest in computers, and back in the 60s, when I first took an interest in electronics, there was no one to turn to for help when something did not work, was misunderstood, or worse, when a book or manual was incorrect. The internet and these forums are such invaluable tools today! I would hate to go back to the time before we had them!

But even so, they are only paths of information. Without the people using them who care to share their knowledge freely, and care enough to make sure the person they are helping understands the information provided, they are just "paths". So it is the people behind them who really make this a better world when it comes to learning things on one's own. I thank you, and all other such people here, who take time from their day to provide this knowledge and help!

Have a great day!
Zoandar
  ---v/\/\/\v---

Nick Gammon


Code: [Select]
int buffer_pos[NUM_AXES] = { 0 };  // start with all zero

The important thing to keep in mind here is still that the number of explicitly valued positions in the array is limited to the number of explicit initializers provided. The rest of the positions in the array may or may not be valued with 0.


I can't agree with you here Paul. The rest of the positions in the array are valued with zero. Check out this page:

http://stackoverflow.com/questions/1920430/c-array-initialization

You can also see that from this sketch:

Code: [Select]
void setup ()
{
Serial.begin (115200);
int foo [10] = { 0 };

for (int i = 0; i < 10; i++)
  Serial.println (foo [i]);
}

void loop () {}


Not only does it print 10 lots of zero, but a dissassembly shows it is moving in 20 bytes of zero to the array (not 2 bytes):

Code: [Select]
int foo [10] = { 0 };
  ec: fe 01        movw r30, r28
  ee: 31 96        adiw r30, 0x01 ; 1
  f0: 84 e1        ldi r24, 0x14 ; 20
  f2: df 01        movw r26, r30
  f4: 1d 92        st X+, r1
  f6: 8a 95        dec r24
  f8: e9 f7        brne .-6      ; 0xf4 <setup+0x34>



When the number of explicit initializers is less than the size of the array, the array values will NOT all be whatever (single) initializer is provided.


That's correct. They will be valued with zero. And of course if the (single) initializer is zero, then they happen to be valued with it as well. But it doesn't copy that initializer into the other places.

Go Up