Dynamic Memory. R. Inkulu (Dynamic Memory) 1 / 19

Similar documents
In Java we have the keyword null, which is the value of an uninitialized reference type

Memory (Stack and Heap)

Lecture 8 Dynamic Memory Allocation

o Code, executable, and process o Main memory vs. virtual memory

Class Information ANNOUCEMENTS

CSC 1600 Memory Layout for Unix Processes"

Dynamic Allocation of Memory

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Programming. Pointers, Multi-dimensional Arrays and Memory Management

CSCI 2212: Intermediate Programming / C Storage Class and Dynamic Allocation

Dynamic Memory Management

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Memory Management. CSC215 Lecture

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

LOÏC CAPPANERA. 1. Memory management The variables involved in a C program can be stored either statically or dynamically.

Dynamic memory allocation (malloc)

Memory Allocation. General Questions

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1


Run-time Environments

Run-time Environments

Memory Management in C (Dynamic Strings) Personal Software Engineering

Dynamic memory. EECS 211 Winter 2019

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Memory Management I. two kinds of memory: stack and heap

Memory Management. CS449 Fall 2017

Scientific Programming in C IV. Pointers

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Fundamental of Programming (C)

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

CMSC 330: Organization of Programming Languages

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Limitations of the stack

C Programming Basics II

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Dynamic Allocation of Memory

Heap Arrays. Steven R. Bagley

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

CPSC 3740 Programming Languages University of Lethbridge. Data Types

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

C11: Garbage Collection and Constructors

Dynamic Memory Allocation I Nov 5, 2002

Computer Programming Unit 3

CMSC 330: Organization of Programming Languages

Dynamic memory allocation

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Dynamic Memory Allocation I

Dynamic Memory Allocation

CSC 270 Survey of Programming Languages. What is a Pointer?

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

CSCI 171 Chapter Outlines

Quick review pointer basics (KR ch )

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

CA31-1K DIS. Pointers. TA: You Lu

Cpt S 122 Data Structures. Data Structures

TI2725-C, C programming lab, course

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Kurt Schmidt. October 30, 2018

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS61C : Machine Structures

Processes. Johan Montelius KTH

A process. the stack

Secure C Coding...yeah right. Andrew Zonenberg Alex Radocea

Variation of Pointers

Run-time Environments - 3

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Dynamic Memory Allocation. Zhaoguo Wang

Dynamic Allocation of Memory Space

Dynamic Memory Allocation: Basic Concepts

COMPSCI 210 Part II Data Structure

Vector and Free Store (Vectors and Arrays)

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

Fundamentals of Programming

Dynamic Allocation in C

Dynamic Memory Management! Goals of this Lecture!

C10: Garbage Collection and Constructors

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

Types II. Hwansoo Han

Memory and C/C++ modules

Arrays and Pointers in C & C++

Operating System Labs. Yuanbin Wu

CSE 307: Principles of Programming Languages

Content. In this chapter, you will learn:

C Structures & Dynamic Memory Management

Heap Arrays and Linked Lists. Steven R. Bagley

Short Notes of CS201

Dynamic Memory Management

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Dynamically Allocated Memory in C

Transcription:

Dynamic Memory R. Inkulu http://www.iitg.ac.in/rinkulu/ (Dynamic Memory) 1 / 19

Types of memory allocations auto local * allocated on stack and uninitialized by default * accessible in the function that it got defined * requesting to allocate in registers for faster access; the request is not necessarily entertained auto global * allocated in data area and initialized by default to zeros * accessible across the program auto static * allocated in data area and initialized by default to zeros * accessible among all functions defined within a file in which the variable is defined dynamic * useful if the size to be allocated is not known at compile time * allocated on heap and inititalized if requested * though not global/static, may be used across functions (Dynamic Memory) 2 / 19

Intro to dynamic memory p useful when the number of objects to be allocated is not known at compile-time system maintains a table of dynamically allocated memory blocks and their corresp. address ranges (Dynamic Memory) 3 / 19 double *p = (double *) malloc(count*sizeof(double)); //allocates count*sizeof(double) bytes contiguously; //p has the address of first byte of those allocated int i; for (i=0; i<count; i++) { p[i] = i*10; printf("%lf, ", p[i]); } //prints 0.000000, 10.000000, 20.000000, 30.000000, free(p); //frees contigous memory referred by p

Dynamic vs non-dynamic memory allocation z p double z[4]; //z is from stack double *p = (double*) malloc(count*sizeof(double)); //p is from stack, and memory block is from heap free(p); memory for auto variables (including arrays) comes from stack, whereas the dynamic memory comes from heap of the process address space (Dynamic Memory) 4 / 19

Pointer arithmatic double *p = (double*) malloc(count*sizeof(double)); double *q = NULL; //denotes irrelevant //stmts //i m here (1) p += 2; q = p; p //i m here (2) free(p-2); p at (1): at (2): q NULL q pointer arithmatic is same whether the memory referred by a pointer is either from the stack or heap (Dynamic Memory) 5 / 19

malloc and free pairs double *p=null; int *q = NULL; p = (double*) malloc(counta*sizeof(double)); q = (int*) malloc(countb*sizeof(int)); free(p); free(q); in any program, malloc and free invocations must exist in pairs avoiding free call corresp. to any malloc call causes memory leak malloc and free corresp. to a block of memory not necessarily invoked in the same function not necessarily bracketed (Dynamic Memory) 6 / 19

Returning pointers from functions double *func(int count) { double *p = NULL; p = (double*) malloc(count*sizeof(double)); return p; } void func1(int counta) { int count; double *q = NULL; q = func(count); free(q); return; } //freeing heap memory allocated in func heap memory referred by a pointer may require to be freed by the caller (precise protocol need to be defined) does not make sense to return the address of a local variable (ex. array) from a function (Dynamic Memory) 7 / 19

