EL2310 Scientific Programming

Similar documents
EL2310 Scientific Programming

EL2310 Scientific Programming

Class Information ANNOUCEMENTS

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

Recitation #11 Malloc Lab. November 7th, 2017

Quiz 0 Review Session. October 13th, 2014

[0569] p 0318 garbage

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

CSC C69: OPERATING SYSTEMS

Lectures 5-6: Introduction to C

CMSC 341 Lecture 2 Dynamic Memory and Pointers

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

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS61, Fall 2012 Section 2 Notes

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Heap Arrays and Linked Lists. Steven R. Bagley

Lecture 2: C Programm

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Lecture 3: C Programm

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

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CS 11 C track: lecture 5

Lectures 5-6: Introduction to C

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CSCI 171 Chapter Outlines

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Vector and Free Store (Pointers and Memory Allocation)

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

SYSC 2006 C Winter 2012

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Pointers and References

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Structures and Pointers

CS24 Week 2 Lecture 1

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

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

High Performance Computing in C and C++

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Chapter 17 vector and Free Store

CSC 1600 Memory Layout for Unix Processes"

Understanding Pointers

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

Chapter 17 vector and Free Store. Bjarne Stroustrup

Chapter 17 vector and Free Store

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

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

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

PRINCIPLES OF OPERATING SYSTEMS

Programming Studio #9 ECE 190

A brief introduction to C programming for Java programmers

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Arrays, Pointers and Memory Management

Memory, Arrays & Pointers

Dynamic Allocation in C

CS 314 Principles of Programming Languages. Lecture 11

Short Notes of CS201

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Lecture 07 Debugging Programs with GDB

Pointers. Memory. void foo() { }//return

Embedded Systems - FS 2018

Intermediate Programming, Spring 2017*

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

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

A Fast Review of C Essentials Part I

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

CS201 - Introduction to Programming Glossary By

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

CS 61C: Great Ideas in Computer Architecture Introduction to C

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Lecture Notes on Types in C

Advanced Pointer Topics

Expressions and Casting

18-600: Recitation #3

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

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Pointers and Memory Management

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 {

CS 11 C track: lecture 6

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

Data Types. Data Types. Integer Types. Signed Integers

Data Representation and Storage. Some definitions (in C)

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

CPSC 213. Introduction to Computer Systems. Static Scalars and Arrays. Unit 1b

Lecture Notes on Memory Management

CS 61c: Great Ideas in Computer Architecture

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

377 Student Guide to C++

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

6.S096: Introduction to C/C++

Lecture Notes on Memory Management

Recitation 2/18/2012

Intro to C: Pointers and Arrays

Transcription:

Lecture 11: Structures and Memory (yaseminb@kth.se)

Overview Overview Lecture 11: Structures and Memory Structures Continued Memory Allocation

Lecture 11: Structures and Memory Structures Continued Memory Allocation

Last time Pointers Started with (struct)

Today Repetition pointers More on (struct) Memory

Pointer A pointer is a variable that holds the address of another variable Ex: int a; int* b = &a; *b = 4; Will set a to be 4

Pointers to pointers Can have pointers to pointer Address of the address to the value Notation similar int a; int *p = &a; int **pp = &p; Example use: Change address of pointer in function Dereferencing: * pp to get pointer to a ** pp to get value of a

void pointer Normal pointers point to a certain type like int The void pointer (void*) represents a general pointer that can point to anything You can assign to and from a void * without a problem You can not dereference a void* The void pointer allows you to write code that can work with addresses to any data type

NULL Bad idea to leave variables uninitialized This is true for pointers as well To mark that a pointer is not assigned and give it a well defined value we use the NULL pointer. Ex: int *p = NULL;... if (p!= NULL) *p = 4; Testing if not NULL before using a pointer is good practice (and setting it to NULL when unassigned)

Pointer to functions Just like in MATLAB you can work with pointers to functions In C you need to declare explicitly what the argument the function has as input and output Ex: Pointer (fcn) to a function that returns an int and takes a double as argument int (*fcn)(double)

Arithmetic operations with pointers Comparison Let int *p1, *p2; What is the difference?... if (p1 == p2)...... if (*p1 == *p2)... Adding/subtracting pointer int *p1, *p2; What does p1+3 mean? What does p2-p1 mean?

Pointers and functions Functions can only have a single return type Scope of argument is local to function Use of pointers for multiple output from function

Task 1 Assign any integer to the closest in the set: {0, 3, 6, 10} 5 2 8 0 3 6 10 Use the above decision tree structure. If greater or equal than the node value, follow right, otherwise, follow left

Task 0 Write a set of void functions that exemplifies, 1. Returning a value through a pointer argument of a function 2. Difference in use between arrays and pointers passed to a function 3. Passing function pointers to functions Use Task1 and Task3 from previous lecture (lecture 10) as examples.

struct So far we looked at basic data types and pointers It is possible to define your own types For this we use a struct Ex: struct complex number { double real; double imag; }; The variables real and imag are called members of the struct complex number. Declaring variables x,y of type complex number is done with struct complex number x,y;

Structures Continued Lecture 11: Structures and Memory Structures Continued Memory Allocation

Structures Continued Assigning struct Can be assign similar to arrays struct complex number x = { 1.1, 2.4 }; Will give the complex number x = 1.1 + 2.4i. One more example: struct person { int age; int sibling ages[10]; char name[32]; }; struct person p1 = {1, {26, 33}, Mr T }; Will assign value to the ages of two siblings

Structures Continued Accessing members of a struct If you want to set/get the value of a member you use the. operator Ex: struct complex number { double real; double imag; }; struct complex number x; x.real = 1.1; x.imag = 2.4;

Structures Continued typedef typedef can be used to give types a new name, like a synonym Can introduce shorter names for things Ex: struct position { double x; double y; }; typedef struct position pos; Alternative: typedef struct { double x; double y; } pos;

Structures Continued 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 Continued 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; };

