3 ms·
Two points: - Data race leads to subtle bugs on all languages and runtimes, including Java and C#. They are just not called undefined behavior in those languag
by netheril96 11y ago
Two points:
- Data race leads to subtle bugs on all languages and runtimes, including Java and C#. They are just not called undefined behavior in those languages.
- char* and unsigned char* are allowed to alias any pointer. This is one of the exceptions to the strict aliasing rule.
- lomnakkus 11y ago> Data race leads to subtle bugs on all languages and runtimes, including Java and C#. They are just not called undefined behavior in those languages. That's because they aren't. You might get incorrect results or exceptions or whatnot, but NOT Undefined Behavior aka. nose-demons. What can happen in cases of data races is always constrained by the VM model. (Obviously, this is modulo bugs in the actual VM implementation, but that probably goes without saying.)
- netheril96 11y agoIn practice, undefined behavior is unpredictable because compiler does optimization with the assumption that you do not have undefined behavior. When you do have them, the optimization won't preserve the "as-if" rule. The same applies to Java/C# when data race is present. The JIT must be generating optimized codes assuming that no data race occurs, because it is impossible to detect or correct them (at least in current implementation). When you do have data race, the bugs will be as subtle and Schrödinger's as if data race occurs in a C program.
- the_why_of_y 11y agoData races in Java/C# races will result in incorrect values (which may be serious if your program is doing anything important and subtle to find, as you say), but they will not ever result in "undefined behavior" in the way ISO C defines the term. Specifically a data race in Java/C# will not cause your program to do out-of-bounds memory accesses or use-after-free or corrupting unrelated objects because other parts of the respective VM specifications prevent such outcomes. For the details see e.g. http://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4.5 http://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html...
- dsfuoi 11y agoThere are other exceptions. Signed or unsigned or a qualified type may alias the corresponding type. A type that includes the type as a member.
- jcranmer 11y agoThere is a very important bit of strict aliasing that your statement misses. Yes, char* and unsigned char* can be used to access any lvalue. However, only signed/unsigned char* can be used to access a signed/unsigned char array. (Strict aliasing is defined via the dynamic type of an object, not which alias pairs are necessarily must-not-alias. Symmetry is not inherent, and in the case of char*, it is indeed not present).
- netheril96 11y agoWhen you write custom memory allocator, you request memory with `malloc` or `mmap` first. The return value of those functions are raw storage, not objects with dynamic type of character array. Neither is the return value of you custom `allocate`. Strict aliasing rule does not apply.