Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

That is, why does C++ support operations that can be used to violate the rules of static (compile-time) type safety?

0
10 Posted

That is, why does C++ support operations that can be used to violate the rules of static (compile-time) type safety?

0

• to access hardware directly (e.g. to treat an integer as a pointer to (address of) a device register) • to achieve optimal run-time and space performance (e.g. unchecked access to elements of an array and unchecked access to an object through a pointer) • to be compatible with C That said, it is a good idea to avoid unsafe code like the plague whenever you don’t actually need one of those three features: • don’t use casts, • keep arrays out of interfaces (hide them in the innards of high-performance functions and classes where they are needed and write the rest of the program using proper strings, vectors, etc.), • avoid void* (keep them inside low-level functions and data structures if you really need them and present type safe interfaces, usually templates, to your users) • avoid unions, • if you have any doubts about the validity of a pointer, use a smart pointer instead, • don’t use “naked” news and deletes (use containers, resource handles, etc.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123