FIRST Forums  

Go Back   FIRST Forums > FRC Archive > 2009 FRC Control System Public Forum > Programming Showcase

Reply
 
Thread Tools
  #11  
Old 10-24-2008, 08:08 PM
GabeRC1717 GabeRC1717 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 26
Default Re: Team 67 C++ Code

It took me the longest time to understand what the difference was between a pointer to an object and using the new keyword without a pointer. But it is fairly simple. In windows there is the stack and the heap. Pointers go on the heap and variables (objects that are not pointers included here) go on the stack. Stack variables expire after the function they are in returns, and that memory is not guaranteed to not be reused. Heap objects last forever, until they are deleted. For example:

Code:
void myFunction() {
     MyClass instanceOnStack = MyClass();
     MyClass *instanceOnHeap = new MyClass();
     return;
}
// instanceOnStack expires after the function returns
// instanceOnHeap lasts forever and that memory will remain untouched until the object is deleted
Hope this helps, and thanks team 67 for posting some C++!
__________________
----------
Gabe
grivescorbett@dpengineering.org
Team 1717, http://dpengineering.org
Reply With Quote
  #12  
Old 10-24-2008, 08:18 PM
Bomberofdoom Bomberofdoom is offline
Junior Member
 
Join Date: Sep 2008
Location: Israel
Posts: 13
Default Re: Team 67 C++ Code

Quote:
Originally Posted by GabeRC1717 View Post
It took me the longest time to understand what the difference was between a pointer to an object and using the new keyword without a pointer. But it is fairly simple. In windows there is the stack and the heap. Pointers go on the heap and variables (objects that are not pointers included here) go on the stack. Stack variables expire after the function they are in returns, and that memory is not guaranteed to not be reused. Heap objects last forever, until they are deleted. For example:

Code:
void myFunction() {
     MyClass instanceOnStack = MyClass();
     MyClass *instanceOnHeap = new MyClass();
     return;
}
// instanceOnStack expires after the function returns
// instanceOnHeap lasts forever and that memory will remain untouched until the object is deleted
Hope this helps, and thanks team 67 for posting some C++!


That made much more sense to why to use the pointer for objects. Thank you.

Just for an example, in order to use the Get functions, say PWM.Get() [returns the latest PWM value], you need to keep that info from the last call of teleop or auto function. Using the stack, will expire when the teleop/auto functions ends (when you reach the function's "}"). using the heap, they will stay untill the robot is shutdown (when I think about it, maybe there's also the use of an EEPROM to store info....Nope...If you think about it, for 2009 they add "Intiailize", "execute/default" and "close" functions for all robot modes (disabled, teleop, auto), therefore when you create your objects, when you close the robot's power, they will be deleted from the heap and created from scratch the next time the robot is powered and the CRio's proccessor goes through the initialize function of whatever robot mode).

Starting to get the hang of it.... I might consider rewriting some of the previous year's code to C++ soon (just writing a template not including the WPI libraries, but can be copied into the offical code when it comes out, and it should work.)


P.S
One more thing:

What about:
code:
Quote:
MyClass instanceOnwhat = MyClass();
__________________
Team 2230 Programming sub-team Leader. Non-Beta Tester.
C/C++ moderate programmer. Labview Programmer. FIRST ADDICT.

Last edited by Bomberofdoom; 10-24-2008 at 08:24 PM.
Reply With Quote
  #13  
Old 10-24-2008, 08:23 PM
GabeRC1717 GabeRC1717 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 26
Default Re: Team 67 C++ Code

Quote:
Originally Posted by Bomberofdoom View Post
That made much more sense to why to use the pointer for objects. Thank you.

Just for an example, in order to use the Get functions, say PWM.Get() [returns the latest PWM value], you need to keep that info from the last call of teleop or auto function. Using the stack, will expire when the teleop/auto functions ends (when you reach the function's "}"). using the heap, they will stay untill the robot is shutdown (when I think about it, maybe there's also the use of an EEPROM to store info....Nope...If you think about it, for 2009 they add "Intiailize", "execute/default" and "close" functions for all robot modes (disabled, teleop, auto), therefore when you create your objects, when you close the robot's power, they will be deleted from the heap and created from scratch the next time the robot is powered and the CRio's proccessor goes through the initialize function of whatever robot mode).
Basically yes. RAM is volatile memory so those objects will be deleted when the robot shuts down. In windows however, it is very important to delete pointers like so:

Code:
MyClass *instance = new Myclass;

//Do some stuff, program is about to exit

delete instance;
If you don't that object will remain in memory until the computer reboots, this isn't an issue on robots, but it is generally considered to be good form when writing C++. Glad I could help clarify.
__________________
----------
Gabe
grivescorbett@dpengineering.org
Team 1717, http://dpengineering.org
Reply With Quote
  #14  
Old 10-24-2008, 08:26 PM
Bomberofdoom Bomberofdoom is offline
Junior Member
 
Join Date: Sep 2008
Location: Israel
Posts: 13
Default Re: Team 67 C++ Code

Quote:
Originally Posted by GabeRC1717 View Post
Basically yes. RAM is volatile memory so those objects will be deleted when the robot shuts down. In windows however, it is very important to delete pointers like so:

Code:
MyClass *instance = new Myclass;

//Do some stuff, program is about to exit

