Memory Organization. The machine code and data associated with it are in the code segment

Similar documents
Kurt Schmidt. October 30, 2018

Fall 2018 Discussion 2: September 3, 2018

Lecture 8 Dynamic Memory Allocation

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Dynamic Memory & ADTs in C. The heap. Readings: CP:AMA 17.1, 17.2, 17.3, The primary goal of this section is to be able to use dynamic memory.

Dynamic Memory & ADTs in C

ENCM 339 Fall 2017 Tutorial for Week 8

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

CS 11 C track: lecture 5

Memory Management. CSC215 Lecture

Common Misunderstandings from Exam 1 Material

Dynamic Data Structures. CSCI 112: Programming in C

High Performance Programming Programming in C part 1

Pointers and Memory Management

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Dynamic Memory & ADTs in C

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

Programs in memory. The layout of memory is roughly:

TI2725-C, C programming lab, course

Dynamic memory. EECS 211 Winter 2019

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

ECE 551D Spring 2018 Midterm Exam

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

COSC Software Engineering. Lecture 16: Managing Memory Managers

CSE 333 Midterm Exam Sample Solution 7/28/14

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

Dynamic memory allocation (malloc)

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Memory (Stack and Heap)

Memory, Arrays & Pointers

C Programming Basics II

Intermediate Programming, Spring 2017*

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

COMP 2355 Introduction to Systems Programming

CSE 333 Lecture 4 - malloc, free, struct, typedef

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

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

CS24 Week 2 Lecture 1

C Structures & Dynamic Memory Management

Arrays, Pointers and Memory Management

Dynamic Allocation of Memory

CSE 333 Midterm Exam 2/14/14

Pointers, Dynamic Data, and Reference Types

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

DAY 3. CS3600, Northeastern University. Alan Mislove

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

CSCI 171 Chapter Outlines

Contents of Lecture 3

CS61C Midterm Review on C & Memory Management

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

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

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

Systems Programming and Computer Architecture ( )

BBM 201 DATA STRUCTURES

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CSC 1600 Memory Layout for Unix Processes"

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

Advanced Pointer Topics

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

ECE 2400 Computer Systems Programming Fall 2018 Topic 6: C Dynamic Allocation

CS201 Some Important Definitions

Secure Coding in C and C++

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

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

ECE 15B COMPUTER ORGANIZATION

A First Book of ANSI C

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Memory Allocation. General Questions

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Review. You Are Here! Recap: The Stack 11/8/12. CS 61C: Great Ideas in Computer Architecture C Memory Management. Recap: C Memory Management

CSE 333 Midterm Exam Sample Solution 5/10/13

Dynamic Data Structures (II)

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

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

ECE551 Midterm Version 1

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

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

CSE 333 Midterm Exam 5/10/13

ECE551 Midterm Version 2

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

The Heap and Structs. CSE 333 Spring Instructor: Justin Hsia

Fast Introduction to Object Oriented Programming and C++

CS201- Introduction to Programming Current Quizzes

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

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

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Class Information ANNOUCEMENTS

Memory Corruption 101 From Primitives to Exploit

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Transcription:

Memory Management

Memory Organization During run time, variables can be stored in one of three pools : 1. Stack 2. Global area (Static heap) 3. Dynamic heap The machine code and data associated with it are in the code segment 2

Stack Maintains memory during function calls: Argument of the function Local variables Address to return to in code segment Variables on the stack have limited life time Stack size must be defined at compile time 3

Stack - Example int foo( int a, double f ) int b;... <call> a f b 4

Stack - Example int foo( int a, double f ) int b;... int c;...... <call> a f b 5

Stack - Example int foo( int a, double f ) int b;... int c;...... <call> a f b c 6

Stack - Example int foo( int a, double f ) int b;... int c;...... <call> a f b c 7

Stack - Example int foo( int a, double f ) int b;... int c;...... <call> a f b c 8