Returning pointers from functions (cont) void *malloc(size_t sizeinbytes) { } void func() returns nothing whereas void* func() returns pointer to a block of memory (could be NULL too) that can contain objects of any type while void signifies nothing; void* denotes pointer to objects of any type any type* is implicitly typecasted to void* whereas the other way around requires an explicit typecast (Dynamic Memory) 8 / 19

Dynamic memory: advantages and disadvantages advantages: useful when the number of objects to be allocated is not known at compile-time gives flexibility to allocate and deallocate memory based on the need; careful user of this primitive can extract benefits although not global, available across function call stack disadvantages: slow due to free/allocated heap space maintainance involved together with the defragmentation overhead due to intermittent mallocs and frees forgetting to deallocate memory causes memory leak (Dynamic Memory) 9 / 19

Non-dynamic memory: advantages and disadvantages advantages: compiler will deallocate the memory automatically for all the global, static, and local memory that it allocated: no memory leak hassles disadvantages: memory allocation and deallocation are not in the control of user for auto local variables, memory is always deallocated at the end of the function for auto global/auto static variables, memory is always deallocated at the end of the program (Dynamic Memory) 10 / 19

A note on modern compilers: simulating array behavior with dynamic memory 1 double A[count]; printf("%p, %p, %p, %p", &A, A, &A[0], (A+2)); //prints 0xbf99c9c8, 0xbf99c9c8, 0xbf99c9c8, 0xbf99c9d8 memory allocation is done on heap buffer allocated is named with the array name heap memory is deallocated at the end of the scope of the referring variable disadv: code may not be portable across all the compilers 1 for this course purpose, we always assume ANSI standard; hence, this kind of usage is not permitted (Dynamic Memory) 11 / 19

Avoid dangling pointers after freeing double *p = (double*) malloc(count*sizeof(double)); p //i m here (1) free(p); //i m here (2) p = NULL; p //i m here (3) p NULL at (1): at (2): dangling at (3): avoid dangling pointers by resetting pointer variable value immediately after the free (Dynamic Memory) 12 / 19

Few memory related errors out-of-bounds read or write reading/writing already freed memory freeing non-dynamically allocated memory freeing a block of heap memory multiple times memory that has no pointer in the program (memory leak) program has only a pointer to the middle of allocated memory (potential memory leak) dereferncing a null pointer reading uninitialized memory (Dynamic Memory) 13 / 19

Few useful functions from stdlib.h void *malloc(size t numbytes) avoid malloc(0) as this is not portable void free(void *p) void *calloc(size t numobj, size t sizeofaobject) same as malloc but zeros the memory allocated void *realloc(void *oldmem, size t numbytes) resizes and where necessary relocates the block pointed by p; moves the contents of *p to the new location; frees the old memory when oldmem is NULL, works same as malloc void *memcpy(void *to, void *from, int numbytes); cannot handle the overlap void *memmove(void *to, void *from, int numbytes); same as memcpy but handles the overlap (Dynamic Memory) 14 / 19

Array of pointers to varying sized arrays *(buf[0]+2) a.k.a. buf[0][2] buf buf[0] buf[1] STACK HEAP double *buf[2]; buf[0] = (double*) malloc(counta*sizeof(double)); buf[1] = (double*) malloc(countb*sizeof(double)); printf("%d, %d \n", sizeof(buf[0]), sizeof(buf)); //prints sizeof(void*), 2*sizeof(void*) free(buf[1]); free(buf[0]); buf is an array[2] of pointers, each entry of which points to a block of memory that contains one or more doubles (Dynamic Memory) 15 / 19

Array of pointers to varying sized arrays (cont) buf z buf[0] buf[1] STACK STACK *(buf[0]+2) or *(buf[1]) double z[4]; double *buf[2]; buf[0] = &z[0]; buf[1] = &z[2]; printf("%d, %d \n", sizeof(buf[0]), sizeof(buf)); //prints 4, 8 //no need of free calls (Dynamic Memory) 16 / 19

Array of pointers to fixed size arrays buf *(buf+0) *(buf+2) STACK *(buf+1) HEAP (*(buf+1))[1] double (*buf)[2] = (double (*)[2])malloc(count*sizeof(double [2])); printf("%d, %d, %d, %d \n", sizeof(double), sizeof(buf[0]), sizeof(buf[2]), sizeof(buf)); //prints 8, 16, 16, 4 when count is 3 free(buf); buf points to count number of array[2]s of doubles in other words, buf[0] is the zeroth array[2]; buf[1] is the first array[2]; etc., (in other words, buf is a two dimensional array with dimensions count 2: count number of rows while each row has two columns) (Dynamic Memory) 17 / 19

Definitions to memory-layouts (review) (i) double a[2]; double *p = &a[0]; (ii) double *p = (double *) malloc(count*sizeof(double)); (iii) double a[2][3]; (iv) double *a[2]; a[0] = (double *) malloc(counta*sizeof(double)); a[1] = (double *) malloc(countb*sizeof(double)); (v) double *a[2], b[2][3]; a[1] = b[0]; (vi) double (*b)[2]; b = (double (*)[2]) malloc(count*sizeof(double [2])); (vii) double (*b)[2], c[4][2]; b = c; homework: using these notions, engineer few more declarations (Dynamic Memory) 18 / 19

Memory-layouts to definitions buf STACK *(buf) HEAP (*buf[1][0]) [1] *(buf+1) HEAP double *buf[2][3] (*(buf+1))[1][1] double (*buf)[2][3] *(buf) *(buf+1) HEAP HEAP ((*(buf+0))[1][0])[1] double *(*buf)[2][2] homework: do the needed allocations and initializations (Dynamic Memory) 19 / 19