Dynamic Allocation in C

Similar documents
Dynamic Allocation in C

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

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

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

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Pointer Casts and Data Accesses

Arrays and Pointers (part 1)

CS 11 C track: lecture 5

Understanding Pointers

Pointers, Dynamic Data, and Reference Types

DAY 3. CS3600, Northeastern University. Alan Mislove

CS201- Introduction to Programming Current Quizzes

[0569] p 0318 garbage

SYSC 2006 C Winter 2012

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

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

Arrays and Pointers (part 1)

CSC 1600 Memory Layout for Unix Processes"

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

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

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

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Lecture 8 Dynamic Memory Allocation

Accessing Data in Memory

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Dynamic Data Structures. CSCI 112: Programming in C

Today s lecture. Continue exploring C-strings. Pointer mechanics. C arrays. Under the hood: sequence of chars w/ terminating null

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

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

Content. In this chapter, you will learn:

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

C++ for Java Programmers

Memory, Arrays & Pointers

C Structures & Dynamic Memory Management

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Arrays, Pointers and Memory Management

CS201 Some Important Definitions

Arrays and Memory Management

C++ for Java Programmers

Lecture Notes on Memory Management

CSC 211 Intermediate Programming. Arrays & Pointers

Dynamic Memory Allocation

MPATE-GE 2618: C Programming for Music Technology. Unit 5.1

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

EL2310 Scientific Programming

CS 222: Pointers and Manual Memory Management

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

Arrays and Pointers.

Pointers in C/C++ 1 Memory Addresses 2

Dynamic memory allocation (malloc)

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

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

Memory (Stack and Heap)

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Memory Management. CSC215 Lecture

12. Pointers Address-of operator (&)

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

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

Lecture Notes on Memory Management

Launchpad Lecture -10

Pointers and File Handling


Quick review pointer basics (KR ch )

Programs in memory. The layout of memory is roughly:

High Performance Programming Programming in C part 1

CS61, Fall 2012 Section 2 Notes

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

Creating a String Data Type in C

First of all, it is a variable, just like other variables you studied

CSE 307: Principles of Programming Languages

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

COSC Software Engineering. Lecture 16: Managing Memory Managers

CS 61c: Great Ideas in Computer Architecture

Common Misunderstandings from Exam 1 Material

Kurt Schmidt. October 30, 2018

a data type is Types

Fall 2018 Discussion 2: September 3, 2018

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

TI2725-C, C programming lab, course

Pointers and Arrays. Introduction To Pointers. Using The "Address Of" The Operator & Operator. Using The Dereference. The Operator * Operator

Pointers. Reference operator (&) ted = &andy;

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

18-642: Code Style for Compilers

C Programming Basics II

Character Strings. String-copy Example

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Pointers, Arrays, Memory: AKA the cause of those Segfaults

14. Memory API. Operating System: Three Easy Pieces

Recitation #11 Malloc Lab. November 7th, 2017

Intermediate Programming, Spring 2017*

Memory Management in C (Dynamic Strings) Personal Software Engineering

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Transcription:

Dynamic Allocation in C 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically, as the program executes. In C, this is accomplished via the Std Library function malloc() and friends: malloc() calloc() realloc() allocates a block of uninitialized memory; returns the address allocates a block of memory and clears it; returns the address resizes a previously allocated block of memory; returns the address int *A = malloc( 1000 * sizeof(int) ); char *B = malloc( 5000 ); uint64_t Size = 100; double *C = malloc( Size * sizeof(double) );

The Heap 2 stack stack space dynamic allocations Dynamic allocations take place in a region of memory called the "heap". heap memory Successful calls to malloc() return a pointer to a block of memory that is now available for your program to use. The block of memory may be larger than your request (but you will never know that).

Allocating Arrays Dynamically 3 You allocate an array by allocating a suitably-sized block of memory: int N;... // assume N is assigned a value int *A = malloc( N * sizeof(int) ); // allocate array // dynamically for (int pos = 0; pos < N; pos++) { // access using ptr name } A[pos] = pos * pos; Any pointer name can be used with array syntax (bracket notation) but you'd better make sure that the pointee really is an array.

Dynamic Allocation Failure 4 It is always possible that an allocation request will be denied; in that case, malloc() and friends will return NULL. A deadly sin is to not check the return value from malloc() to be sure it isn't NULL: int *pa = malloc( 1000 * sizeof(int) ); if ( pa == NULL ) { fprintf(stderr, "Failed to allocate space for pa!\n"); exit(1); } Without the check of A, the subsequent code will probably lead to a runtime error (unless there was no need for the array).

Deallocation in C 5 One of the most glaring differences between Java and C is how memory deallocation is accomplished. In C, we have static allocations, local or automatic allocations, and dynamic allocations. The first two are of no particular interest here. Everything that your C program allocates dynamically must eventually be deallocated. The responsibility is yours. Failure to deallocate memory in a timely but safe manner is one of the most common programming mistakes in many languages, including C. Deallocation is accomplished by using the Std Library function free(): int *pa = malloc( 1000 * sizeof(int) );... // do stuff with the array free(pa); free() does not reset the value of the pointer on which it is invoked!