Stack recursive example void foo( int depth ) int a; if( depth > 1 ) int main() foo(3); foo( depth-1 ); <call> depth a <call> depth a <call> depth a 9

Stack recursive example void foo( int depth ) int a; if( depth > 1 ) int main() foo(3); foo( depth-1 ); <call> depth a <call> depth a <call> depth a 10

Stack recursive example void foo( int depth ) int a; if( depth > 1 ) int main() foo(3); foo( depth-1 ); <call> depth a <call> depth a 11

Stack recursive example void foo( int depth ) int a; if( depth > 1 ) int main() foo(3); foo( depth-1 ); <call> depth a <call> depth a <call> depth a 12

Stack -- errors void foo( int depth ) int a; if( depth > 1 ) foo( depth ); Will result in run time error: out of stack space 13

Global area Memory for global variables must be defined in compile time #include <stdio.h> const size_t LIST_OF_NUMBER_SIZE = 1000; int ListOfNumbers[LIST_OF_NUMBER_SIZE]; 14 int main()...

Global area: reverse example Example: program to reverse the order of lines of a file. To this task, we need to Read the lines into memory Print lines in reverse How do we store the lines in memory? 15

Global area: reverse example const size_t LINE_LENGTH = 100; const size_t NUMBER_OF_LINES = 10000; char g_lines[number_of_lines][line_length]; 16... int main() size_t n= ReadLines(); while(n-- > 0) printf("%s\n", g_lines[n]); return 0;

Global area: reverse example This solution is problematic: The program cannot handle files larger than these specified by the compile time choices If we set NUMBER_OF_LINES to be very large, then the program requires this amount of memory even if we are reversing a short file The life-time of global variables is the entire program running time. Want to use memory on as needed basis 17

Dynamic Heap 1. Memory that can be allocated and freed during run time. 2. The program controls how much is allocated and when. 3. Limitations based on run-time situation - available memory on the computer. 18

Allocating Memory from Heap #include <stdlib.h> void* malloc( size_t size ); Returns a pointer to a new memory block of size size, or NULL if it cannot allocate memory of this size. 19

Why do we need dynamic memory? In static arrays we need to know the array size at compilation time: int arr[] = 1,2,3,4,5; But, this size may not be known in advance, or might be changed during the program execution. The solution: use dynamic array: size_t arraysize= readinputfromuser( ); int* arr = (int*)malloc(sizeof(int)*arraysize); 20

We can also use it for a single int int* iptr = (int*) malloc(sizeof(int)); 21

Always check allocation success - or document that behavior is undefined otherwise char *str = (char *)malloc(5*sizeof(char)); if (str==null) //print error message exit(1); //or perform relevant operation 22

What will be in the allocated memory? int* iptr = (int*) malloc(sizeof(int)); struct Complex* complex_ptr = (struct Complex*) malloc(sizeof(struct Complex)); 23

What will be in the allocated memory? *iptr = undefined *complex_ptr = undefined 24

Good design an initialization function for a struct (poor constructor). struct Complex* newcomplex(double r, double i) struct Complex *p = (struct Complex*) malloc (sizeof (Complex)); p->_real = r; p->_imag = i; return p; 25

Good design an initialization function for a struct (poor constructor). struct Complex *complex_ptr = newcomplex (1.0, 2.1); 26

De-allocating memory void free( void *p ); Returns the memory block pointed by p to the pool of unused memory 27 No error checking! Undefined if p: Is not NULL and Was not allocated by malloc or its friends or free-ed before

Example of free int* iptr =... (int*) malloc(sizeof(int)); free(iptr); 28

How to free a pointer to struct? void foo( double r, double i ) struct Complex* p_c = newcompelx (r,i); // do something with p_c free(p_c); This version frees the allocated memory 29

What about structs that allocate memory by themselves? struct Vec ; int *_arr; size_t _length; 30 struct Vec *newvec (size_t length) struct Vec *p = (struct Vec*) malloc (sizeof (struct Vec)); if (p!=null) p->_arr = (int*) malloc (sizeof(int)*length); p->_length= length; return p; int main () struct Vec *v = newvec(5); // do something free (v);

