ME964 High Performance Computing for Engineering Applications C Programming Intro Sept. 4, 2008.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Hello World Program The source code #include int main() { printf("Hello World\n"); return(0); }
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
1 Memory Allocation Professor Jennifer Rexford COS 217.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C Tutorial CSU480 Donghui Zhang Adapted from Wei Qian’s C tutorial slides.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Compiler Construction
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Stack and Heap Memory Stack resident variables include:
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
ITEC113Algorithms and Programming Techniques C programming: Variables, Expressions Part I.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C is a high level language (HLL)
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
C/C++ Tutorial.
C Primer.
Pointers and Memory Overview
Lecture 6 C++ Programming
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Memory Allocation CS 217.
Chapter 15 Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
15213 C Primer 17 September 2002.
C Tutorial Adapted from Wei Qian’s C tutorial slides.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

ME964 High Performance Computing for Engineering Applications C Programming Intro Sept. 4, 2008

Before we get started… Last Time Discussed the ME964 syllabus Started quick C Programming Intro Asked you to read chapter 5 (Pointers and Arrays) of Kernighan and Ritchie – very important to have a good understanding of this Today Wrap up the C Programming Intro HW Assigned: HW1, posted on the class bulletin board today HW due on September 11. solutions according to procedure outlined last time 2

A Quick Digression About the Compiler #include /* The simplest C Program */ int main(int argc, char **argv) { printf(“Hello World\n”); return 0; } my_program __extension__ typedef unsigned long long int __dev_t; __extension__ typedef unsigned int __uid_t; __extension__ typedef unsigned int __gid_t; __extension__ typedef unsigned long int __ino_t; __extension__ typedef unsigned long long int __ino64_t; __extension__ typedef unsigned int __nlink_t; __extension__ typedef long int __off_t; __extension__ typedef long long int __off64_t; extern void flockfile (FILE *__stream) ; extern int ftrylockfile (FILE *__stream) ; extern void funlockfile (FILE *__stream) ; int main(int argc, char **argv) { printf(“Hello World\n”); return 0; } Compilation occurs in two steps: “Preprocessing” and “Compiling” In Preprocessing, source code is “expanded” into a larger form that is simpler for the compiler to understand. Any line that starts with ‘#’ is a line that is interpreted by the Preprocessor. Include files are “pasted in” (#include) Macros are “expanded” (#define) Comments are stripped out ( /* */, // ) Continued lines are joined ( \ ) Preprocess Compile The compiler then converts the resulting text into binary code the CPU can execute. 3

Today’s Objective Concentrate on the concept of pointer To discuss pointers, we need to talk a bit about the concept of memory Keep this in mind, otherwise you won’t see the forest for the trees We’ll conclude by touching on a couple of other C elements: Arrays, typedef, and structs 4

The “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 [10] int float int64_t a single character (1 slot) an array of 10 characters signed 4 byte integer 4 byte floating point signed 8 byte integer AddrValue ‘H’ (72) 5‘e’ (101) 6‘l’ (108) 7 8‘o’ (111) 9‘\n’ (10) 10‘\0’ (0)

What is a Variable? char x; char y=‘e’; A Variable names a place in memory where you store a Value of a certain Type. SymbolAddrValue x4 Some garbage y5‘e’ (101) You first Declare a variable by giving it a name and specifying its type and optionally an initial value declare vs. define Type is single character (char) extern? static? const? Name What names are legal? Initial value Variable x declared but undefined The compiler puts x and y somewhere in memory. symbol table? 6

Multi-byte Variables char x; char y=‘e’; int z = 0x ; 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) SymbolAddrValue x4 Some garbage y5‘e’ (101) 6 7 z x means the constant is written in hex An int requires 4 bytes padding 7

Memory, 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 in 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 smallest byte. word 0 word 1 word Stack Heap 8

Example … V1 V2 V3 V

Another Example What is the value of: - sizeC - sizeD - sizeDarr #include 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; } 10 NOTE: sizeof is a compile-time operator that returns the size, in multiples of the size of char, of the variable or parenthesized type-specifier that it precedes.

