Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If C# pointers are considered "unsafe," does that mean C++ pointers are "unsafe" too?

I am doing a project in C#, which could benefit from a linear algebra package. I've looked at the ones out there, but I don't really want to pay, or I found them not very good. So I decided to write my own.

I read that C++ arrays are much faster than C# arrays, but that it was possible to get similar performance using pointer arrays in C#, although they are considered "unsafe." I'm curious to know how C++ pointers differ, and if the "unsafe-ness" applies to C++ as well, or if they are two fundamentally different things.

like image 853
user3685285 Avatar asked May 15 '16 16:05

user3685285


People also ask

Is IF () a function in C?

The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input.

What is if statement in C?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.

How do you use if and C?

C if StatementThe if statement evaluates the test expression inside the parenthesis () . If the test expression is evaluated to true, statements inside the body of if are executed. If the test expression is evaluated to false, statements inside the body of if are not executed.

Why is if condition used?

The IF function is one of the most popular functions in Excel, and it allows you to make logical comparisons between a value and what you expect. So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.


2 Answers

Both C# (unsafe) pointers and C++ (raw) pointers have the following characteristics:

  • They allow you to reference an address in a given address space.
  • They allow you to perform simple arithmetic operations (addition and subtraction) on them, involving integers as offsets.
  • They allow you to dereference whatever they point to as data of a particular type.
  • Wrong usage of them can invoke undefined behavior, making it exclusively your responsibility to ensure that you're using them correctly.

In that sense, and regardless of any minor differences (like syntax, pinning, etc), C# pointers and C++ pointers are pretty much the same programming concept. Therefore, they lend themselves to static analysis pretty much equally and thus they are equally safe or unsafe. So the fact that C# explicitly calls this construct out as unsafe doesn't make the equivalent C++ construct "safe". Rather, the ability to use "unsafe" code is "always on" in C++.

As an example, consider the case where you attempt to access an array using an index that's out of bounds:

  • With a C# array you will get an exception when using the indexer syntax and you will invoke undefined behavior when using a pointer and an offset.
  • With a C-style array in C++ you will invoke undefined behavior when using either the indexer syntax or a pointer and an offset (because those two syntaxes are equivalent for C-style arrays).
  • With a C++11 std::array you will get an exception when using array::at and you will invoke undefined behavior when using the indexer syntax.
like image 173
Theodoros Chatzigiannakis Avatar answered Sep 18 '22 10:09

Theodoros Chatzigiannakis


Roughly speaking (and it is a very crude approximation), a C# unsafe pointer is the same sort of thing as a C++ pointer.

With both, there is a lot more responsibility on the programmer to get it right whereas with normal C# if you get things wrong, the worst that will happen is that an exception will be thrown. The run-time checks that give those guarantees cost performance, but if you switch them off - you are on your own.

like image 26
Martin Bonner supports Monica Avatar answered Sep 19 '22 10:09

Martin Bonner supports Monica