Memory leak every malloc should have a corresponding free otherwise a bug! struct Vec ; int *_arr; size_t _length; 31 struct Vec *newvec (size_t length) struct Vec *p = (struct Vec*) malloc (sizeof (struct Vec)); if (p!=null) p->_arr = (int*) malloc (sizeof(int)*length); p->_length= length; return p; int main () struct Vec *v = newvec(5); // do something free (v);

Poor destructors struct Vec int *_arr; ; size_t _length; void freevec (struct Vec *v) free (v->_arr); free (v); 32 int main () struct Vec *v = newvec(5); // do something freevec (v);

Poor destructors struct Vec int *_arr; ; size_t _length; But, what if v is NULL? void freevec (struct Vec *v) free (v->_arr); free (v); 33 int main () struct Vec *v = newvec(5); // do something freevec (v);

Poor destructors struct Vec int *_arr; ; size_t _length; void freevec (struct Vec *v) if (v==null) return; free (v->_arr); free (v); 34 int main () struct Vec *v = newvec(5); // do something freevec (v);

Unit Testing Reminder: See slides: programming style and correctness

36 List code example

List* List_clone(const List* list)

List* List_clone(const List* list) List* ret= List_alloc();

List* List_clone(const List* list) List* ret= List_alloc();

List* ret= List_alloc(); const Node* old= list->_head;

const Node* old= list->_head; Node** copy= &(ret->_head);

Node** copy= &(ret->_head); ret->_size= list->_size; while(old)

*copy= Node_alloc(old->_data,NULL);

*copy= Node_alloc(old->_data,NULL);

old= old->_next;

copy= &((*copy)->_next);

copy= &((*copy)->_next);

copy= &((*copy)->_next);

Array reallocation we need to resize an array 49

Array reallocation wrong solution int *arr = (int*)malloc(sizeof(int)*arraysize); //put some values in arr... int* narr= (int*)malloc(sizeof(int)*(arraysize+1)); //copy values from arr to newarr... arr = narr; // BAD: lost address of first allocation! 50

Array reallocation Allocate array on the heap and assign a pointer to it arr 1 2 3 51

Array reallocation Use temporary pointer to point to the old array. arr temp 1 2 3 52

Array reallocation Use temporary pointer to point to the old array. Reallocate new array. arr temp 1 2 3????? 53

Array reallocation Use temporary pointer to point to the old array. Reallocate new array. Copy values from the old array to the new one. arr temp 1 2 3 1 2 3?? 54

Array reallocation Use temporary pointer to point to the old array. Reallocate new array. Copy values from the old array to the new one. Free the old array. arr temp 1 2 3 1 2 3?? 55

Array reallocation version 1 void reallocatememory(int **parr, size_t oldsize, size_t newsize) assert(newsize>oldsize); int *old = *parr; //partial example do not forget checking malloc! *parr = (int*)malloc(sizeof(int)*newsize); size_t i = 0; while(i < oldsize) (*arr)[i] = old[i]; ++i; free(old); 56

Array reallocation version 1 int main() int *arr = (int*)malloc(sizeof(int)*arrsize); //do some stuff... reallocatememory(&arr,arrsize,newsize); free(arr) 57

Array reallocation version 2 int* reallocatememory(int *arr, size_t oldsize, size_t newsize) assert(newsize>oldsize); 58 int *old = arr; //partial example do not forget checking malloc! arr = (int*)malloc(sizeof(int)*newsize); size_t i = 0; while(i < oldsize) arr[i] = old[i]; ++i; free(old); return arr;

Array reallocation version 2 int main() int *arr = (int*)malloc(sizeof(int)*arrsize); //do some stuff... arr = reallocatememory(arr,arrsize,newsize); free(arr); 59