Can a function modify its arguments? What if we wanted to implement a function pow_assign() that modified its argument, so that these are equivalent: float p = 2.0; /* p is 2.0 here */ pow_assign(p, 5); /* p is 32.0 here */ float p = 2.0; /* p is 2.0 here */ p = pow(p, 5); /* p is 32.0 here */ void pow_assign(float x, uint exp) { float result=1.0; int i; for (i=0; (i < exp); i++) { result = result * x; } x = result; } Would this work? 11

In C you can’t change the arguments of a function call… void pow_assign(float x, uint exp) { float result=1.0; int i; for (i=0; (i < exp); i++) { result = result * x; } x = result; } // a code snippet that uses above // function { float p=2.0; pow_assign(p, 5); // the value of p is 2 here… } In C, all arguments are passed by value But, what if the argument is the address of a variable? 12

The Concept of 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… 13

Pointers (cont.) The need for pointers In instances where you want to modify a variable (its value) inside a function The pointer is passed to that function as an argument Passing large objects to functions without the overhead of copying them first Accessing memory allocated on the heap Referring to functions 14

Pointer Validity A Valid pointer is one that points to memory that your program controls. Using invalid pointers will cause non-deterministic behavior Very often the code will crash with a SEGV, that is, Segment Violation, or Segmentation Fault. There are two general causes for these errors: Coding errors that end up setting the pointer to a strange number Use of a pointer that was at one time valid, but later became invalid char * get_pointer() { char x=0; return &x; } { char * ptr = get_pointer(); *ptr = 12; /* valid? */ } Will ptr be valid or invalid? 15 Good practice: Initialize pointers to 0 (or NULL). NULL is never a valid pointer value, but it is known to be invalid and means “no pointer set”.

Answer: No, it’s invalid… A pointer to a variable allocated on the stack becomes invalid when that variable goes out of scope and the stack frame is “popped”. The pointer will point to an area of the memory that may later get reused and rewritten. char * get_pointer() { char x=0; return &x; } int main() { char * ptr = get_pointer(); *ptr = 12; /* valid? */ other_function(); return 0; } But now, ptr points to a location that’s no longer in use, and will be reused the next time a function is called! 16 Here is what I get in DevStudio when compiling: 1> c:\cygwin\me964\fall2008\driver.cpp(5) : warning C4172: returning address of local variable or temporary

Example: What gets printed out? int main() { int d; char c; short s; int* p; int arr[2]; printf( “%p, %p, %p, %p, %p\n”,&d, &c, &s, &p, arr ); return 0; } d c s p arr NOTE: Here &d = 920 (in practice a 4- byte hex number such as 0x22FC3A08) 17 Q: What does get printed out by the printf call in the code snippet above?

Example: Usage of pointer int main() { int d; char c; short s; int* p; int arr[2]; p = &d; *p = 10; c = (char)1; p = arr; *(p+1) = 5; p[0] = d; *( (char*)p + 1 ) = c; return 0; } d c s p arr[1] arr[0] = 920 = 10 =

Example: Use of pointers p = &d; *p = 10; c = (char)1; p = arr; *(p+1) = 5; // int* p; p[0] = d; *( (char*)p + 1 ) = c; d c s p arr[1] arr[0] = 904 = 10 = = 5 = Question: arr[0] = ?

Pass pointer parameters into function 20 Use of pointers, another example… What will happen here? void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } int a = 5; int b = 6; swap(&a, &b); int * a; int * b; swap(a, b);

Allows the program to determine how much memory it needs at run time and to allocate exactly the right amount of storage. It is your responsibility to clean up after you (free the dynamic memory you allocated) The region of memory where dynamic allocation and deallocation of memory can take place is called the heap. 21 Dynamic Memory Allocation (Allocation on the Heap)

Functions that come into play in conjunction with dynamic memory allocation An example of dynamic memory allocation 22 Dynamic Memory Allocation (cont.) int * ids; //id arrays int num_of_ids = 40; ids = (int*) malloc( sizeof(int) * num_of_ids); … do your work… free(ids); void *malloc(size_t number_of_bytes); // allocates dynamic memory size_t sizeof(type); // returns the number of bytes of type void free(void * p) // releases dynamic memory allocated

