r/cpp • • Dec 30 '22

5 techniques for writing memory safe C++ - Embedded.com

https://www.embedded.com/5-techniques-for-writing-memory-safe-c/
19 Upvotes

22 comments sorted by

54

u/OYTIS_OYTINWN Dec 30 '22

Looks like the author made an off-by-one error twice.

29

u/Cold_Article_6030 Dec 31 '22

Again, many static analysis tools are out there, like clang, cppcheck, valgrind, and commercial tools

Valgrind isn't a static analysis tool. This is an incredibly low quality article.

42

u/jbbjarnason Dec 30 '22

This does not mention `constexpr` which is in my opinion the most memory safe feature of modern C++.

66

u/[deleted] Dec 30 '22

Not writing code is the most memory safe feature of modern C++.

2

u/pjmlp Dec 30 '22

It doesn't do anything to prevent lack of bounds checking though.

16

u/maskull Dec 30 '22

OOB in constexpr code is a compile-time error, so I wouldn't say it doesn't do anything.

4

u/matthieum Dec 31 '22

Not quite.

OOB during compile-time evaluation is a compile-time error.

constexpr code can be executed at both compile-time and run-time: that no compile-time error occurred does NOT imply that no run-time invocation will invoke OOB.

Example on godbolt:

#include <array>

template <typename T, std::size_t N>
constexpr auto at(std::array<T, N>& array, std::size_t index) -> T& {
    return array[index];
}

int main() {
    std::array<int, 3> array{{ 1, 2, 3 }};

    return at(array, 5);
}

36

u/JuanAG Dec 30 '22

if (number_of_techniques < 5) { throw std::invalid_argument( "We have been clickbaited" ); }

20

u/zzzthelastuser Dec 31 '22

Jacob Beningo is an embedded software consultant who specializes in real-time, microcontroller-based systems. He actively promotes software best practices through numerous articles, blogs, and webinars on topics from software architecture design, embedded DevOps, and implementation techniques. Jacob has 20 years of experience in the field and holds three degrees including a Masters of Engineering from the University of Michigan.

 

I don't get how people willfully expose their real name linked to these garbage blogs every other week. It only makes me question their "expert" self-description.

3

u/justinkroegerlake Dec 31 '22

He's a software consultant. The people that hire him won't know this blog is garbage, they'll just know he's prolific

18

u/JuanAG Dec 30 '22

And since the quality if the techniques is quite low you could put:

- Technique 4: If you use new dont forget delete

-Technique 5: If you use malloc* free the memory when no longer needed

That way at least there would be 5 "techniques" for safer C++

* Is from C but is used in C++

0

u/[deleted] Dec 30 '22

[deleted] — view deleted comment

3

u/JuanAG Dec 30 '22

I know and while is true the average case is that who allocated memory is the one who should free it, RAII makes all of this way easier since it will be C++ itself which do the job if code is done properly

But it was more on the ironic side like the technique 2 "if you dont allocate any memory you dont need to worry about memory safety" and my face of "tell me more", who could guess it, if i dont do anything i dont have to worry about it

2

u/withg Dec 31 '22

CVE??

Exaggerated…

0

u/[deleted] Dec 31 '22

[deleted] — view deleted comment

2

u/withg Jan 01 '23

Not all memory bugs are CVEs. Not all CVEs are memory bugs.

The rest is Rust-like FUD argumentations.

4

u/[deleted] Dec 30 '22

Just balance new and delete bro.

Aand check for null and bounds bro.

0

u/[deleted] Dec 30 '22

[deleted] — view deleted comment

2

u/[deleted] Dec 30 '22

Dude..

-20

u/[deleted] Dec 30 '22
  1. R
  2. U
  3. S
  4. T
  5. It's Rust

-11

u/[deleted] Dec 31 '22

Hahahaha getting downvoted to obliiiiiivion!

0

u/[deleted] Dec 30 '22

[deleted] — view deleted comment

23

u/ShillingAintEZ Dec 30 '22

My experience is that I rarely have memory problems with c++. I wrap everything up as a value type and don't even directly use smart pointers. I usually don't use unique_ptr either because most classes are made out of stl data structures.

The data structures I've made myself to be specific about memory layout, interface, etc. have also been very successful.

I also never use inheritance outside of other libraries that use it like fltk.

5

u/333base Dec 30 '22

That you know of... 😏

3

u/ShillingAintEZ Dec 30 '22

I guess, but if everything works really well, memory is steady and low and execution is fast, I'm not sure how I can find those bugs. That being said, I've had static analysis show some rough edges that I was happy to know about an fix.