Not really; lets take a bit of C:
char szString [10];
char *pChar;
int *pInt;
pChar = pInt;
szString [12] = ‘A’;
pChar = (char *) pInt
… again leading to disaster.
Ok, you can break anything if you try hard enough, the fact you ‘can’ drive a car into a brick wall doesn’t make it inherently unsafe …
First;
pChar = pInt;
This will compile (as you would want it to) but it will tell you that what you’ve done might cause you a problem. Specifically you’ll get an “assignment from incompatible pointer type” warning … it might be that this is actually what you want to do, it’s certainly something I’ve used in the past, naturally if you wanted to do this you would type the conversion with pChar = (char*)pInt - off the top of my head, quite handy for converting an integer into bytes, here’s a runnable segment, compile with “gcc -std=c99 mp.c -o mp”;
/* mp.c */
#include "stdio.h"
#include "stdlib.h"
main()
{
char *pChar;
int *pInt;
pChar = pInt;
*pInt=10;
for(char *p=pChar;p<(pChar+sizeof(pInt));p++)
printf("%02x ",*p);
printf("\n");
}
As I’m guessing you know, no ‘C’ programmer would do this;
szString [12] = 'A';
Assuming this is the function required, you’d always wrap it with something like;
int setChar(char *memory,int size,int index,char byte)
{
if(index>=size) return 0;
*(memory+index)=byte;
return 1;
}
setChar(szString,sizeof(szString),12,'A');
Which would be safe … as for C++, nobody in their right mind (fx: don’s fireproof suit) uses C++ through choice. See this link; The Invention of C++ - Nice bit of net lore. Now I know this is touted as a “joke”, but I think there’s more than a sliver of truth in it, which is why it’s been reproduced so many times … (!)
Firstly I like Pascal, it was the first language I was taught at Uni, however it’s doesn’t typically have library support for writing high level applications and it’s not flexible enough when it comes to system applications. The only half-way decent implementations were Borland Pascal and Borland Delphi, (or Kylix on Linux) but eventually Borland sort of collapsed and they do all-sorts now - Delphi never really worked for them and Kylix was a spectacular failure.
Code: [Select]
szString : string [10];
pChar : ^char;
pInt : ^int;
pChar := pInt;
szString [12] := ‘A’;
Would just not compile! There is no way to force a Pascal (or ADA) compiler to do such nonsense.
Erm, Pascal has typecasting just the same as ‘C’ has typecasting, it’s really easy to produce the same sort of problem in Pascal and as for Delphi, it’s potential for mis-casting is epic! See how badly this breaks for you …
program demo;
type
pChar = ^Char;
var
S1 : String = '123';
ch : pChar;
begin
Writeln('Hello World');
ch := @S1;
Writeln(ch);
end.
At the end of the day, all languages (generally, I think Pascal is an exception) are written in ‘C’, as is Linux, and until you learn ‘C’ you’ll never ‘really’ understand how all the other languages work … personally I got a lot from writing an interpreter in Pascal, but didn’t quite get everything until I ported it into ‘C’.
Incidentally, were you aware that Linus did actually port the Linux kernel to C++ and released a version at one point … from memory it was back in the early / mid 90’s … but it was very short lived. More recently Linus has been quoted as saying;
C++ is a horrible language. It's made more horrible by the fact that a lot
of substandard programmers use it, to the point where it's much much
easier to generate total and utter crap with it. Quite frankly, even if
the choice of C were to do *nothing* but keep the C++ programmers out,
that in itself would be a huge reason to use C.
Also;
I've come to the conclusion that any programmer that would prefer the project to be
in C++ over C is likely a programmer that I really *would* prefer to piss
off, so that he doesn't come and screw up any project I'm involved with.
My personal preferences are ‘C’ for anything needing performance, ‘Python’ for system services / scripting, PHP for web / JSON applications, and Javascript (obviously) for client side browser UI work. (and Gambas for native/UI work) I don’t tend to use anything else apart from the occasional Bash script. I’d use Pascal, if I had to (never have) , C++ if I was desperate, and Perl the day Hell freezes over.
When I get some time I will be taking a Peek at “go”;