delete instance;
If you don't that object will remain in memory until the computer reboots, this isn't an issue on robots, but it is generally considered to be good form when writing C++. Glad I could help clarify.
Yeah....the diffrence between a computer and a robot....
This really brings me back to the days when I started my first year of FIRST, trying to understand writing C for a ROBOT. Took me about 2-3 weeks to understand it all. And that was on the Build Season's time. Good thing we're gathering info before the kickoff.

P.S

I wasn't thinking about the fact the the RAM in a robot is volatile, I was thinking more of because of the Close functions assigned for each robot mode, the objects get deleted. Or atleast you could use it to do the delets there and be more....safe? :S
__________________
Team 2230 Programming sub-team Leader. Non-Beta Tester.
C/C++ moderate programmer. Labview Programmer. FIRST ADDICT.

Last edited by Bomberofdoom; 10-24-2008 at 08:29 PM.
Reply With Quote
  #15  
Old 10-24-2008, 08:39 PM
GabeRC1717 GabeRC1717 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 26
Default Re: Team 67 C++ Code

Also I have noticed that the body of all of the functions are defined right in the class definition. Instead of having the class definition in a header file and the function bodies in a cpp file. Is there are particular rhyme or reason for this?
__________________
----------
Gabe
grivescorbett@dpengineering.org
Team 1717, http://dpengineering.org
Reply With Quote
  #16  
Old 10-25-2008, 08:05 AM
swaldo
Guest
 
Posts: n/a
Default Re: Team 67 C++ Code

Quote:
Originally Posted by GabeRC1717 View Post
Also I have noticed that the body of all of the functions are defined right in the class definition. Instead of having the class definition in a header file and the function bodies in a cpp file. Is there are particular rhyme or reason for this?
The sample code was provided that way. It was simple to just expand on the samples. I agree, it's a little unconventional.
Reply With Quote
  #17  
Old 10-27-2008, 06:14 PM
Dave Doerr Dave Doerr is offline
Member
 
Join Date: Oct 2008
Location: Milford MI USA
Posts: 32
Default Re: Team 67 C++ Code

Quote:
Originally Posted by Hjelstrom View Post
How is the debugger support? Can you step through code running on the cRio? Can you inspect the values of variables? Print to a debug window on the driver station?
Greg,

Wind River does have debugger support, and we've tried using it, but we haven't made much progress learning how. However, note that I have almost zero experience using a debugger. I recommend downloading the WR evaluation and spending time with it.

You can step through running code and inspect the values of variables. You can't yet display user information on the driver station or dashboard (using C++), but both of those have been promised at a later date (actual date TBD).

So far, the method we rely on for debugging is the old standard, "printf" to the console. There are several methods to view console output. You can view output on two different WR consoles over your laptop ethernet connection -- wired or wireless. Also, the cRIO has a serial port to which you can attach a terminal (your laptop or another laptop).

The method I like best is the bluetooth serial adapter that we've just begun using. It plugs into the cRIO serial port and can link to your bluetooth laptop. We have a "Firefly" from Spark Fun Electronics. It's a class 1 device - if you have class 1 on your laptop that will give you a 300 meter-long serial "cable".

Dave D
__________________
FRC: HOT, Team 67, Mentor, C++ Beta Test Lead
FLL: Dark Matter Dark Energy, Team 3069, Coach
Reply With Quote
  #18  
Old 10-28-2008, 04:50 PM
lilstogi11 lilstogi11 is offline
Junior Member
 
Join Date: Aug 2008
Location: Flander, NJ
Posts: 1
Default Re: Team 67 C++ Code

We're def using C++ this year. Thanks for posting your code.

Has anyone written anything in Python yet?? I really need to know how it works out. We've been dieing to use Python.
__________________
MORT Team 11 Captain
Reply With Quote
  #19  
Old 11-01-2008, 09:30 PM
GabeRC1717 GabeRC1717 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 26
Default Re: Team 67 C++ Code

Quote:
Originally Posted by Bomberofdoom View Post
MyClass instanceOnwhat = MyClass();
This would be on the stack. The new keyword is what causes a pointer to be returned instead of a variable. In fact, with stack variables, the constructor can be called implicitly:

Code:
MyClass instanceOnStack;
instanceOnStack is now a fully initialized MyClass on the stack, the constructor is called automatically.
__________________
----------
Gabe
grivescorbett@dpengineering.org
Team 1717, http://dpengineering.org
Reply With Quote
  #20  
Old 11-02-2008, 01:58 AM
Bomberofdoom Bomberofdoom is offline
Junior Member
 
Join Date: Sep 2008
Location: Israel
Posts: 13
Default Re: Team 67 C++ Code

Quote:
Originally Posted by GabeRC1717 View Post
This would be on the stack. The new keyword is what causes a pointer to be returned instead of a variable. In fact, with stack variables, the constructor can be called implicitly:

Code:
MyClass instanceOnStack;
instanceOnStack is now a fully initialized MyClass on the stack, the constructor is called automatically.
I'm sorry, it's been a while since I posted that post....
Don't really remember why I wrote that, and I don't see the logic in why I did that since you've allready wrote the same type of code and you said the the instance goes to the stack in that way.

So, I belive I made a mistake and wanted to ask what happens when I write:

Code:
MyClass InstanceonWHO = new MyClass();
__________________
Team 2230 Programming sub-team Leader. Non-Beta Tester.
C/C++ moderate programmer. Labview Programmer. FIRST ADDICT.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:35 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.