. Plab – Tirgul 5 pointers to pointers, libraries.

Slides:



Advertisements
Similar presentations
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
Programming Languages and Paradigms The C Programming Language.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
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:
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Informática II Prof. Dr. Gustavo Patiño MJ
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
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.
Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.
Plab – Tirgul 3 Makefiles, Libraries, Debugging and Common Bugs.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Outline Midterm results Static variables Memory model
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
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.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
ECE Application Programming
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
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.
Today’s Material Strings Definition Representation Initialization
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.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
S -1 Library Functions. S -2 Standard Libraries Any system call is not part of the C language definition Such system calls are defined in libraries, identified.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Object Oriented Programming Lecture 2: BallWorld.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Multiple file project management & Makefile
Pointers to pointers & multi-dimensional arrays
Pointers verses Variables
Dynamic Memory Allocation
Memory Allocation CS 217.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
C Programming Lecture-8 Pointers and Memory Management
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
Presentation transcript:

. Plab – Tirgul 5 pointers to pointers, libraries

Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int *ip1 = &i; (5)int *ip2 = &j; ipp: (6)int **ipp = &ip1;

Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: ipp: (4)int *ip1 = &i; (5)int *ip2 = &j; (6)int **ipp = &ip1; (7)ipp = &ip2;

Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: ipp: (4)int *ip1 = &i; (5)int *ip2 = &j; (6)int **ipp = &ip1; (7)ipp = &ip2; (8)*ipp = &k;

pointers to pointers: example //put pointer to an allocated string in pp (1)int allocString(int len, char ** pp) { (2)char *str = malloc(len + 1); (3)if (str==NULL) (4)return -1; (5)*pp = str; (6)return 0; (7)} // copy a string using “allocString” (8)void main() { (9)char *s = “example”; (10)char *copy = NULL; (11)allocString( strlen(s), &copy ); (12)strcpy( copy, string); (13)}

Multi-dimensional arrays Can be created in few ways: 1. Automatically: u int arr[5][7];  3 rows, 5 columns  continuous memory (divided to 5 blocks)  access: arr[row][col] = 0; u When sending ‘arr’ as an argument to a function, only the 1 st index can be omitted:  void func( int x[5][7] ) //ok  void func( int x[][7] ) //ok  void func( int x[][] ) //error (always)  void func( int * x[] ) //error  void func( int ** x ) //error

Multi-dimensional arrays 2. Semi-dynamic: u Define an array of pointers: int *pa[5]; // allocates memory for 5 pointers for (i=0; i<5; i++) { pa[i] = (int*) malloc( 7*sizeof(int) ); // pa[i] now points to a memory for 7 ints }

Multi-dimensional arrays 3. Dynamically: (1)int ** array; (2)array = (int**) malloc( 5*sizeof(int*) ); (3)for (i=0; i<5; i++) { (4) arr[i] = malloc( 7*sizeof(int) ); (5)}

Multi-dimensional arrays Dynamically allocated multi-dimensional array: u Memory not continuous u Each pointer can be with different size u Access: arr[ i ][ j ] u Don’t forget to free all the memory for (i=0; i<nrows; i++ ) { free( array[i] ); array[i] = NULL; } free( array );

pointers to pointers to … We also have pointers to pointers to pointers, etc. double ** mat1 = getMatrix(); double ** mat2 = getMatrix(); // allocate an array of matrices double *** matrices = (double***) malloc( n*sizeof( double**) ); matrices[0] = mat1; matrices[1] = mat2;

. Libraries

u Library is a collection of functions, written and compiled by someone else, that you may want to use u Examples:  C’s standard libraries  Math library  Graphic libraries u Libraries may be composed of many different object files

Libraries 2 kinds of libraries: u Static libraries:  linked with your executable at compilation time  standard unix suffix:.a u Shared libraries:  loaded by the executable at run-time  standard unix suffix:.so

Static libraries Using the static library libdata.a: g++ -o prog object1.o object2.o –ldata Creating the library data.a (2 commands): ar rcu libdata.a data.o stack.o list.o ranlib libdata.a u ar is like tar – archive of object files u ranlib builds a symbol table for the library  to be used by the linker

static vs. shared Static libraries pros: u Independent of the presence/location of the libraries u Less linking overhead on run-time Shared libraries pros: u Smaller executables u No need to re-compile executable when libraries are changed u The same executable can run with different libraries u Dynamic Library Loading (dll) possible

Libraries in makefile libdata.a:${LIBOBJECTS} ar rcu libdata.a ${LIBOBJECTS} ranlib libdata.a OBJECTS = foo.o bar.o CC = g++ prog: ${OBJECTS} libdata.a ${CC} ${OBJECTS} –ldata –o prog

Learn by yourselves u Keywords: register, volatile u Pointer casting  and the void* pointer