DOC PREVIEW
CORNELL CS 404 - Lecture Slides

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1OutlineOutline• Announcements– HWI due on Friday• Differences between FORTRAN and C• Calling FORTRAN from CComparing C and FORTRANComparing C and FORTRANFORTRANFORTRAN• FORmula TRANslator– One of the first programming languages– Most common strain was standardized in 1977– Designed for “Scientific Computing” (e.g. physics)– complex type fully implemented, integrated– lots of legacy code– simple– fast!2• F77 is ancient– Missing “modern” features like pointers, novel datastructures (or even records)– Missing not-so-modern features like recursion!– Encourages bad programming:• heavy use of goto-statement• common blocksFORTRAN: DisadvantagesFORTRAN: Disadvantages• In many ways, C is similar to FORTRAN– Procedural– few built-in types and data structures• But more modern– pointers--allows you to manipulate memory directly– structs--allows you to implement records– Together, pointers and structs allow you to createnew data structures– supports recursion– can do everything you could ever want to do (math, CS,graphics)CCC: Key disadvantagesC: Key disadvantages• Programming with pointers can be complicatedand even dangerous• No complex type or functions• LESS LEGACY CODE!– Calling this old code from C would allow us to havethe best of both worlds!3Calling FORTRAN from CCalling FORTRAN from C• In theory, we should be able to– Compile FORTRAN code to object code (-c option)– Compile C code to object code– Link objects together• However, there are a few wrinkles:– Namespace problem• C needs to refer to the routines using the correct names• ANSI C code needs prototypes– Call-by-value problem• C can use call-by-value, FORTRAN uses only call-by-reference• In general, need to make sure we’re sending the FORTRANroutines the type of data they expectNamespace ProblemNamespace Problem• The section of a .o file for a specific routine isgiven a name.• The name is used by the linker to figure outhow the executable is put together• We must ensure that calls to FORTRANroutines in C object code use the same nameas in the FORTRAN .o fileNamespace ProblemNamespace Problem• Routine FooBar in a FORTRAN .o file could be– FooBar_– FOOBAR_– foobar_ (g77)• To call FooBar from C, you will need to use thecorrect case and add the underscore– Some compilers provide a -f option which forces thenames in the .o to be all lower case• CAUTION: Every system/compiler is different!Read the documentation!4Call-by-Value ProblemCall-by-Value Problem• In C, a variable can be passed to a subroutineby value or reference.– call-by-value: the number stored in the variable ispassed to the subroutine. The value in the callingroutine WILL NOT CHANGE!• int m = 4• Foo(m); /* m won’t change */• prototype for Foo:– void Foo (int m);– call-by-reference: the memory address is passed. Ifthe subroutine modifies the value, the value WILLCHANGE in the calling routine.• Use “&” to pass a scalar by value:– Foo(&m) /* m might change */– prototype for Foo:» void Foo(int *m); /* “*”==pointer */• Arrays are already pointers, so they are automaticallypassed by reference:– int m[10],tot;– tot=SumArray(m,10);– prototype for SumArray:» int Foo(int *m, int n); /* n=length(m) */Call-by-reference:Call-by-reference:Type EquivalencesType Equivalencesstruct dcomp{ double real; double cmplx;};struct dcomp c;complex *16 cdoubledouble (real *8)floatreal (real *4)intinteger (integer *4)char c[n]character*n cCFORTRAN5• 1D arrays of equivalent type are representedidentically by C and FORTRAN– not true for multidimensional arraysMultidimensional ArraysMultidimensional Arrays654321654321635241A=C: Column-majorFORTRAN:


View Full Document

CORNELL CS 404 - Lecture Slides

Download Lecture Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?