1 Day 03 Introduction to C. 2 Memory layout and addresses 5 10 12.5 9. 8 r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; 430043044308431243164317.

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.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
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. 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.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Informationsteknologi Friday, September 14, 2007Computer Systems/Operating Systems - Class 51 Today’s class Finish operating system overview Review of.
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.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
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.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
 2006 Pearson Education, Inc. All rights reserved Pointers.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
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
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Class 2 CSI2172
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.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
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.
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.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
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:
Day 03 Introduction to C.
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
Day 03 Introduction to C.
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

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’; x y f g c d

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

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

5 Pointer operations Creation 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)

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 ? Address …2032

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 = = 2008 => ptr = &(p[2]); Pointer arithmetic p[0]p[1]p[2]p[3]p[4]p[5]p[6]ptr ? Address …2032 ERROR: p = ptr; because “p” is a constant address, points to the beginning of a static array

#include 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; } 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)

int *ptr; ? ptr 4000 ptr = (int*)malloc(4 * sizeof(int)); //Address 6000 on the heap is allocated ???? 6000 *ptr=4; 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

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)

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 4000card[0] 4004card[1] 4008card[2] 4012card[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

15 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.

16 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; }