EL2310 Scientific Programming

Similar documents
EL2310 Scientific Programming

EL2310 Scientific Programming

PRINCIPLES OF OPERATING SYSTEMS

CSC C69: OPERATING SYSTEMS

File IO and command line input CSE 2451

High Performance Programming Programming in C part 1

Computers Programming Course 5. Iulian Năstac

25.2 Opening and Closing a File

Recitation 2/18/2012

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

File Handling in C. EECS 2031 Fall October 27, 2014

CSC 1107: Structured Programming

CSC 1107: Structured Programming

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

Computer Organization & Systems Exam I Example Questions

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

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

Programming in C Quick Start! Biostatistics 615 Lecture 4

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

Final CSE 131B Spring 2004

Heap Arrays. Steven R. Bagley

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

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

Programming refresher and intro to C programming

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

CSE 333 Lecture 2 Memory

C programming basics T3-1 -

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

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

CS24 Week 2 Lecture 1

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Lab # 4. Files & Queues in C

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

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Lesson #8. Structures Linked Lists Command Line Arguments

Arithmetic Operators. Portability: Printing Numbers

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

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

Course organization. Course introduction ( Week 1)

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

A Fast Review of C Essentials Part I

CSE 374 Programming Concepts & Tools

Today s Learning Objectives

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

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

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

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

Stream Model of I/O. Basic I/O in C

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C-Refresher: Session 10 Disk IO

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Chapter 7. Basic Types

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

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

More on C programming

OBJECT ORIENTED PROGRAMMING

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

211: Computer Architecture Summer 2016

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

Heap Arrays and Linked Lists. Steven R. Bagley

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

C++ Important Questions with Answers

ECEN 449 Microprocessor System Design. Review of C Programming

Lecture 02 C FUNDAMENTALS

Subject: Fundamental of Computer Programming 2068

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

C Language Programming

Lecture 04 Introduction to pointers

Reserved Words and Identifiers

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Programming Language Basics

LSN 3 C Concepts for OS Programming

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

BSM540 Basics of C Language

Bitwise Operator and Typecasting

A brief introduction to C programming for Java programmers

Memory Allocation in C

Data Types. Data Types. Integer Types. Signed Integers

ENG120. Misc. Topics

Memory Management. CSC215 Lecture

EECE.2160: ECE Application Programming Spring 2018 Exam 3 May 10, 2018

Functions in C C Programming and Software Tools

CSCI 171 Chapter Outlines

Lecture 3: C Programm

Dynamic memory allocation (malloc)

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

Transcription:

Lecture 11: Memory, Files and Bitoperations (yaseminb@kth.se)

Overview Overview Lecture 11: Memory, Files and Bit operations Main function; reading and writing Bitwise Operations

Lecture 11: Memory, Files and Bit operations Main function; reading and writing Bitwise Operations

Last time Complex data structures (struct) Memory

Today More on Memory Reading/writing files Bitwise operations

Pointers and structures You can use pointers to structures Ex: struct complex number x; struct complex number *xptr = &x; To access a member using a pointer we use the > operator Ex: xptr->real = 2; Same as (*xptr).real = 2; and x.real = 2;

Structures of structures You can have any number of levels of structures of structures Ex: struct position { double x; double y; }; struct line { struct position start; struct position end; };

Pointers to structures in structures Normally you need to declare a type before you use it. You can have a pointer to the structure you define Ex: struct person { char name[32]; struct person *parent; };

cast Some conversions between types are implicit Ex: double x = 4; (cast from int to double) In other cases you need to tell the compiler to do this Ex: int a = (int)4.2; (will truncate to 4) Often used together with pointers Ex: int a; unsigned char *byte = (unsigned char*)&a;

Dynamic allocation of memory Sometimes you do not know the size of arrays etc. Idea: Allocate memory dynamically This way you can allocate memory at runtime

malloc Allocate memory with malloc Need to #include<stdlib.h> This function returns a pointer of type void* Ex: int *p = malloc(100*sizeof(int)); To avoid warnings, add explicit cast Ex: int *p = (int *)malloc(100*sizeof(int)); Will allocate memory for 100 ints

free You should free the memory that you no longer need!!! Ex: int *p = (int *)malloc(100*sizeof(int));... free(p); If you do not free allocated memory you will get memory leaks Your program will crash eventually A big problem if you program should run a very long time

Common mistakes Forgetting to free memory (memory leak!!!) Using memory that you have not initialized Using memory that you do not own Using more memory than you allocated Returning pointer to local variable (thus no longer existing)

Tip when using dynamic memory allocation If you have a malloc think about where the corresponding free is

Main function; reading and writing Lecture 11: Memory, Files and Bit operations Main function; reading and writing Bitwise Operations

Main function; reading and writing Command line arguments You add parameters to the main function int main(int argc, char **argv) First argument is in argv[1], argv[0] contains program name atoi and atof are useful to get number from char arrays Ex: int value;... if (argc > 1) value = atoi(argv[1]); else value = 42;

Main function; reading and writing Reading from the keyboard Can use char getchar(); to get a single character For more more complex input try scanf(...) which is the dual of printf(...) The arguments for scanf the same as for printf except that it wants pointers to where to put the data Ex: int i; double num[3]; printf("enter 3 number: "); for (i = 0; i < 3; i++) { scanf("%lf", &num[i]); }

Main function; reading and writing Opening/closing a file FILE *fopen(char *path, char *mode); mode is r : read, w : write, a :append,... On success returns pointer to file descriptor, else NULL fclose(file*);

Main function; reading and writing Writing to a file Write to the file with for example fprintf(file*,...); Ex: double x=1, y=2, theta=0.5; FILE *fd = NULL; fd = fopen("test.txt", w ); fprintf(fd, "Robot pose is %f %f %f\n", x,y,theta); fclose(fd);

Main function; reading and writing Reading from a file Read from the file with for example fscanf(file*,...); Ex: double x,y,theta; FILE *fd = NULL; fd = fopen("test.txt", "r"); fscanf(fd, "Robot pose is %lf %lf %lf\n", &x,&y,&theta); fclose(fd); Notice that you need %lf when you read a double, %f for a float Function sscanf() is similar but operates on a char array instead of a file

Bitwise Operations Lecture 11: Memory, Files and Bit operations Main function; reading and writing Bitwise Operations

Bitwise Operations Bitwise operations When programming at low level, bitwise operations are common Also, if you want to store flags it is very wasteful to use 1 byte for every flag that can only be 0 or 1. Typical construction, use bitmask Let each bit in the variable be one flag

Bitwise Operations Bitwise operator & bitwise AND bitwise inclusive OR ˆ bitwise exclusive OR << left shift >> right shift bitwise NOT

Bitwise Operations Example of bit operations mask = mask & 0xF Set all but the lower 4 bits to zero (0xF = 1111) mask = mask 0x3 Set lower 2 bits 0x3 = 11 short value;... unsigned char lower = (value & 0xFF); (0xFF = 11111111) unsigned char upper = (value >> 8); What is printed? int x = 1, y = 2; if (x && y) printf("case 1\n"); if (x & y) printf("case 2\n");

Bitwise Operations Shift operators Should primarily be used on unsigned data types Shifting results in division (right) and multiplication (left) of integers by 2 times the number of shifts