Notifications
Clear all

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

24 Posts
4 Users
4 Likes
2,593 Views
(@starnovice)
Member
Joined: 5 years ago
Posts: 110
 

@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)
Member
Joined: 5 years ago
Posts: 1458
 

@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)
Member
Joined: 5 years ago
Posts: 1458
 
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)
Member
Joined: 5 years ago
Posts: 324
 

@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)
Member
Joined: 4 years ago
Posts: 40
Topic starter  

@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 reacted
ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Member
Joined: 4 years ago
Posts: 40
Topic starter  

@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!


   
frogandtoad reacted
ReplyQuote
triform
(@triform)
Member
Joined: 5 years ago
Posts: 324
 

@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)
Member
Joined: 5 years ago
Posts: 110
 

@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)
Member
Joined: 5 years ago
Posts: 1458
 

@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