CS111 Computer Programming

Slides:



Advertisements
Similar presentations
Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
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
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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.
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.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
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.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
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.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
Introduction to Programming
Day 03 Introduction to C.
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
Lecture 6 C++ Programming
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Pointers.
14th September IIT Kanpur
Pointers.
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Memory Allocation CS 217.
Pointers.
Dynamic Memory Allocation
By Hector M Lugo-Cordero September 17, 2008
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
Pointers Pointers point to memory locations
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Chapter 9: Pointers and String
Dynamic Memory – A Review
Pointers.
EECE.2160 ECE Application Programming
Module 13 Dynamic Memory.
Introduction to Pointers
Dynamic Data Structures
Presentation transcript:

CS111 Computer Programming Introduction to pointers

Idea 308 roomNo Location Name Location Name 4294953952 Location Address Can we see this values?

Question: Is it a variable? Or a Constant ? Definition A pointer is a symbolic representation of an address in the computer's memory. Question: Is it a variable? Or a Constant ? We can change We can't change

int *roomNoPtr = &roomNo; Pointer Variable int *roomNoPtr = &roomNo; roomNo roomNoPtr 308 4294953952 4294953952 4294852953

The Jargon of Pointers int a = 10; int *b; b = &a; b has address of a b is an integer pointer Value at address pointed by b is an int …

Pointer of other data types Pointer is assigned to a specific data type also Be careful while casting !!

Passing Address to Function Example of Call by Reference Address of the variables are passed to the function Operation happens on the actual address No need to return

The Jargon of Pointers int a = 10; int *b; b = &a; b has address of a b is an integer pointer Value at address pointed by b is an int …

Pointer of other data types Pointer is assigned to a specific data type also Be careful while casting !!

Pointers and 1D Array int myIntArr [10]; float myFloatArr [10]; -9 20 35 64 77 -85 74 32 float myFloatArr [10]; 2.5 3.6 7.5 -8.2 0.25 3.11 4.69 8.25 -10.12 3.66 char myCharArr [10]; A C B F c E X y Z O

1D Array Memory Allocation char myCharArr [10]; int myIntArr [10]; 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 …

Pointer to an Integer vs Pointer to an Array of Integer int myIntArr [10]; int *myPtr = &myIntArr[0]; myPtr ++;

Pointer to an Integer vs Pointer to an Array of Integer int myIntArr [10]; int (*myPtr)[10] = &myIntArr; myPtr ++;

Passing Array to Function 1. Element by Element 2. Address of individual elements 3. Entire Array/ Base Address of the Array

malloc() e.g. char *line; int linelength = 100; line = (char*)malloc(linelength); malloc() To allocate memory use void *malloc(size_t size); Takes number of bytes to allocate as argument. Use sizeof to determine the size of a type. Returns pointer of type void *. A void pointer may be assigned to any pointer. If no memory available, returns NULL.

malloc() example Note we cast the return value to int*. To allocate space for 100 integers: int *ip; if ((ip = (int*)malloc(100 * sizeof(int))) == NULL){ printf("out of memory\n"); exit(); } Note we cast the return value to int*. Note we also check if the function returns NULL.

calloc() Similar to malloc(), the main difference is that the values stored in the allocated memory space are zero by default. With malloc(), the allocated memory could have any value. calloc() requires two arguments - the number of variables you'd like to allocate memory for and the size of each variable. void *calloc(size_t nitem, size_t size); Like malloc(), calloc() will return a void pointer if the memory allocation was successful, else it'll return a NULL pointer.