Segmentation fault protection: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(One intermediate revision by one other user not shown)
Line 55:
...
</syntaxhighlight>
 
=={{header|Nim}}==
Nim can generates native code (using C as intermediate language) and it is possible for a program to cause a segmentation violation. Therefore the language has been designed to prevent this to happen, at least if the user doesn’t use unsafe features:
 
# Nim is a statically and strongly typed language. That means that is is possible to detect a lot of errors at compile time.
# Nim proposes a memory management model which ensures memory integrity.
# At run time Nim insures that all variables are initialized which prevents some problems.
# Nim uses copy semantic which means that an assignment copies the value instead of copying a reference. This prevents many errors related to aliasing.
# Nim generates code to check that indices are valid, that values are in a valid range, that operations do not overflow, etc. However, checking that a reference or a pointer is not nil is not done for performance reasons. This isn't really a problem, since accessing a null address instantly terminates the program (with a segmentation violation, of course). So doing a check to raise a non catchable exception would not be an improvement.
 
So far, so good, but Nim provides pragmas to deactivate the checks in some parts of the code. And it is also possible to compile the programs in “danger” mode. This way, we improve the performance but at a high price as it is no longer possible to ensure the memory integrity.
 
As a system language, Nim also provides unsafe features such as accessing the address of a variable, using type casting, deactivating the initialization of a variable, using unmanaged memory via pointers, etc. Of course, such constructions may cause memory corruption.
 
So, provided you don't use unsafe features, Nim generates code that greatly reduces the risk of a segmentation violation.
 
=={{header|Perl}}==
Line 127 ⟶ 142:
 
So the example code below compiles:
<syntaxhighlight lang="ecmascriptwren">var myArray = [1, 2, 3, 4, 5, 6]
var myVariable = myArray[500]</syntaxhighlight>
but if you try to run it you get a "Subscript out of bounds" error.
9,476

edits