CS111 Computer Programming

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
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:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Pointers Applications
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
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.
ECE Application Programming
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
CSC Programming for Science Lecture 34: Dynamic Pointers.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
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.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
Programming application CC213
Today’s Material Arrays Definition Declaration Initialization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 03 Introduction to C.
Module 2 Arrays and strings – example programs.
2-D arrays a00 a01 a02 a10 a11 a12 a20 a21 a22 a30 a31 a32
Arrays & Dynamic Memory Allocation
Dynamic Memory Allocation
Some examples.
Pointers.
14th September IIT Kanpur
CS1100 Computational Engineering
Programming and Data Structures
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
prepared by Senem Kumova Metin modified by İlker Korkmaz
By Hector M Lugo-Cordero September 17, 2008
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Introduction to Problem Solving and Programming
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
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
Chapter 10-1: Dynamic Memory Allocation
Arrays and Matrices Prof. Abdul Hameed.
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
Presentation transcript:

CS111 Computer Programming Array of more than one dimension

Multi-dimensional Arrays 1 2 3 A[4][3] B[2][4][3] 1 2 1 2 1 2 3 1 Arrays with two or more dimensions can be defined

Two Dimensional Arrays Declaration: int A[4][3] : 4 rows and 3 columns, 4 × 3 array Elements: A[i][j] - element in row i and column j of array A Note: rows/columns numbered from 0 Storage: row-major ordering elements of row 0, elements of row 1, etc Initialization: int B[2][3]={{4,5,6},{0,3,5}}; 1 2 3 A[4][3]

Matrix Operations An m-by-n matrix: M: m rows and n columns Rows : 1, 2, … , m and Columns : 1, 2, … , n M(i,j) : element in ith row, jth column, 1 ≤ i ≤ m, 1 ≤ j ≤ n Array indexes in C start with 0.

Matrix multiplication Multiply two numbers N Sum of N products

Using Matrix Operations main(){ int a[11][11], b[11][11], c[11][11]; / * max size 10 by 10 */ int aRows, aCols, bRows, bCols, cRows, cCols; int i, j, k; scanf("%d%d", &aRows, &aCols); for(int i = 1; i <= arows; i++) for(int j = 1; j <= acols; j++) scanf("%d", &a[i][j]); scanf("%d%d", &bRows, &bCols); for(int i = 1; i <= brows; i++) for(int j = 1; j <= bcols; j++) scanf("%d", &b[i][j]); … Remember bRows=aCols

cRows = aRows; cCols = bCols; for(int i = 1; i <= crows; i++) for(int j = 1; j <= ccols; j++) { c[i][j]=0; for(int k = 1; k <= aCols; k++) c[i][j] += a[i][k]*b[k][j]; } for(int i = 1; i <= crows; i++){ for(int j = 1; j <= ccols; j++) /* print a row */ printf("%d ", c[i][j]); /* notice missing \n */ printf("\n"); /* print a newline at the end a row */

Initialization /* Valid declaration*/ int abc[2][2] = {1, 2, 3 ,4 } /* Invalid declaration – you must specify 2nd dimension*/ int abc[][] = {1, 2, 3 ,4 } /* Invalid */ int abc[2][] = {1, 2, 3 ,4 }

Memory Mapping (Conceptual) int abc[2][2] = {1, 2, 3 ,4 }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 … 1 2 3 4

Memory Mapping (Actual) int abc[2][2] = {1, 2, 3 ,4 }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 … 1 2 3 4

2D Matrix - Recap Declaring a 2D Array Memory allocated int myInt[3][4]; float myFloat[5][7]; char myChar[4][10]; Memory allocated sizeOf(Type of variable) * number of 1D array* number elements in one 1D array

Memory map int myInt[3][4]; myInt[0] myInt[1] myInt[2]

How much memory (bytes) required? Memory map int myInt[3][4]; Dynamic Memory Allocation How much memory (bytes) required? Sufficient for 12 Integers !!

int myInt[3][4]; One integer variable’s size = 4 bytes. So total memory = 12 x 4 = 48 bytes. Syntax: int *myPtr = (int*)malloc(48);

Dynamic allocation: 2D int *myPtr = (int*)malloc(48); *myPtr = 10;

Dynamic allocation: 2D int *myPtr = (int*)malloc(48); *(myPtr+1) = 20;

Dynamic allocation: 2D myPtr+i*4+j = myPtr + 2*4+1 = myPtr + 9 int *myPtr = (int*)malloc(48); Dynamic allocation: 2D scanf("%d",myPtr+i*4+j);     myPtr+i*4+j = myPtr + 2*4+1 = myPtr + 9 myPtr+1 myPtr+2 myPtr myPtr myPtr+3 myPtr+4 myPtr+5 myPtr+6 myPtr+8 myPtr+7 myPtr+9

2D Array as collection of 1D Array: Pointers myPtr1[0] myPtr1[1] myPtr1[2]

Function and 2D Array Passing 1D Array to a function Always pass by reference Need to pass the size of the array Passing 2D Array to a function Pass by reference Need to pass the size of individual arrays Can be accessed via pointers in the function Operations (if any) is on the actual array locations Need not return an array

Ways of Passing 2D Array (E.g. Integer) to a Function int myInt[3][4]; void display(?); Pointer to AN INTEGER Pointer to Array of INTEGERs void display ( int (*myPtr)[4], int row, int col); void display ( int *myPtr, int row, int col); void display (int myPtr[][4], int row, int col);

Changing size dynamically

Changing size dynamically realloc() void *realloc( void *ptr, size_t new_size ) Defined in header <stdlib.h>

Reallocation procedure Reallocates the given area of memory. It must be previously allocated by malloc(), calloc() or realloc() and not yet freed with a call to free or realloc. The reallocation is done by either: Expanding or contracting the existing area pointed to by ptr, if possible. The contents of the area remain unchanged up to the lesser of the new and old sizes. Allocating a new memory block of size new_size bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block. If there is not enough memory, the old memory block is not freed and null pointer is returned.

Changing size dynamically myPtr int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);

Changing size dynamically ptr_new int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);

Changing size dynamically myPtr int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);

Changing size dynamically myPtr ptr_new int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);

Changing size dynamically ptr_new int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3); Content of first two element remain same !!

Free allocated memory Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You must explicitly use free() to release the space. free(ptr);