DOC PREVIEW
UW-Madison ME 964 - Quick Overview of C Programming

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 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 32 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 32 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 32 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 32 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 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ME964High Performance Computing for Engineering Applications“There is no reason for any individual to have a computer in their home.” Ken Olson, president and founder, Digital Equipment Corporation, 1977.© Dan Negrut, 2012ME964 UW-MadisonQuick Overview of C ProgrammingJanuary 26, 2012Before We Get Started… Last time Course logistics & syllabus overview Today Quick overview of C Programming Essential reading: Chapter 5 of “The C Programming Language” (Kernighan and Ritchie) Acknowledgement: Slides on this C Intro include material due to Donghui Zhang and Lewis Girod First assignment will be emailed to you later today Due one week from today, at 11:59 PM Correction: Course website is http://sbel.wisc.edu/Courses/ME964/2012/2The “memory”Memory: similar to a big table of numbered slots where bytes of data are stored.The number of a slot is its Address.One byte Value can be stored in each slot.Some data values span more than one slot, like the character string “Hello\n”A Type provides a logical meaning to a span of memory. Some simple types are:char char char char char [10]char [10]char [10]char [10]intintintintfloatfloatfloatfloatint64_tint64_tint64_tint64_ta single character (1 slot)an array of 10 characterssigned 4 byte integer4 byte floating pointsigned 8 byte integerAddr Value01234 ‘H’ (72)5 ‘e’ (101)6 ‘l’ (108)7 ‘l’ (108)8 ‘o’ (111)9 ‘\n’ (10)10 ‘\0’ (0)11123What is a Variable?char x;char x;char x;char x;char y=‘e’;char y=‘e’;char y=‘e’;char y=‘e’;A Variable names a place in memory where you store a Value of a certain Type.Symbol Addr Value0123x 4Somegarbagey 5 ‘e’ (101)6789101112You first Declare a variable by giving it a name and specifying its type and optionally an initial valuedeclare vs. defineType is single character (char)extern? static? const?NameWhat names are legal?Initial valueVariable x declared but undefinedThe compiler puts x and y somewhere in memory.symbol table?4Multi-byte Variableschar x;char x;char x;char x;char y=‘e’;char y=‘e’;char y=‘e’;char y=‘e’;int z = 0x01020304; int z = 0x01020304; int z = 0x01020304; int z = 0x01020304; Different types require different amounts of memory. Most architectures store data on “word boundaries”, or even multiples of the size of a primitive data type (int, char)Symbol Addr Value0123x 4Some garbagey 5 ‘e’ (101)67z 8 49 310 211 1120x means the constant is written in hexAn int requires 4 bytespadding5In this picture, the architecture uses little-endian convention, since it stores the most significant byte firstMemory, a more detailed view… A sequential list of words, starting from 0. On 32bit architectures (e.g. Win32): each word is 4 bytes. Local variables are stored on the stack Dynamically allocated memory is set aside on the heap (more on this later…) For multiple-byte variables, the address is that of the least significant byte (little endian).6048StackHeapword 0word 1word 2Example…+3 +2 +1 +0V1V2V3V49009049089129169209249289329369409447Another ExampleWhat is the value of:- sizeC- sizeD- sizeDarr#include <iostream>int main() {char c[10];int d[10];int* darr;darr = (int *)(malloc(10*sizeof(int)));size_t sizeC = sizeof(c);size_t sizeD = sizeof(d);size_t sizeDarr = sizeof(darr);free(darr);return 0;}8NOTE: sizeof is a compile-time operator that returns the size, in multiples ofthe size of char, of the variable or parenthesized type-specifier that it precedes.Can a C function modify its arguments?What if we wanted to implement a function pow_assign() that modified its argument? Are these are equivalent?float p = 2.0;float p = 2.0;float p = 2.0;float p = 2.0;/* p is 2.0 here *//* p is 2.0 here *//* p is 2.0 here *//* p is 2.0 here */pow_assignpow_assignpow_assignpow_assign(p, 5);(p, 5);(p, 5);(p, 5);/* /* /* /* Is p Is p Is p Is p is 32.0 here is 32.0 here is 32.0 here is 32.0 here ? */? */? */? */float p = 2.0;float p = 2.0;float p = 2.0;float p = 2.0;/* p is 2.0 here *//* p is 2.0 here *//* p is 2.0 here *//* p is 2.0 here */p = p = p = p = powpowpowpow(p, 5);(p, 5);(p, 5);(p, 5);/* /* /* /* p p p p is 32.0 here is 32.0 here is 32.0 here is 32.0 here */*/*/*/void pow_assign(float x, uint exp)void pow_assign(float x, uint exp)void pow_assign(float x, uint exp)void pow_assign(float x, uint exp){{{{float result=1.0;float result=1.0;float result=1.0;float result=1.0;int i;int i;int i;int i;for (i=0; (i < exp); i++) {for (i=0; (i < exp); i++) {for (i=0; (i < exp); i++) {for (i=0; (i < exp); i++) {result = result * x;result = result * x;result = result * x;result = result * x;}}}}x = result;x = result;x = result;x = result;}}}}Would this work?9????Native function, to use you need #include <math.h>In C you can’t change the value of any variable passed as an argument in a function call… void pow_assign(float x, uint exp)void pow_assign(float x, uint exp)void pow_assign(float x, uint exp)void pow_assign(float x, uint exp){{{{float result=1.0;float result=1.0;float result=1.0;float result=1.0;int i;int i;int i;int i;for (i=0; (i < exp); i++) {for (i=0; (i < exp); i++) {for (i=0; (i < exp); i++) {for (i=0; (i < exp); i++) {result = result * x;result = result * x;result = result * x;result = result * x;}}}}x = result;x = result;x = result;x = result;}}}}// a code snippet that uses above // a code snippet that uses above // a code snippet that uses above // a code snippet that uses above // function// function// function// function{{{{float p=2.0;float p=2.0;float p=2.0;float p=2.0;pow_assign(p, 5);pow_assign(p, 5);pow_assign(p, 5);pow_assign(p, 5);// the value of p is 2 here…// the value of p is 2 here…// the value of p is 2 here…// the value of p is 2 here…}}}}In C, all arguments are passed by valueBut, what if the argument is the address of a variable? 10Keep in mind: pass by value requires the variable to be copied. That copy is then passed to the function. Sometime generating a copy can be expensive…C Pointers What is a pointer? A variable that contains the memory address of another variable or of a function In general, it is safe to assume that on 32 bit architectures pointers occupy one word Pointers to int, char, float, void, etc. (“int*”, “char*”, “*float”, “void*”), they all occupy 4 bytes (one word). Pointers: *very* many bugs in C programs are traced back to mishandling of pointers…11Pointers (cont.) The need for pointers


View Full Document

UW-Madison ME 964 - Quick Overview of C Programming

Documents in this Course
Load more
Download Quick Overview of C Programming
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 Quick Overview of C Programming 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 Quick Overview of C Programming 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?