High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Similar documents
High-performance computing and programming I. Introduction to C programming in *NIX

Kurt Schmidt. October 30, 2018

Arrays and Memory Management

Contents. A Review of C language. Visual C Visual C++ 6.0

Memory Corruption 101 From Primitives to Exploit

Lecture 07 Debugging Programs with GDB

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

High Performance Programming Programming in C part 1

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Arrays and Pointers in C. Alan L. Cox

211: Computer Architecture Summer 2016

ECE 15B COMPUTER ORGANIZATION

ch = argv[i][++j]; /* why does ++j but j++ does not? */

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Pointers, Arrays, Memory: AKA the cause of those Segfaults

CS 0449 Sample Midterm

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Fundamental of Programming (C)

Array Initialization

EL2310 Scientific Programming

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Today s class. Review of more C Operating system overview. Informationsteknologi

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

EL2310 Scientific Programming

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Pointers and File Handling

Recitation 2/18/2012

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

EL2310 Scientific Programming

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Lesson #8. Structures Linked Lists Command Line Arguments

Dynamic memory allocation (malloc)

Processes. Johan Montelius KTH

Class Information ANNOUCEMENTS

SYSC 2006 C Winter 2012

A process. the stack

Review Topics. Final Exam Review Slides

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Intermediate Programming, Spring 2017*

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Lectures 5-6: Introduction to C

Dynamic memory allocation

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

C for C++ Programmers

PRINCIPLES OF OPERATING SYSTEMS

In Java we have the keyword null, which is the value of an uninitialized reference type

CS201 Lecture 2 GDB, The C Library

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

CS 11 C track: lecture 5

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Quick review pointer basics (KR ch )

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Systems Programming and Computer Architecture ( )

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

211: Computer Architecture Summer 2016

Memory (Stack and Heap)

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

FUNCTIONS POINTERS. Pointers. Functions

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

CS C Primer. Tyler Szepesi. January 16, 2013

Programming. Pointers, Multi-dimensional Arrays and Memory Management

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

CS 314 Principles of Programming Languages. Lecture 11

Lectures 13 & 14. memory management

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS61, Fall 2012 Section 2 Notes

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

C programming basics T3-1 -

COMP 2355 Introduction to Systems Programming

Lecture 04 Introduction to pointers

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Arrays and Pointers (part 2) Be extra careful with pointers!

CS 261 Fall Mike Lam, Professor. Structs and I/O

CSE 303: Concepts and Tools for Software Development

CS 61c: Great Ideas in Computer Architecture

Arrays and Pointers (part 2) Be extra careful with pointers!


Intermediate Programming, Spring 2017*

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

377 Student Guide to C++

Memory Allocation in C

Transcription:

High-performance computing and programming Intro to C on Unix/Linux IT Uppsala universitet

What is C? An old imperative language that remains rooted close to the hardware C is relatively small and easy to understand C is a nice language for writing small programs or kernels in larger programs C is not C++ C++ is modern in ways that C is not C++ is useful in ways that C is not

Additional material Google is your friend. Want more practice with C? Translate an old Matlab code or something from an other course.

Hello World #include <stdio.h> // provides printf() // This function prints Hello, World! int main( int argc, char** argv ) { printf("hello, World! %d arguments\n", argc - 1); printf( My name is %s\n, argv[0]); return 0; }

Function declaration/definition // Declaration: tells compiler how this function // will be used. // *Must* come before function is called. // Often placed in a.h file. double random(double min, double max); // Definition: what the function does when called. // Placed in.c file, turned into object code by compiler. double random(double min, double max) { double r = (double)rand()/rand_max; r = r*(max-min)+min; return r; }

Input & output in C Command line arguments are handled via arguments to main() The library <stdio.h> handles: Reading from files or keyboard (stdin) e.g. fscanf, scanf Writing to files or terminal (stdout) e.g. fprintf, printf Convert from string to numbers: <stdlib.h>: atoi, atof

Using printf() printf( double=%f\n, double); printf( in file %s: line %d\n, FILE, LINE ); printf( array starts at %p, last element is at %p\n, array, &array[n-1]);

Variable scope in C A variable exists within its set of {} int myglobal; int main(int argc, char** argv){ int myvar; if(argc > 1){ int myvar = argc; } printf( argc > 1? %d\n, myvar); }

Pointers in C A pointer is a variable that contains a memory address It is an integer double foo = 5.5; double * foo_ptr = &foo; //address of foo double foo2 = *foo_ptr; //pointer dereference Use pointers to pass data by reference BEWARE OF SCOPE!

Pointers to stack variables int * dont_do_this{} { int a; a = 5; return &a; } What happens when you try to use the value returned by dont_do_this?

Memory layout The virtual memory map of a running process:

The Stack Heap vs Stack Call a function, stuff pops on the stack Return a function, stuff pops off the stack Local variables vanish at the end of their scope The Heap User-managed Allocated with malloc() or calloc() Stays around until free() is called #1 cause of programmer error

Segmentation faults A program tries to access a forbidden segment of memory triggers the signal SIGSEGV Easiest to debug with a debugger (e.g. gdb) Common causes: Using uninitialized pointers Array out of bounds Using a freed pointer Dereferencing NULL pointers

Arrays C has static and dynamic arrays //static: double a[3] = {0.3, 0.4, 0.5}; //dynamic: double *b; b = (double*) malloc(3*sizeof(double)); b[1] = a[0]; printf( %f\n,b[1]); free(b);

Pointer arithmetic double *a; a = (double*) malloc(3*sizeof(double)); for(i = 0; i < 3; i++) a[i] = i; double *b = a; for(i = 0; i < 3; i++){ printf( %f\n, *b); b++; }

Arrays vs Pointers int static[3] = {0,2,4}; int *dynamic = (int*) malloc(3*sizeof(int)); printf( %d\n,sizeof(static)); // 12 printf( %d\n,sizeof(dynamic)); // 4 /* dynamic = static; // legal static = dynamic; // illegal dynamic++; // legal static++; // illegal */

On the stack: 2D arrays double array[3][4]; On the heap: int rows = 3, columns = 4; double **array; array = (double**)malloc(rows*sizeof(double*)); for( int i=0; i<rows; i++) { array[i]=(double*)malloc(columns*sizeof(double)); }

2D arrays, contiguous int rows = 5, columns = 6; double * data; data = malloc(rows*columns*sizeof(double)); double ** array; array = (double **)malloc(rows*sizeof(double*)); for( int i=0; i<rows; i++) { } array[i] = &data[i*columns];

Structures & typedef typedef struct point { double a, b; } point_t; struct triangle { point_t a, b, c; }; int main(){ } struct triangle my_triangle; my_triangle.a.a = 1.0;

Bitwise operators char a = 1; // 0000 0001 char b = a << 2; // 0000 0100 b == 4; // TRUE char c = a & b; // 0000 0000 c = a b; // 0000 0101 c == 5; // TRUE char d = ~c; // 1111 1010 c = c 2; // 0000 0111 d = d & ~(1<<3); // 1111 0010 char e = c ^ d; // 1111 0101

Assert #include <assert.h> assert( false-if-bug ) Asserts are used to debug as you code. When you write a function, think about: Invariants: properties of a data structure that must be guaranteed Pre-conditions: what is true at the start of a function Post-conditions: what is true at the end of a function When you re happy with your bug-free code, compile with -DNDEBUG This automatically removes all assert statements!