4 ms·
What if with "dangerous languages" like C you could turn off pointers, or make only functional subroutines? For instance, perl has this construct, to make your
by m463 6y ago
What if with "dangerous languages" like C you could turn off pointers, or make only functional subroutines?
For instance, perl has this construct, to make your programs better:
a program foo.pl:
$c = 1;
runs fine. Now turn on strict:
use strict;
$c = 1;
and
$ perl ./foo.pl
Global symbol "$c" requires explicit package name at ./foo.pl line 2.
Execution of ./foo.pl aborted due to compilation errors.
you must declare $c:
my $c = 1;
the idea would be... write your program and you can opt-in to various constructs.
NOTE: my first example didnt' work.
use strict;
$a = 1;
won't throw an error. Turns out the variables "$a" and "$b" are exempt from "use strict;" ha. wow.
- scarmig 6y ago`use stricter;` is the obvious solution.
- snicker7 6y agoNot being allowed to use pointers means not having control of memory layout.
- m463 6y agothe idea would be routine by routine you could turn on features. So make this function functional with no side effects. This function doesn't (overtly) use pointers. etc... At various jobs, we've done things like this with warnings. Get a source file to compile warning free, then turn on -Werror (this is done via compile flags, a different mechanism)
- lmm 6y agoYou have to be very careful that the looser language doesn't break the rules that the stricter language relies on. Have a look at the Noether design for a serious effort at doing this kind of stratified language.
- raverbashing 6y ago> What if with "dangerous languages" like C you could turn off pointers You can't. C is a fundamentally defective language because it doesn't have the "memory slice"/"array" construct. Of course you can work around it, but if you eliminate pointers then you can't do much with C. Heck you can't even print "Hello World" with it (well, ok, technically you can but not in the usual way).
- daotoad 6y agoYou could use MISRA guidelines for C. https://en.wikipedia.org/wiki/MISRA_C https://en.wikipedia.org/wiki/MISRA_C
- raverbashing 6y agoThey seems to be behind a paywall so I can't comment much on that But still, I believe you can't do much with C if you "shut down" pointers completely. Because strings are pointers in the end.
- m463 6y agomaybe the parent changed the link? the link appears to be wikipedia :)
- raverbashing 6y agoYes, the wiki has some recommendations (and I remember the C++ JSF standard) but if you follow the link to the actual document it seems to be behind a paywall
- m463 6y agowith my "use strict" perl example, the warnings had some carveouts. For example it had a carveout so that "$a" and "$b" didn't have to be declared (I think they are known variable names for sort?) Why not allow: printf("Hello world\n"); but turn off: acpi_ut_repair_name(&(*converted_name)[j]); or if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
- jimbob45 6y agoC# does that with unsafe blocks. Honestly, C# covers the gamut of needs up to classes and closures down to marshaling and pointers. If someone were to make a GC-less C# (cough Vala), I could see it replacing C++ in a matter of single-digit years.
- GordonS 6y agoSince dotnet core 2.1 the garbage collector has been easily pluggable, just by setting an env var, `COMPlus_GCName`, to point to your implementation. That doesn't mean it will operate without a GC mechanism per se, but it can operate with a "zero" GC that never collects garbage. I know this isn't what you meant, but thought it was an interesting point.
- dntbnmpls 6y agoGC-less C# wouldn't even replace Visual C++ codebase in 10 years let alone the entire C++ codebase. And if you are going to use unsafe C#, you might as well just stick to Visual C++. Microsoft really shouldn't have included unsafe code/context into C#. It's the one glaring flaw in an otherwise decent language.
- simias 6y agoThe problem is that C without pointers is extremely limiting. At least in C++ you'd have references left. More generally I don't think you can really "patch" a language to make it safe. It's going to leak unsafety all over the place. Consider a simple `printf("%s", 12);` which is obviously broken but doesn't really trigger any kind of memory unsafety at the call site. You might argue that "%s" is a pointer in disguise, but if you ban C strings and arrays you won't have a lot left to work with (besides in this case the string pointer is perfectly valid, it's the format string that's incorrect).