Memory allocation & parameter passing

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
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 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Informationsteknologi Friday, September 14, 2007Computer Systems/Operating Systems - Class 51 Today’s class Finish operating system overview Review of.
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’;
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.
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.
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.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Outline Midterm results Static variables Memory model
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Stack and Heap Memory Stack resident variables include:
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
+ 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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
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.
Memory Management.
Object Lifetime and Pointers
Intro to Pointers in C CSSE 332 Operating Systems
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Day 03 Introduction to C.
CSE 374 Programming Concepts & Tools
Lesson One – Creating a thread
ENEE150 Discussion 07 Section 0101 Adam Wang.
Pointers, Polymorphism, and Memory Allocation
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 04 Introduction to C.
Day 03 Introduction to C.
Checking Memory Management
Pointers Revisited What is variable address, name, value?
C Basics.
Dynamic Memory Allocation
Programming Languages and Paradigms
Lecture 6 C++ Programming
Dynamic Memory Allocation
14th September IIT Kanpur
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Memory Allocation CS 217.
Programming in C Pointer Basics.
EECE.2160 ECE Application Programming
By Hector M Lugo-Cordero September 17, 2008
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
C Programming Lecture-8 Pointers and Memory Management
Homework Continue with K&R Chapter 5 Skipping sections for now
Chapter 9: Pointers and String
Programming in C Pointer Basics.
C Structures and Memory Allocation
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Chapter 16 Pointers and Arrays
Presentation transcript:

Memory allocation & parameter passing CSSE 332 Operating Systems Rose-Hulman Institute of Technology Pass out quiz on Memory Management 1

Announcements HW 2 is due Thursday at 11:59 PM Run svn st at a terminal to check the status of your repo. 2

Explicit memory allocation >> cd Examples/ >> emacs example11.c Study the use of malloc() and free() They allow explicit allocation and de-allocation of dynamic memory >> gcc example11.c –o example11 >> ./example11 malloc is defined in section 3 of the manual pages. >> man –s 3 malloc Man formats and displays the manual pages for some name that you pass to it. >> man man Really, a pointer to an array of integers. 3

Explicit memory allocation (2) int *ptr; ptr = (int*)malloc(4 * sizeof(int)); *ptr=4; 6000 6004 6008 6012 ptr 6000 ? ? ? ? ? 4 Code from example 11 Might want to display *ptr. 4000 free(ptr); 4

Dynamic array int *ptr, i, size; printf("Enter the size of the array:"); scanf("%d", &size); ptr = (int *) malloc(size * sizeof(int)); for(i = 0; i < size; i++){ ptr[i] = i; } Might be able to modify example 11 to include this and experiment with this code. Might want to display the content of the array. 5

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 pointers */ char *card[4]; 4000 card[0] NULL 4004 card[1] NULL 4008 card[2] NULL 4012 card[3] NULL Q1 6

Array of Pointers (2) card[0] = (char *)malloc(6 * sizeof(char)); . . . Static allocation of a 2D array: char card[4][10]; // waste of space 7

Common errors - Memory leak int *ptr, x; //ptr points to newly allocated memory ptr = (int*)malloc(10*sizeof(int)); // ptr now points away from allocated memory ptr = &x; Memory allocated with 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. Use box and pointer diagrams to illustrate 8

Common errors - Dangling pointers int *i, *x; i = (int*)malloc( 5 * sizeof(int)); x = i; //both point to the same address. /* i is a dangling pointer; trying to access either i or x can cause logical errors */ free(x); x = NULL; // One way to prevent incorrect access i = NULL; 9

Functions – pointers as arguments >> cd Examples/ >> emacs example12.c Study the code to see how pointers are passed to functions. >> gcc example12.c –o example12 >> ./example12 The only value that’s changed is the value of c because the pointee pointed to by pc is c. This is like making a function return multiple values. 10

In main() a 4 4000 b 5 4004 c 6 4008 ptr 4004 4012 a, b, c, and ptr are local variable in main() pa, pb, and pc are local variables in sumAndInc() pa 4000 6000 pb 4004 6004 pc 4008 6008 In function 11

In main() after the function call 4 4000 b 5 4004 c 7 4008 ptr 4004 4012 In main() after the function call 12

What’s wrong with this ? >> cd Examples/ >> emacs example13.c Does the code compile? Does it run correctly? >> gcc example13.c –o example13 >> ./example13 The code compiles correctly but when run, it gives a segmentation fault. Why? p is still uninitialized. Q2 13

p ? 4000 In main() ptr temp In the function 6004 6000 ? 6004 8 In function, temp is stored on the stack. 14

In main() after function call p ? 4000 In main() after function call The stack frame has been popped so temp no longer exists. p is still uninitialized. This is why we receive a segmentation fault. 15

Passing & returning array >> cd Examples/ >> emacs example14.c Why is the size of the array passed? Arrays are always passed as pointers? >> gcc example14.c –o example14 >> ./example14 An array does not know its size. Recall that an array is a pointer to a set of contiguous memory locations. 16

Passing & returning a structure /* pass struct by value */ void displayYear_1(struct birthday mybday) { printf("I was born in %d\n", mybday.year); }/* - inefficient: why ? */ /* pass pointer to struct */ void displayYear_2(struct birthday *pmybday) { printf("I was born in %d\n", pmybday->year); } /* Note: ‘->’, not ‘.’, after a struct pointer */ /* return struct by value */ struct birthday get_bday(void){ struct birthday newbday; newbday.year=1971; /* ‘.’ after a struct */ return newbday; }/* - also inefficient: why ? */ Q3 17