Moving to C++

Learning C++ is extremely helpful for embedded development on Arduino and ESP32. Our team uses an ESP32 to drive a display marquee on our robot. My advice is, if you want to learn it, dive in.

There are significant differences between C++ and Java, including:

  • C++ programmers have to delete objects created with new. Java programmers merely cut them loose for garbage collection. C++ now provides smart pointers that simplify life cycle management.
  • C++ is compiled into native code. Java compiles into pseudocode that is interpreted at runtime. As a result, C++ programs can run faster than their Java equivalents. C++ programs generally require less memory than equivalent Java because Java requires a heavyweight interpreter.
  • C++ supports multiple inheritance, Java mostly does not. Multiple inheritance is almighty dangerous. (Ask me how I know!) Default method implementations support mix-ins in Java. My advice is to avoid multiple inheritance in all forms.
  • C++ supports templates while Java supports generics. Template instantiation generates new classes while generic instantiation does not. Both the C++ std::vector template and the Java ArrayList generic implement lists. At runtime, std::vector is a totally different class from std::vector, while ArrayList and ArrayList compile to ArrayList.
  • C++ does not check array indices. Java does. Here be dragons.

Have fun!

1 Like

Overuse of smart pointers is also an antipattern in C++. Only use them if you need non-nested lifetimes. Nest lifetimes on the stack when possible because it’s less typing, less to think about lifetime-wise, and more efficient.

You should edit your post to wrap these in code blocks or backticks. It looks like CD interpretted what you wrote as HTML tags and omitted it from the post.

This is true for raw arrays, but not necessarily for STL containers. std::vector<T,Allocator>::at - cppreference.com and std::array<T,N>::at - cppreference.com throw an exception on out-of-bounds accesses.

2 Likes

Going great so far!
I am following this playlist and that australian C++ guy someone here suggested, and most(dynamic memory management is pain) of it doesn’t seem that hard!

I have copied the code from 3512 and will later mess around with it to get a better grasp of how wpilib and C++ interact.

I’m really starting to appreciate some java features, such as the array out of bounds exception and array lists.
Though I’ve seen Tyler suggest alternatives so I suppose every little inconvenient thing in C++ has a smart workaround…? lol
Guess I’ll learn about it eventually

When running odometry at 200Hz alongside heavy logging I’m seeing constant loop overruns.
Sometimes of only 22ms, but I’ve seen ups of 50ms

Not to mention I have to invoke the GC every few seconds or the program runs out of memory every other run, and that inevitably causes even worse loop overruns which cause the LEDs to freeze (!).

Seriously though, I’m worried that this could cause some problems when running control on the roborio.
I AM using RoboRio 1 though…

std::vector does pretty much everything ArrayList does, FWIW. C++ collections are inherently somewhat more complicated (iterators…), but the API really isn’t that different.

3 Likes

Actually, Java collections support iterators too via Iterable and Iterator, and both languages support range-based for. C++ implements range-based for in terms of iterators, and I wasn’t able to determine how Java implements it.

Every language has “gotchas”, but this reminds me of one of my favorite ones in Java. The enhanced for loop syntax (for-each) in Java is a really convenient way to avoid fiddling with an index or an Iterator, but the devil is in the details. Under the covers it actually allocates a new Iterator. Every for-each loop called during periodic is rapidly adding to the heap. For ordered collections, you can relieve memory pressure by using the index instead.

To me this sounds like you should spend some time investigating the memory usage with a profiler. There’s likely some relatively simple fixes if your memory usage is growing that much. That’s where I’d start rather then learning another programming language.

On the other hand, learning manual memory management will make you a better programmer in any language.

1 Like

I believe that’s his main reason for switching.
The memory problem is most likely a RoboRio 1.0 limit IMO. As he mentioned, he did try a lot of methods and they’ve worked okay.
I would switch to/buy a RoboRio 2.0 if possible.

1 Like

Iterators tend to be scalarized by the JIT to avoid heap allocations. They’re one of the few things that it will actually reliably scalarize

1 Like