Structures Continued Structures of structures Continued. struct line l; l.start.x = 4; l.start.y = 6; l.end.x = 2; l.end.y = -1; struct line *lp = &l; lp->start.y = 42;

Structures Continued 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; };

Structures Continued cast Some conversions between types are implicit Ex: double x = 4; In other cases you need to tell the compiler to do this Ex: double fraction = 3 / 4; will give 0 Ex: double fraction = (double)3 / 4; We casted 3 from an int to a double Be careful when casting!

Structures Continued Casting pointers We can convert also between pointer types These are typically allowed with gcc (implicit convertions): int a; char *pa = &a; int *b; char *pb = b; Will generate a warning, use an explicit cast: int a; char *pa = (char*) &a; int *b; char *pb = (char*) b; Be even more careful when casting pointers!

Memory Allocation Lecture 11: Structures and Memory Structures Continued Memory Allocation

Memory Allocation Dynamic allocation of memory Sometimes you do not know the size of arrays when you write code Idea: Allocate memory dynamically This way you can allocate memory at runtime You can calculate how much memory you need and allocate (e.g. array) only then

Memory Allocation 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)); Will allocate memory for 100 ints You can use an explicit cast: int *p = (int*)malloc(100*sizeof(int));

Memory Allocation 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

Memory Allocation 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)

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

Memory Allocation How to Debug C Program using gdb Compile your C program with -g option to allow the compiler to collect the debugging information. Launch gdb with your program e.g.: gdb main Set a breakpoint: break line number Run your program: run Print variable values: print variable name Run step by step: c or continue: continue executing until the next break point. n or next: execute the next line as single instruction. s or step: same as next, but does not treats function as a single instruction, instead goes into the function and executes it line by line.

Lecture 11: Structures and Memory Structures Continued Memory Allocation

Task 2 Define a structure for a complex number Define functions to perform operations on the complex numbers Write a program that uses these functions to compute addition, multiplication, subtraction and division of two complex numbers the magnitude the angle

Task 3 Write a program that investigates how different numbers are represented Define a set of variables short, int, unsigned int,... and print the value of each byte in turn Does your computer use little-endian (least significant byte (LSB) at the lowest address) or big-endian (MSB at the lowest address) representation of numbers