Example of using pointers (cont.) p = (int*) malloc(sizeof(int)*3); p[2] = arr[1] * 3; s = (short)( *(p+2) ); free( p ); d c s p arr[1] arr[0] = 904 = 10 = = 5 = Assumption: we have the same memory layout like in the previous example

d c s p arr[1] arr[0] = 932 = 10 = = 5 = 266 p[2] = 15 = 15 Q: what if you say p[2]=0 after you free the memory? A: run time error. Q: what if you do not call free(p)? A: memory leak. 24 p = (int*) malloc(sizeof(int)*3); p[2] = arr[1] * 3; s = (short)( *(p+2) ); free( p ); Example of using pointers (cont.) Q: what will be the value of “s”, and why?

d c s p arr[1] arr[0] = 932 = 10 = = 5 = 266 p[2] = = Q: what is the value of “s” now, and why? A: 25 int dummy = p = (int*) malloc(sizeof(int)*3); p[2] = dummy * 3; s = (short)( *(p+2) ); free( p ); Example of using pointers (cont.) p,3932int * [0] int [1] int [2] int s-26245short

Moving on to other topics… What comes next: Creating logical layouts of different types (structs) Creating new types using typedef Using arrays Parsing C type names 26

Data Structures A data structure is a collection of one or more variables, possibly of different types. An example of student record 27 struct StudRecord { char name[50]; int id; int age; int major; };

A data structure is also a data type Accessing a field inside a data structure 28 Data Structures (cont.) struct StudRecord my_record; struct StudRecord * pointer; pointer = & my_record; my_record.id = 10; \\ or pointer->id = 10;

Allocating a data structure instance NOTE: Never calculate the size of data structure yourself. Rely on the sizeof() function. 29 Data Structures (cont.) struct StudRecord* pStudentRecord; pStudentRecord = malloc(sizeof(struct StudRecord)); pStudentRecord ->id = 10;

The “typedef” concept struct StudRecord { char name[50]; int id; int age; int major; }; typedef struct StudRecord RECORD; int main() { RECORD my_record; strcpy_s(my_record.name, “Joe Doe"); my_record.age = 20; my_record.id = 6114; RECORD* p = &my_record; p->major = 643; return 0; } Using typedef to improve readability… 30

Arrays Arrays in C are composed of a particular type, laid out in memory in a repeating pattern. Array elements are accessed by stepping forward in memory from the base of the array by a multiple of the element size. /* define an array of 10 chars */ char x[5] = {‘t’,’e’,’s’,’t’,’\0’}; /* accessing element 0 */ x[0] = ‘T’; /* pointer arithmetic to get elt 3 */ char elt3 = *(x+3); /* x[3] */ /* x[0] evaluates to the first element; * x evaluates to the address of the * first element, or &(x[0]) */ /* 0-indexed for loop idiom */ #define COUNT 10 char y[COUNT]; int i; for (i=0; i<COUNT; i++) { /* process y[i] */ printf(“%c\n”, y[i]); } Brackets specify the count of elements. Initial values optionally set in braces. Arrays in C are 0-indexed (here, 0..9) x[3] == *(x+3) == ‘t’ (notice, it’s not ‘s’!) SymbolAddrValue char x [0]100‘t’ char x [1]101‘e’ char x [2]102‘s’ char x [3]103‘t’ char x [4]104‘\0’ Q: What’s the difference between char x[5] and a declaration like char *x? For loop that iterates from 0 to COUNT-1. 31

How to Parse and Define C Types At this point we have seen a few basic types, arrays, pointer types, and structures. So far we’ve glossed over how types are named. int x; /* int; */ typedef int T; int *x; /* pointer to int; */ typedef int *T; int x[10]; /* array of ints; */ typedef int T[10]; int *x[10]; /* array of pointers to int; */ typedef int *T[10]; int (*x)[10]; /* pointer to array of ints; */ typedef int (*T)[10]; C type names are parsed by starting at the type name and working outwards according to the rules of precedence: int (*x)[10]; x is a pointer to an array of int int *x[10]; x is an array of pointers to int Arrays are the primary source of confusion. When in doubt, use extra parens to clarify the expression. typedef defines a new type 32