Space/Science — HabitableZone

Space/Science » in reply to re: It's like the "array out of bounds" bug in C programming

No, that code snippet will actually run...

Larry J said:

>Actually, your code would've blown up for two reasons:
>
> double foo[13] ; foo[14] = 1.23 ; printf("%f\n", foo[14]) ;
>
>First, remember that all arrays in C (C++, Java, too) begin
>with zero.... Depending on the OS, you might have a crash,
>memory corruption at the location just past the end of the
>array, or a General Protection Fault.

I chose that example because it's wrong but will run correctly anyway. Here's a screen shot from Linux running on a PC:

-------------------------------- begin screen shot ---------------------------

[27] cat wrong.c
#include <stdio.h>
int main()
{
double foo[13] ;
foo[14] = 1.23 ;
printf("%f\n", foo[14]) ;
return(0) ;
}

[28] gcc -Wall wrong.c -o wrong

[29] wrong
1.230000

------------------------------- end screen shot ---------------------

Note that "wrong.c" compiled cleanly without producing a warning and then output the correct result. This sort of array-out-of-bounds error would eventually cause stack corruption or randomly overwrite other data in memory if the program became long enough.

Larry J also said:

>When C was created at Bell Labs back in the early
>1970s, it was designed by professional programmers
>for professional programmers. As a result, C doesn't
>do any error checking for things like out of bounds or
>off by one errors. It doesn't check for such statements
>at compile or at run time. As a result, C programs tend
>to be smaller and execute faster than programs written
>in most other languages (other than assembly, of course).
>
>C assumes you know what you're doing.
>C is like shaving with a straight razor.
>C will cheerfully let you cut your own throat.

I completely agree with Larry on these points. By the way, the guys at Bell Labs who invented C and Unix may have been the brightest computer programmers who ever lived.

>Don't get me wrong, C was a wonderful language in its
>day and is still used for low level code (things like OS
>kernels) where speed and size are important. Almost all
>mainstream application software is object-oriented today,
>so later languages like C++ and Java are more widely
>used than C.

This is where I part company with Larry about C. As an industrial strength programming language, I would argue that C ***IS*** optimal (almost a perfect professional programming language). It's primitive enough that I can get nice and close to the hardware but high level enough that I can easily read well written source code. C is rich enough as a programming language that I can exercise proper programming practice. This is what I want from a programming language:

1) source code portability
2) executable raw speed
3) readabie source code
4) source code longevity

Properly written C will run on any computer that supports the Gcc compiler (that's just about every computer in the world). Only highly optimized assembly language written by an expert is faster than fully optimized C. Properly written C is very easy to read (much easier than assembly language or poorly written Fortran). The C programming language will never die. Why? Because almost all the operating system kernels and shells are written in C. Code written in the early 1970s can cleanly compile on modern computers. Only very rarely have I encountered an old NASA legacy code written in Fortran that will compile on the first try on a modern machine. Almost always the old Fortran codes are written in obsolete dialects that require some modification to compile on a modern machine. Larry went on to praise C++ and Java in comparison to C. My experience with C++ and Java for high performance scientific programs is that these languages are significantly slower than C due to the near useless OOP overhead. C++ and Java are also harder to read due to the OOP constructs. Most of the C++ source code that I've seen at NASA is little more than vanilla C with C++ comments and I/O.

Can novice programmers cut their throats with C? You bet they can!! C is for professional programmers only. Use Matlab or Java if you want to train kids how to program.

Here's some fun with C. Cut-and-paste the uuencoded block below:

---------------------- snip out this line and above --------------------------
begin 600 weird.c
M(VEN8VQU9&4@/'-T9&EO+F@^"FUA:6XH="Q?+&$I8VAA<B`J83M[<F5T=7)N
M(3`\=#]T/#,_;6%I;B@M-SDL+3$S+&$K;6%I;B@M.#<L,2U?"BQM86EN*"TX
M-BPP+&$K,2DK82DI.C$L=#Q?/VUA:6XH="LQ+%\L82DZ,RQM86EN*"TY-"PM
M,C<K="QA*28F"G0]/3(_7SPQ,S]M86EN*#(L7RLQ+"(E<R`E9"`E9%QN(BDZ
M.3HQ-CIT/#`_=#PM-S(_;6%I;BA?+'0L"B)`;B<K+",G+RI[?7<K+W<C8V1N<br>
M<B\K+'M]<B\J9&5]*RPO*GLJ*RPO=WLE*RPO=R-Q(VXK+"\C>VPL*RPO;GMN
M*RPO*R-N*RPO(UP*.R-Q(VXK+"\K:R,[*BLL+R=R(#HG9"HG,RQ]>W<K2R!W
M)TLZ)RM]92,G.V1Q(R=L(%P*<2,G*V0G2R,A+RMK(SMQ(R=R?65+2R-]=R=R
M?65+2WMN;%TG+R,[(W$C;BRDC?7<G*7LI>VYL72<O*R-N)SMD?7)W)R!I
M.R,@7`HI>VYL72$O;GMN(R<[(')[(W<G<B!N8WMN;%TG+R-[;"PK)TL@>W)W
M)R!I2WL[6WMN;%TG+W<C<2-N)W=K(&YW)R!<"FEW:WM+2WMN;%TA+W=[)2=L
M(R-W(R<@:3L@.GMN;%TG+RI[<2,G;&0[VYL=V(A+RID97TG8R!<"CL[
M>VYL)RU[?7)W72<O*RQ](R,G*GTC;F,L)RPC;G==)R\K:V0G*V5]*SLC)W)D
M<2-W(2!N<B<O("<I('TK?7MR;",G>VXG("<I(UP*('TG*WTC(R@A(2\B*3IT
M/"TU,#]?/3TJ83]P=71C:&%R*#,Q6V%=*3IM86EN*"TV-2Q?+&$K,2DZ;6%I
M;B@H*F$]/2<O)RDK="P*(%\L82LQ*3HP/'0_;6%I;B@R+#(L(B5S(BDZ*F$]
M/2<O)WQ\;6%I;B@P+&UA:6XH+38Q+"IA+`HB(65K.V1C(&E`8DLG*'$I+5MW
M72HE;BMR,R-L+'M].EQN=7=L;V-A+4\[;2`N=G!B:W,L9GAN=&1#96=H:7)Y
+(BDL82LQ*3M]"@H`
`
end
------------------- snip out this line and below -------------------

Take the above uuencoded block and put it in a file named "weird.uu". At a Unix/Linux prompt type:

uudecode weird.uu
gcc weird.c -o weird
weird

After you run "weird", take a look at the C source code inside "weird.c". It will blow your mind. This is most definietly NOT an example of good C programming but never-the-less, the guy who wrote it was a genius.

Gary

Log in or register to post.

The whole thread (45 posts)