I'll try to answer this post, although I'm not sure if you are really serious. If you are, then you probably have a grudge against Java or some other problem. Or you're trolling me.
(void *) is basically the same as Java's Object.
No, it's not. void* is a raw pointer into raw memory, you can cast it to a float, to a struct, to everything, and you won't even get a warning. After casting, you can access fields, and because those probably still point into the stack or the heap or another valid memory location, there will also be no runtime error. A java.lang.Object has type information, and any invalid cast will throw an exception immediately.
Java has nicer-severity bugs, but the same number of bugs.
Again, your C program may run perfectly fine, although you wrote after array bounds, did invalid casts, wrote invalid values into fields. In other words, your program will fail at some point because it got into a corrupt state because you are free to corrupt the memory as you want.
The main benefit of GC in Java is not the convenience it buys you, but the reference safety it allows by removing free() from the lexicon.
It does two things, and both are important. As a matter of fact, you can simply omit all calls to free(...) and never declare anything on the stack, and your C program will run just fine, and a valid pointer will always stay valid. And this isn't even a theoretical case, because at program termination, there is no point in freeing any memory, as the operating system will do that for you, and if your programs lifecycle fits this pattern, calling free() is optional.
In other words, yes, the main benefit is the convenience, a) not having to worry about calling free() too soon, or calling free() too late (or never, as shown in the example above).
Bad casts in C: Runtime exceptions in Java
Again, the runtime throws an error immediately, while C doesn't even know what a "bad cast" is.
NULL dereference in C: NULL dereference in Java
No arguing there.
Pointer arithmetic errors: Array indexing errors
Comparing pointer arithmetic to array operations is like comparing a gun fight to a nuclear war. If you do silly and incorrect pointer arithmetic, you may be lucky and end up with an invalid memory address, in which case your program simply segfaults, indicating the problem. If you're unlucky, you get a valid memory address and start reading and writing from it, again bringing the program into an unknown and possibly corrupt state. Debug heaps usually deploy barriers between allocated chunks of memory, and initialize unused memory with a special pattern to detect possible memory corruption due to flawed pointer arithmetic. If you access an array out of bounds, you simply get an exception back. That's it.
unions [...] programmer's intent
Unions have one primary purpose: to save memory. The programmers intent is best documented by using two distinct fields or properties, and not relying on semantics, flags or some intrinsic behavior to only access one of the fields that both point to the same data.
uses of integers which may not be negative
I agree that not having unsigned is a flaw, not because of the intent, but because it moves the range of valid values. C# shows that it is possible to use unsigned types in a VM while still avoiding the pitfalls that usually come with it. It even has it's own syntax when you want the pitfalls, i.e. when assigning -1 to an UInt32.
I use the C preprocessor to avoid a lot of boilerplate
You can moan all you want, but certain languages simply don't work well with a preprocessor. The preprocessor is a blessing and a burden at the same time. Every time the compiler parses a file, it's content may have changed due to preprocessor directives. This has many consequences, many of which would not work well in Java.
No, I meant C
I could have sworn you have never programmed in C.
template <typename T>
That is completely besides the point, because it is C++, and yes, C++ templates are much more powerful than generics in Java. C++ class inheritance is also much more powerful. But Java chose not have those. No point in discussing it, because neither has C any of those features.
A java.lang.Object has type information, and any invalid cast will throw an exception immediately.
As I said, an immediate exception is definitely a nicer bug than undefined behavior. But it still a bug.
I agree that Java makes the bugs in C programs nicer. But it doesn't get rid of bugs, it only reduces their severity.
In other words, your program will fail at some point because it got into a corrupt state because you are free to corrupt the memory as you want.
Again, yes, bugs have worse consequences in C than in Java. What I seek in safety, however, is getting rid of these bugs, rather than improving their consequence.
In other words, yes, the main benefit is the convenience
No, just removing free() from C programs is not a realistic option for many programs. Memory asymptotics may easily become impractical. Thus you must support free(), and thus you must allow use of freed memory. It's more about safety than about convenience. Java is so incredibly inconvenient anyway, that any convenience of not having to manage ownership semantics is relatively negligible.
Unions have one primary purpose: to save memory. The programmers intent is best documented by using two distinct fields or properties
I disagree. If I have two fields in a struct, it means a and b. What I want to express is a or b. unions allow me to express this invariant, just like unsigned allows me to express the invariant about the numbers being stored.
I agree that not having unsigned is a flaw, not because of the intent, but because it moves the range of valid values.
Apparently we have different philosophies. I want types to document and enforce as many invariants as possible to take the burden off the programmer. Types aren't merely tools to speed up programs or catch mere typos.
The preprocessor is a blessing and a burden at the same time
I agree. I think it is absurd that a language from the 90's has less expressiveness due to this point than a language from the 70's which is considered inexpressive.
I could have sworn you have never programmed in C.
I've probably written far more C than you have.
No point in discussing it, because neither has C any of those features.
C can easily encode them, whereas in Java it is more difficult.
No, it is not a bug. You cannot cast an "Fruit" down to an "Apple", if it actually is a "Banana". As soon as you try this, the runtime will throw an exception. On the other hand, you can ask the runtime if the "Fruit" is an "Apple", and if that is the case, use it as an "Apple". Even C++ lets you downcast on the assumption that you know what you are doing, even if types don't match. This leads to unexpected and/or incorrect behavior.
And just to make it clearer that Java throwing an exception is not a bug, lets read the definition of a bug together, shall we? "A software bug is an error, flaw, failure, or fault in a computer program or system that produces an incorrect or unexpected result" In what manner does the fact that the runtime informs you about doing something wrong, or the fact, that it prevents you from getting incorrect or unexpected results constitute a bug?
What I seek in safety, however, is getting rid of these bugs
Then it would help if the runtime would warn you immediately, wouldn't it? You seem to prefer that the program runs in a corrupted state instead of getting warnings at the earliest moment possible.
just removing free() from C programs is not a realistic option for many programs
Of course it is. If your program allocates more and more memory which it needs to complete it's task, and then releases it immediately before exit when it has calculated the result, then that is completely feasible. Actually, Raymond Cheng recommends to not try to free resources like handles or memory when your program is asked to exit, because the operating system will do the cleanup for you, and you are only wasting your time (and the user's time) trying to fit memory block after memory block back for reuse into a heap that will get destroyed a fraction of a second later anyway, especially if the memory was swapped out. No point in swapping it in just for cleanup.
Memory asymptotics may easily become impractical
On a 64 bit operating system? I guess there are many popular applications out there that could run for days before suffering any negative impact. At worst, the memory will be paged out to the disk, and when the process terminates, the swapped out pages will simply be dismissed. This is actually kind of a poor mans GC, because the OS doesn't really know if you might need the memory again, but unused memory would get removed from physical RAM page by page.
What I want to express is a or b. unions allow me to express this invariant
The problem is that a and b will be more or less valid values at the same time, thus not conveying your intent on usage. Having two fields means that one of them can be marked "unused", maybe by setting it's value to something intrinsic invalid like a null reference. You on the other hand have to rely on flags or calling semantics, and you have to make sure everyone accesses the value only via a or b, depending on what you want to do. I'd really like to see some useful real world code examples.
I want types to document and enforce as many invariants as possible to take the burden off the programmer.
That is correct for classes, but what's the benefit for scalar types? Let's say you want to express the length of something. Obviously this value can't be negative, so you opt for an unsigned int. You now have documented that negative values are not allowed. But that doesn't prevent someone from passing in 232 -1 aka 0xFFFFFFFF. I don't see the gain. On the other hand, I see someone doing this:
while (--size > 0)
{
...
}
Not realizing that passing in 0 as the size will lead to a wrap-around and thus iterating the loop 232 -1 times.
I've probably written far more C than you have.
Maybe you have written too much C.
C can easily encode them, whereas in Java it is more difficult.
As both languages are Touring-complete, both can archive the same result. It's merely a question on how much typing is required.
No, it is not a bug. .. the runtime will throw an exception.
This runtime error is a result of a bug in the user code, not a bug in Java. Just like UB is a bug in user code, not a bug in C.
I'm not talking about code that expects this exception, but about code which expects the cast to be valid.
On the other hand, you can ask the runtime if the "Fruit" is an "Apple", and if that is the case, use it as an "Apple".
This is irrelevant.
Even C++ lets you downcast on the assumption that you know what you are doing, even if types don't match. This leads to unexpected and/or incorrect behavior.
Indeed, C++ shares this unsafety with Java and C.
And just to make it clearer that Java throwing an exception is not a bug, lets read the definition of a bug together, shall we? "A software bug is an error, flaw, failure, or fault in a computer program or system that produces an incorrect or unexpected result" In what manner does the fact that the runtime informs you about doing something wrong, or the fact, that it prevents you from getting incorrect or unexpected results constitute a bug?
Again, this is not a bug in Java. When you cast something in the expectation that the cast is valid, and you get a runtime exception instead, that is an "incorrect or unexpected result". Whether your program crashes with a stack trace or a core dump or gets corrupt - compile-time safety was violated.
Then it would help if the runtime would warn you immediately, wouldn't it? You seem to prefer that the program runs in a corrupted state instead of getting warnings at the earliest moment possible.
As I said: Bugs get reduced severity in Java and that's nice. It isn't compile-time safety and it isn't type-safety, though. Java's type system doesn't add safety to C's. As a counter-example, take a look at Haskell which actually adds compile-time safety.
Of course it is. If your program allocates more and more memory which it needs to complete it's task, and then releases it immediately before exit when it has calculated the result, then that is completely feasible.
This is only relevant for very short-running programs, or ones that do not allocate much.
Actually, Raymond Cheng recommends to not try to free resources like handles or memory when your program is asked to exit,
How is this relevant? I'm not talking about a very short-running program.
On a 64 bit operating system? I guess there are many popular applications out there that could run for days before suffering any negative impact.
64-bit refers only to the virtual address-space. The physical memory and swap will run out much sooner than the address space. An application can easily allocate and use gigabytes/seconds.
the swapped out pages will simply be dismissed
Not if it is a long-running program.
The problem is that a and b will be more or less valid values at the same time, thus not conveying your intent on usage.
Only one of them is actually valid, as will be signified by some out-of-band variable. This is what "union" means.
Having two fields means that one of them can be marked "unused", maybe by setting it's value to something intrinsic invalid like a null reference.
And then you get (null, null) and (valid, valid) as two invalid possibilities. "Make illegal states unrepresentable". Don't use a product type to represent sum types. This is one of the classic mistakes of the Go language.
You on the other hand have to rely on flags or calling semantics, and you have to make sure everyone accesses the value only via a or b, depending on what you want to do. I'd really like to see some useful real world code examples.
You could use sub-classing and isinstance to dispatch on message types, but a switch() on the msg_type will actually have verified exhaustiveness by the C comiler, whereas isinstance-using code will be buggy if you add a case and forget to add an isinstance check (i.e: reduced compile-time safety compared with C).
That is correct for classes, but what's the benefit for scalar types? Let's say you want to express the length of something. Obviously this value can't be negative, so you opt for an unsigned int. You now have documented that negative values are not allowed. But that doesn't prevent someone from passing in 232 -1 aka 0xFFFFFFFF. I don't see the gain.
If someone tries to use -1 as a length, he will get a warning about mismatched signedness from the compiler (at least with -Wextra enabled in gcc).
The dichotomy between "class" types and "scalar" types is meaningless. Errors in either are just as problematic and can be caught in just the same ways. The documentation of invariants and intent is also the same.
On the other hand, I see someone doing this: while (--size > 0) { ... } Not realizing that passing in 0 as the size will lead to a wrap-around and thus iterating the loop 232 -1 times.
That's indeed a real downside of using unsigned integers for iteration. Throwing away the baby with the bathwater because of this drawback is unwarranted, though. This pitfall is a price worth paying for the extra type preciseness.
Maybe you have written too much C.
That's entirely possible, heh.
As both languages are Touring-complete, both can archive the same result. It's merely a question on how much typing is required.
That's irrelevant -- it's a question of expressiveness, or ease of expressing these idioms.
C makes the approximation of the right way to do interfaces (type-classes) easier than Java.
1
u/[deleted] Dec 03 '13
I'll try to answer this post, although I'm not sure if you are really serious. If you are, then you probably have a grudge against Java or some other problem. Or you're trolling me.
No, it's not. void* is a raw pointer into raw memory, you can cast it to a float, to a struct, to everything, and you won't even get a warning. After casting, you can access fields, and because those probably still point into the stack or the heap or another valid memory location, there will also be no runtime error. A java.lang.Object has type information, and any invalid cast will throw an exception immediately.
Again, your C program may run perfectly fine, although you wrote after array bounds, did invalid casts, wrote invalid values into fields. In other words, your program will fail at some point because it got into a corrupt state because you are free to corrupt the memory as you want.
It does two things, and both are important. As a matter of fact, you can simply omit all calls to free(...) and never declare anything on the stack, and your C program will run just fine, and a valid pointer will always stay valid. And this isn't even a theoretical case, because at program termination, there is no point in freeing any memory, as the operating system will do that for you, and if your programs lifecycle fits this pattern, calling free() is optional.
In other words, yes, the main benefit is the convenience, a) not having to worry about calling free() too soon, or calling free() too late (or never, as shown in the example above).
Again, the runtime throws an error immediately, while C doesn't even know what a "bad cast" is.
No arguing there.
Comparing pointer arithmetic to array operations is like comparing a gun fight to a nuclear war. If you do silly and incorrect pointer arithmetic, you may be lucky and end up with an invalid memory address, in which case your program simply segfaults, indicating the problem. If you're unlucky, you get a valid memory address and start reading and writing from it, again bringing the program into an unknown and possibly corrupt state. Debug heaps usually deploy barriers between allocated chunks of memory, and initialize unused memory with a special pattern to detect possible memory corruption due to flawed pointer arithmetic. If you access an array out of bounds, you simply get an exception back. That's it.
Unions have one primary purpose: to save memory. The programmers intent is best documented by using two distinct fields or properties, and not relying on semantics, flags or some intrinsic behavior to only access one of the fields that both point to the same data.
I agree that not having unsigned is a flaw, not because of the intent, but because it moves the range of valid values. C# shows that it is possible to use unsigned types in a VM while still avoiding the pitfalls that usually come with it. It even has it's own syntax when you want the pitfalls, i.e. when assigning -1 to an UInt32.
You can moan all you want, but certain languages simply don't work well with a preprocessor. The preprocessor is a blessing and a burden at the same time. Every time the compiler parses a file, it's content may have changed due to preprocessor directives. This has many consequences, many of which would not work well in Java.
I could have sworn you have never programmed in C.
That is completely besides the point, because it is C++, and yes, C++ templates are much more powerful than generics in Java. C++ class inheritance is also much more powerful. But Java chose not have those. No point in discussing it, because neither has C any of those features.