C free() 6 It's important to understand just what free() does (and does not do). First, free() can only be applied to a pointer storing the address of a target that was allocated by calling malloc() and friends. Second, free() can only be applied to a pointer whose a target that has not already been deallocated. Third, when free() is invoked on a pointer, the pointer is not automatically reset to NULL. Fourth, free() causes the deallocation of the target of the pointer, not the deallocation of the pointer itself. You don't free a pointer, you free its target.

Array Names are const Pointers 7 So, what's the difference between an array name (static allocation) and a pointer? int N;... // assume N is assigned a value int *A = malloc( 1000 * sizeof(int) ); // allocate array int B[1000]; // declare array for (int pos = 0; pos < N; pos++) { } A[pos] = pos * pos; B[pos] = pos * pos; // access using ptr name // access using array name free( A ); A = NULL; free( B ); B = NULL; // OK; deallocates array A // OK // NO! cannot deallocate static memory // NO! array name is const pointer

Array Notation vs Pointers int A[1000];... int x = A[10]; int *B = malloc(1000*sizeof(int));... int x = *(B + 10); 8 index address B 0 1000 1 1004 1008 10 1040 B + 10 Really means: B + 10 * sizeof(int)

Arithmetic on Pointers So, what's the effect of: int A[1000]; // allocate array; static doesn t matter 9 int *p = A; // p points to A[0] p = p + 100; // where does p point now?? A[100] p = p 50; // now? A[50] p++; // now? A[51] p--; // now? A[50] The effect of adding a value to a pointer depends on the pointer type:

Arithmetic on Pointers 10 The effect of adding a value to a pointer depends on the pointer type: When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

Using Arithmetic on Pointers 11 Here's an implementation of a variation of the C library strlen() function; note the use of pointer arithmetic: uint64_t strlen(const char* Source) { } uint64_t Length = 0; while ( *Source!= '\0' ) { Length++; Source++; } return Length; Source 43 'C' 6F 'o' 6D 'm' 70 'p' 4F 'O' 72 'r' 67 'g' 00 '\0' Length: 0, 1, 2, 3,..., 7

Using Arithmetic on Pointers And here's a slightly more idiomatic version: 12 uint64_t strlen(const char* Source) { uint64_t Length = 0; } while ( *Source++!= '\0' ) Length++; return Length; Precedence note: the dereference operator (*) is evaluated before the increment operator (++) So, we access the current target of Source first, then we move Source to the next array element. QTP: is *Source++ equivalent to (*Source)++?

Using Arithmetic on Pointers 13 Here's an implementation of the C library strcpy() function: char* strcpy(char* Dest, const char* Source) { } int i = 0; while ( true ) { Dest[i] = Source[i]; if (Dest[i] == '\0') break; // we're done i++; } return Dest;

Using Arithmetic on Pointers 14 And here's a version that uses pointer arithmetic to achieve the same effect: char* strcpy(char* Dest, const char* Source) { } while ( (*Dest++ = *Source++)!= '\0' ) ; 43 'C' 6F 'o' 6D 'm' 70 'p' 4F 'O' 72 'r' 67 'g' 00 '\0' Dest Source

Identity vs Equality Revisited 15 x equals y x and y, in some precise sense, have the same value In C, this is equivalent to x == y. x is identical to y x and y are actually the same object In C, this is equivalent to &x == &y. Side notes: If x and y are pointers, then x equals y if and only if x and y have the same target. In other words, two pointers are equal if and only if their targets are identical.

Memory Leaks 16 A memory leak occurs when a process allocates memory dynamically and then fails to deallocate that memory before losing access to it: int *p = malloc(1000 * sizeof(int));... // no calls to free() here p = malloc(1500 * sizeof(int)); // leaked original array... // or here p = NULL; // leaked second array Memory leaks are common in badly-written C/C++ code. Taken to extremes, they can consume all available memory on a system. Garbage-collected memory management automates the process of deallocation, but can never be more efficient than well-written code that deallocates memory manually.

Memory Leaks 17 One issue you'll encounter in testing is that the Linux implementation of malloc() will zero memory when it is allocated to your process. That hides errors rather than fixing them. For better testing, add the following include: #include <malloc.h> Then in main() add this call: mallopt(m_perturb, 205); This will guarantee that memory that is allocated with malloc() or realloc() is NOT automatically written with zeros. We recommend ALWAYS doing this when you're testing code that uses malloc() or realloc(). Or use the tool Valgrind

The Key to Good Memory Management 18 The key to good memory management practice can be stated quite easily: In designing your system, keep careful track of exactly who (i.e., module or function) has ownership of each dynamically-allocated object. Practicing this is not easy. But, with careful attention to detail, and meticulous recording of design decisions about ownership in comments, it is certainly possible to eliminate memory leaks altogether.

Example 19 #define MAX_LINELENGTH 1024 // maximum length guaranteed? // reading a line of text from a file char* line = calloc( MAX_LINELENGTH + 1, sizeof(char) ); fgets(line, MAX_LINELENGTH + 1, fp); // but the line is actually likely to be much shorter than // that, so we can offer to "shrink" it line = realloc( line, sizeof(char)*(strlen(line) + 1) );

Example 20 // copying a string; suppose we have line from last slide // first, make an array of exactly the right size: char* copy = calloc( strlen(line) + 1, sizeof(char) ); // then use strncpy() to duplicate the characters: strncpy(copy, line, strlen(line));