Day 03 Introduction to C.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Chapter 6 Data Types
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.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
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 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’;
CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.
Surviving C and PostgreSQL CS186 Supplemental Session 9/13/04 Matt Denny, Paul Huang, Murali Rangan (Originally prepared by Shariq Rizvi, Wei Xu, Shawn.
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
Informationsteknologi Monday, September 10, 2007Computer Systems/Operating Systems - Class 31 Today’s class Review of more C Operating system overview.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Class 2 CSI2172
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
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.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSE 220 – C Programming malloc, calloc, realloc.
Object Lifetime and Pointers
Memory allocation & parameter passing
Intro to Pointers in C CSSE 332 Operating Systems
Stack and Heap Memory Stack resident variables include:
Chapter 6 – Data Types CSCE 343.
C Primer.
Day 03 Introduction to C.
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
CSCE 210 Data Structures and Algorithms
Lecture 6 C++ Programming
8 Pointers.
14th September IIT Kanpur
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
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
Pointers and Arrays Beyond Chapter 16
Dynamic Memory A whole heap of fun….
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
Dynamic Memory CSCE 121.
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
Pointers.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Day 03 Introduction to C

Memory layout and addresses int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; x y f g c d 5 10 12.5 9. 8 r s 4300 4304 4308 4312 4316 4317

Pointers made easy - 1 ? ? 4300 4300 4304 f f_addr float f; // data variable - holds a float float *f_addr; // pointer variable – holds an address to a //float ? f_addr 4304 NULL 4300 f_addr = &f; // & = address operator

*f_addr = 3.2; // indirection operator or dereferencing 4300 4304 3.2 g 4308 float g=*f_addr; // indirection: g is now 3.2 1.3 3.2 f = 1.3;

Pointer operations Creation Pointer assignment/initialization int *ptr; Pointer assignment/initialization ptr = &i; (where i is an int and &i is the address of i) ptr = iPtr; (where iPtr is a pointer to an int) Pointer indirection or dereferencing i = *ptr; *ptr = 5; (i is an int and *ptr is the int value pointed to by ptr) Int *ptr, a; Only ptr is a ptr, a is an int.

Pointers and arrays int p[7], *ptr; // Both p and ptr are pointers // i.e. hold addresses. // p is already pointing to a fixed location and // cannot be changed. // ptr is still to be initialized. p[i] is an int value. p, &p[i] and (p+i) are addresses or pointers. *p = 5;  p[0] = 5; *(p+i) = 5;  p[i] = 5; Label p[0] p[1] p[2] p[3] p[4] p[5] p[6] ptr Value 24 34 202 88 92 56 17 ? Address 2000 2004 2008 2012 2016 2020 2024 … 2032

Pointer arithmetic int *ptr; int p[7]; ….. ptr = p; // or ptr = &p[0] ptr +=2; What is the value of ptr? ptr = ptr + 2 * sizeof(int) = ptr+8 bytes ptr = 2000 + 8 = 2008 => ptr = &(p[2]); ERROR: p = ptr; because “p” is a constant address, points to the beginning of a static array. p[0] p[1] p[2] p[3] p[4] p[5] p[6] ptr 24 34 202 88 92 56 17 ? Address 2000 2004 2008 2012 2016 2020 2024 … 2032 2008 2000

Dynamic memory allocation Explicit allocation and de-allocation by user using malloc() and free(). (void *)malloc(size_t size); void free(void *ptr); bytes sizeof(type) #include <stdio.h> int main() { int *ptr; /* allocate space to hold 4 ints */ ptr = (int*)malloc(4 * sizeof(int)); /* do stuff with the data */ *ptr=4; //ptr[0] = 4; /* free up the allocated space */ free(ptr); return 0; } malloc is defined in section 3C.

int *ptr; ? ptr 4000 ptr = (int*)malloc(4 * sizeof(int)); //Address 6000 on the heap is allocated *ptr=4; 6000 6004 6008 6012 ? 6000 4 free(ptr);

Man pages – Section 3 >>man –s 3 free >>man –s 3 malloc

Memory allocation for a process. Code segment Static data segment Dynamic data segment (heap) Stack malloc looks for space on the heap int *p; //p is created in the static data segment p = (int *)malloc(4 * sizeof(int)); //Space for 4 ints i.e. contiguous 16 //bytes is allocated on the heap

count * sizeof(int) Dynamic array int *ptr, i, count; printf(“Enter the number of items in the array”); scanf(“%d”,&count) //Create a dynamic array of “size” ints. ptr = (int*)malloc( count * sizeof(int) ); for(i=0; i<count; i++){ ptr[i] = i; } count * sizeof(int)

Variable length strings Array of Pointers Variable length strings char *card[4]; // card[4] => array of 4 elements //char* => element is a pointer to a character. // card[4] => array of 4 char pointers 4000 card[0] 4004 card[1] 4008 card[2] 4012 card[3]

card[0] = (char*)malloc(6*sizeof(char)); card[1] = (char*)malloc(3*sizeof(char)); and so on. Static allocation of a 2D array: char card[4][10]; //waste of space

Common errors - Memory leak int *ptr, x; ptr = (int*)malloc(10*sizeof(int)); //ptr gets space //starting at address 3000 ptr = &x; The space allocated through malloc is no longer available for use by the program. Released only when program quits. Becomes a problem in large programs where a large number of variables are created and destroyed during the execution of the program.

Common erros - Dangling pointers int *i, *x; i = (int*)malloc( 5 x sizeof(int)); x = i; / * both point to the same address. */ free(x); /* both i and x are dangling pointers and trying to access either of them can cause logical errors */ x = NULL; /* One way to prevent incorrect access */ i = NULL; void free_ptr(void *ptr){ free(ptr); ptr = NULL; }