Array reallocation using realloc int * arr = (int*)malloc(sizeof(int)*oldsize); arr = (int*)realloc(arr,sizeof(int)*newsize); realloc tries to reallocate the new memory in place, if fails, tries elsewhere The old data is preserved The new cells contents is undefined If arr=null, behaves like alloc 60

Further Knowledge Read manual page of malloc calloc realloc free 61

Variable Length Arrays (VLA) How to allocate an array on the stack with size defined only on running time? ANSI C - use malloc: size_t length= get_user_length(); int* arr= (int*) malloc(length*sizeof(int)); C99 - introduce VLA size_t length= get_user_length(); int arr[length]; 62

VLA - disadvantage No clear definition of the implementation Not accepted by C++ standard (use std::vector instead) Not mandatory feature in C11 Still not supported by common compilers like visual studio 2012 (and probably will not be supported) Not allowed during the course (use Wvla or Makfile in the List code) More information here: http://www.drdobbs.com/efficient-variable-automaticbuffers/184401740?pgno=6 63

Common memory/pointers bugs

bug 1 (1) typedef struct _Student (2) int id; (3) char * name; (4) Student; (5) Student * stud = (Student *) malloc( sizeof(student) ); (6) stud->id = 123456; (7) stud->name = (char *) malloc(100*sizeof(char)); (8) free(stud); Memory leak of name!

bug 2 1) void myfunc() 2) int * x = randomnumptr(); 3) int result = *x; //unexpected! 4) *x = 17; //accessing unallocated space! 5) 6) 7) int * randomnumptr() 8) int j= srand( time(0) ); 9) return &j; 10) Never return a address of a stack-variable!

bug 3 1) void myfunc(const char * input) 2) char * name; 3) if (input!= NULL ) 4) name= (char*)malloc(max_size*sizeof(char)); 5) strcpy(name, input); 6) 7) 8) free( name ); 9) 10) free on an address that was not allocated using malloc. Sometimes initialize pointers to NULL.

No bug 3 1) void myfunc(const char * input) 2) char * name= NULL; 3) if (input!= NULL ) 4) name= (char*)malloc(max_size*sizeof(char)); 5) strcpy(name, input); 6) 7) 8) free( name ); 9) 10) Sometimes initializing pointers to NULL prevents these bugs

Remarks It is not always a good practice to initialize variables. The compiler can catch some of the used uninitalized variables which is probably a bug and is better caught at compile time then hided under arbitrary initialization. In the last example, gcc 4.8.4 (2016) did not catch the problem and the NULL initialization is a good solution as the logic is reasonable. In general, variables should be declared and initialized as locally as possible (not possible in ansi C, but possible in gcc and in this course) However, sometimes for efficiency it is better to give them a longer lifetime.

How to duplicate a string? #include <stdio.h> #include <string.h> #include <stdlib.h> int main() const char *p1 = "hi mom", *p2 = (char*)malloc(strlen(p1)*sizeof(char)); strcpy(p2,p1); printf("%s\n",p2); return 0; 70

How to duplicate a string? #include <stdio.h> #include <string.h> #include <stdlib.h> int main() const char *p1 = "hi mom", *p2 = (char*)malloc(strlen(p1)*sizeof(char)); strcpy(p2,p1); printf("%s\n",p2); return 0; 71

How to duplicate a string? #include <stdio.h> #include <string.h> #include <stdlib.h> int main() const char *p1 = "hi mom", *p2 = (char*)malloc((strlen(p1)+1)*sizeof(char)); strcpy(p2,p1); printf("%s\n",p2); return 0; 72

strdup: duplicate a string (part of the standard library) char* strdup( char const *p ) size_t n = strlen(p); char* q =(char*)malloc(sizeof(char)*(n+1)); if( q!= NULL ) strcpy( q, p ); return q; 73

Heap Memory Management void foo( char const* p ) char *q; q = strdup( p ); // do something with q <call> p q The allocated memory remains in use cannot be reused later on a b \0

Memory Management void foo( char const* p ) char *q = strdup( p ); // do something with q free(q); Now the memory is being freed. 75