Skip to main content
72 votes

How can Rust be "safer" and "faster" than C++ at the same time?

You seem to have quite a bit of misconceptions, which I'll address alongside your question. If anything is still unclear, please feel free to comment on this answer so I can address it Safer Yes, Rust ...
Matthieu M.'s user avatar
  • 15.2k
53 votes

How can Rust be "safer" and "faster" than C++ at the same time?

The assumptions are wrong twice: safer code does not necessarily mean more instructions, when the safety is obtained with safer language design and rules; one language is not faster than another: it'...
Christophe's user avatar
  • 81.4k
27 votes

How can Rust be "safer" and "faster" than C++ at the same time?

To look at this a different way, Fortran is: Safer than C, because it doesn't have C-style unrestricted pointers. "Unrestricted" here meaning "you can do arbitrary operations on them&...
Philip Kendall's user avatar
14 votes

Is there a sensible way to sort coordinates?

the smaller the difference between the indices of any two elements, the smaller the difference between the values of the elements. When working with lists of coordinates or similar values with more ...
Michael Borgwardt's user avatar
13 votes

How can Rust be "safer" and "faster" than C++ at the same time?

I have been told that Rust is both safer and faster than C++. If that is true, how can that be even possible? One way Rust does this is that it refuses to compile many programs for which C++ would ...
jpa's user avatar
  • 1,408
12 votes
Accepted

Should execution continue if array is empty?

Checking against an empty array will increase the size of your code considerably and make it harder to read. The benefit is a tiny improvement in efficiency, but all you save is the price of a few ...
Kilian Foth's user avatar
11 votes
Accepted

How did the custom of using square brackets for array elements develop?

The main precursor language to C and Pascal was Algol. The earliest version of that was Algol 58 which used square brackets for array declarations and references. The reason that Algol used square ...
Alex's user avatar
  • 3,932
9 votes
Accepted

Fastest algorithm of dividing array into positive and negative numbers

That task is simple: Iterate from start and end at the same time, and swap the element if needed. L = index_first R = index_last while L < R while L < R and v[L] < 0 L++ while ...
Deduplicator's user avatar
  • 9,129
9 votes
Accepted

How exactly indexing works in arrays?

Forget hashing, it is much simpler than that. Arrays will always be laid out in memory using consecutive storage locations. The compiler knows the array to start at memory cell x. When it needs to get ...
Martin Maat's user avatar
  • 18.6k
9 votes

Why does C not support direct array assignment?

C originally came from a predecessor language called B, made by Ken Thompson. In B, there were no types. Everything was a "word" (basically, an int). In B, arrays were just pointers to the ...
Min4Builder's user avatar
8 votes

Is "Array[1]" the first element or second element in the array?

Programming languages: Array[1] uses an implicit mapping between the index 1 a specific array element. This mapping is language specific. Many languages start at 0, some at 1. Some languages allow ...
Christophe's user avatar
  • 81.4k
8 votes

Are there any use cases for List when Deques and Arrays are available?

A List is general and flexible. Generalized structures are ideal for return values in business, web, and desktop applications. It's not optimal to return a Deque or an Array if your consumers will be ...
svidgen's user avatar
  • 15.2k
8 votes

sizeof(a)/sizeof(a[0]) vs sizeof(a)/sizeof(t) where t is type in C from K.N.King

The difference between sizeof(a)/sizeof(t) and sizeof(a)/sizeof(a[0]) is that for the first one you need to supply two pieces of information (the array name and the type of its elements) while the ...
Bart van Ingen Schenau's user avatar
8 votes
Accepted

Why does C not support direct array assignment?

Why does C not support direct array assignment? It is arguably a shortcoming, due to missing features around arrays, but one the original designers choose not to resolve.  Add in the (over) emphasis ...
Erik Eidt's user avatar
  • 34.7k
7 votes

C# is fantastic, if only List 'd respect Remove&Return

Typically, people remove things from lists already knowing what those things are, as that knowledge is why they want to remove the item in the first place. By contrast, people don't usually want to ...
Nat's user avatar
  • 1,101
7 votes

Arrays vs Maps for listing items that have a unique id

The guiding principle for choosing a data structure should be its usage, not its members. If, as you say, you only need the items to render sequentially, an array is perfectly fine. If you need to ...
Avner Shahar-Kashtan's user avatar
6 votes

Check for empty PHP array

If the intent of your condition is to check that the array contains 0 elements, using count($array) === 0 is the best and most readable solution.
Thijs Riezebeek's user avatar
6 votes

Handling out of bounds requests in embedded C library

since we're implementing our own error code, the caller has to actively remind themselves to to check this value, making it easy to miss. Why not design the API in a way it becomes hard for the caller ...
Doc Brown's user avatar
  • 218k
6 votes

Should I move tasks which is just for a specific element only out of for loop?

There is a huge difference in both code snippets: if this.arr.length is 0 then option 1 works as designed, whereas option 2 fails in an attempt to perform operations on not existing elements. ...
Christophe's user avatar
  • 81.4k
6 votes
Accepted

Should I use Array or Set if both can be used to finish my task?

The use of either Array or Set by your function is equivalent: You create a container, and you push stuff into it. This leaves two areas for consideration: What does the consumer need to do with the ...
cmaster - reinstate monica's user avatar
6 votes

Should execution continue if array is empty?

The question is whether the operation would be meaningful for an empty array. Take the function count() which returns the number of elements. That is meaningful for an empty array - the count of ...
JacquesB's user avatar
  • 61.6k
6 votes
Accepted

How to guarantee an ID is unique in a non-UUID set of values?

Math You are looking for a formula that allows you to traverse the entire symbol space without every repeating itself. One such formula for modulo integers is x := x + 1. Seed x with any value and ...
Kain0_0's user avatar
  • 16.5k
6 votes
Accepted

What is the difference between an index and an offset?

An index stands alone, though some data structure (e.g. array) is implied in using the index.  Still the index can be used (e.g. as an id) without an array. An offset has to be an offset from ...
Erik Eidt's user avatar
  • 34.7k
6 votes
Accepted

Do C++ compilers optimize/pack small data types (e.g. boolean values)?

Yes, such masks and stencils will be performed efficiently. But at least when writing C, this is not automatic. In C and C++, every object must have an address. Bools are objects. The smallest ...
amon's user avatar
  • 136k
5 votes
Accepted

Return array pointers vs. populating an array inserted as a parameter?

In classical C, returning an array from a function isn't as easy as in Java. That's why C functions often choose to populate an array passed in from the caller, whereas Java methods typically follow ...
Ralf Kleberhoff's user avatar
5 votes

Return array pointers vs. populating an array inserted as a parameter?

From a semantic perspective, returning an array from the method suggests that it's a different array than the one you passed in. That's why the accepted C style, when modifying an array in place, is ...
Robert Harvey's user avatar
5 votes

How did the custom of using square brackets for array elements develop?

This is an interesting read: https://en.m.wikipedia.org/wiki/Bracket The following are my own observations. The C designers took great care adopting the meaning of characters and constructs as they ...
Martin Maat's user avatar
  • 18.6k
5 votes

JS - two array filters vs. one forEach?

This might be a case of pre-optimization. Each call to filter will loop over the array, so your first example iterates twice. Your second example iterates once. On the surface your second approach ...
Greg Burghardt's user avatar
5 votes
Accepted

What prevents Java from having immutable primitive arrays?

Arrays are "special" in Java. They are not implemented as a class with constructor and members. Instead array creation and array operations (like reading and writing elements) are compiled ...
JacquesB's user avatar
  • 61.6k

Only top scored, non community-wiki answers of a minimum length are eligible