Notifications
Clear all

Don't put this on the Stack? Well, ummm...  

Page 2 / 2
  RSS

starnovice
(@starnovice)
Estimable Member
Joined: 1 year ago
Posts: 169
2020-05-08 3:16 pm  

@frogandtoad So just reviewed by reference and by ptr, in the c++ manual.  Apparently it is a compiler convenience to pass by reference but at the end of the day it is still passed by ptr. It is just that by reference allows the compiler to have a clearer understanding and to do addition checks on the validity of the code (like not being able to pass a NULL ptr by mistake).  I do agree that in c++ one should use by reference whenever possible.

Pat Wicker (Portland, OR, USA)


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2020-05-08 3:17 pm  

@starnovice

Posted by: @starnovice

OK, I'm confused, I always thought by reference was just a fancy way of saying "by ptr".  Can you clarify the difference?

Sure:

void passByPointer(int* array) {
 }
 
void passByreference(int (&array)[10]) {
 }
 
void setup() {
  Serial.begin(9600);
  int myArray1[10];
  int myArray2[20];
  
  passByPointer(myArray1);
  passByreference(myArray1);

  passByPointer(myArray2);
  
  // Will not compile - Reference attempt to bind to incorrect size
  //passByreference(myArray2);
 }

Both functions can directly manipulate the array, but the C++ with reference is safer because it won't allow you to bind at compile time to a different size arry, stopping you from just passing in old any pointer by value, of which the content it points to can still be manipulated.


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2020-05-08 3:26 pm  
Posted by: @starnovice

@frogandtoad So just reviewed by reference and by ptr, in the c++ manual.  Apparently it is a compiler convenience to pass by reference but at the end of the day it is still passed by ptr. It is just that by reference allows the compiler to have a clearer understanding and to do addition checks on the validity of the code (like not being able to pass a NULL ptr by mistake).  I do agree that in c++ one should use by reference whenever possible.

I don't know of any manual that says "pass by pointer".  Although I understand what is trying to be conveyed, but a pointer is passed by value in C, or by value or reference in C++... there really is a difference in both functionality and terminology, as they are not the same thing and should not be assumed to be so.


ReplyQuote
triform
(@triform)
Reputable Member
Joined: 1 year ago
Posts: 331
2020-05-08 6:08 pm  

@frogandtoad

You are correct as well.

And again, more noise.  Right or wrong un-necessary noise that was not needed for the original posters question.  You apparently are one of the ones that have to be right, no matter what, and make it know.  Less is more sometimes.  EOC.  


ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 6 months ago
Posts: 46
2020-05-09 8:13 pm  

@triform

I always take the examples and rewrite them because I have a different idea of what outputs (reports on the monitor) should be.

I also //comment what they haven't and fill in the blanks. I HATE UNDOCUMENTED CODE!!! #smh 


triform liked
ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 6 months ago
Posts: 46
2020-05-09 8:25 pm  

@frogandtoad

Thank you kindly for that info.  I've once seen someone write and implement their own version of the loop() function using a C programming int main {} structure, but never knew how they did what they did, and never looked further into it. I have so much to learn.

C++ and Arduino are NOT my strongholds by any measure...at those I suck. I do enjoy digging into bit-level pushin' and poppin' using C and Assembler, though. Me likes.

I'm good on the F macro for strings in flash memory. Another handy little trick.

Nice to meet you, frogandtoad!


ReplyQuote
triform
(@triform)
Reputable Member
Joined: 1 year ago
Posts: 331
2020-05-09 8:52 pm  

@itscaseydambit

I'm actually doing a Wiring type library for the 16F Pic's now out of boredom. Making it very light and just for fun really. I will probably skip the setup and loop functions 🙂 I have a blink program working now (pinMode and digitalWrite) and will do Digi read and the analog read soon.  I can hear engineers crying and laughing all over the world now! 

Scott

  


ReplyQuote
starnovice
(@starnovice)
Estimable Member
Joined: 1 year ago
Posts: 169
2020-05-09 9:12 pm  

@frogandtoad I'm pretty sure you would find it to be the same thing in the assembly language.  By reference just gives the compiler some clues for what kind of mistakes to prevent.

Pat Wicker (Portland, OR, USA)


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2020-05-12 7:44 am  

@starnovice

Sorry, but I'm not exactly sure what you're referring to when you say "it" would be the same thing in the assembly language?

 


ReplyQuote
Page 2 / 2