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

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

Memory Analysis tools

Lecture 8 Dynamic Memory Allocation

Debugging (Part 2) 1

Memory Management. CS449 Fall 2017

Memory (Stack and Heap)

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

Princeton University Computer Science 217: Introduction to Programming Systems. Data Structures

Pointers and Memory Management

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

CSE 333 Midterm Exam Sample Solution 7/28/14

CSC 1600 Memory Layout for Unix Processes"

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

CMSC 341 Lecture 2 Dynamic Memory and Pointers

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

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

Use Dynamic Analysis Tools on Linux

Intermediate Programming, Spring 2017*

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

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

Scientific Programming in C IV. Pointers

C Structures & Dynamic Memory Management

Class Information ANNOUCEMENTS

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

Memory Management. CSC215 Lecture

Dynamic Data Structures. CSCI 112: Programming in C

CSE 374 Final Exam 3/15/17. Name UW ID#

Programming. Pointers, Multi-dimensional Arrays and Memory Management

TI2725-C, C programming lab, course

CA341 - Comparative Programming Languages

Dynamic memory allocation (malloc)

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program:

Praktische Aspekte der Informatik

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

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Heap Arrays. Steven R. Bagley

Arrays and Memory Management

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018

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

Dynamic memory allocation

CS 222: Pointers and Manual Memory Management

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Both parts center on the concept of a "mesa", and make use of the following data type:

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

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

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

C PROGRAMMING Lecture 5. 1st semester

6.S096: Introduction to C/C++

CS61, Fall 2012 Section 2 Notes

CS 11 C track: lecture 5

DAY 3. CS3600, Northeastern University. Alan Mislove

The Valgrind Memory Checker. (i.e., Your best friend.)

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

COSC Software Engineering. Lecture 16: Managing Memory Managers

Dynamic Memory Management

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

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

CS 2461: Computer Architecture I: Heap: Dynamic Memory Allocation Data Structures. Recap. Variables and Structures

The Valgrind Memory Checker. (i.e., Your best friend.)

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

A Capacity: 10 Usage: 4 Data:

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

Dynamically Allocated Memory in C

Processes. Johan Montelius KTH

Memory Allocation. General Questions

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

Dynamic Allocation in C

A process. the stack

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Systems Programming and Computer Architecture ( )

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

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

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

ECE551 Midterm Version 2

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Content. In this chapter, you will learn:

Dynamic Allocation of Memory

Memory and C/C++ modules

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

ECE 551D Spring 2018 Midterm Exam

CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory

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

Example: Runtime Memory Allocation: Example: Dynamical Memory Allocation: Some Comments: Allocate and free dynamic memory

14. Memory API. Operating System: Three Easy Pieces

Dynamic Allocation in C

Kurt Schmidt. October 30, 2018

POINTER AND ARRAY SUNU WIBIRAMA

ME964 High Performance Computing for Engineering Applications

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

ECE551 Midterm Version 1

CSE 160 Discussion Section. Winter 2017 Week 3

Heap Arrays and Linked Lists. Steven R. Bagley

Reminder: compiling & linking

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

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

Arrays and Pointers (part 1)

CSCI-1200 Data Structures Spring 2013 Lecture 7 Templated Classes & Vector Implementation

Transcription:

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

The Easy Way Java (JVM) automatically allocates and reclaims memory for you, e.g... Removed object is implicitly reclaimed (garbage collected) when there are no longer any references to it public class LinkedList {... public void addfirst (Object obj) { Node newnode = new Node(); newnode.data =...; newnode.next = first; first = newnode; } public Object removefirst() { if (first == null) throw new emptyexception(); Object obj = first.data; first = first.next; return obj; }... } CSC230: C and Software Tools NC State Computer Science Faculty 2

The Harder Way C requires you to manually allocate and reclaim memory, e.g... Programmer explicitly indicates there are no more references to the removed object void addfirst (Object obj) { Node * newnode = (Node *) malloc (sizeof(node)); assert( newnode!= NULL ); newnode->data =...; newnode->next = first; first = newnode; } Object removefirst() { assert (first!= NULL); Node * old = first; Object obj = first->data; first = first->next; free (old); return obj; } CSC230: C and Software Tools NC State Computer Science Faculty 3

Increasing memory addresses Memory Layout of a Program The heap is an area of virtual memory available for dynamic (runtime) memory allocation Instructions (Code) Static Data Statically allocated The Heap The Stack Dynamically allocated CSC230: C and Software Tools NC State Computer Science Faculty 4

Why Dynamic Memory Allocation? Don't know how much data will need to be stored until runtime; choices? Choice 1: Declare static array of maximum size that could possibly occur #define MAXCLASSSIZE 500 struct student { definition here }; struct student students[maxclasssize]; int i = 0; while (more_students && (i < MAXCLASSSIZE)) readstudents (students[i++]); CSC230: C and Software Tools NC State Computer Science Faculty 5

Why Dynamic (cont d) Choice 2: Declare dynamic (auto) array of specific size needed, at run time int main (void) { int maxnum; printf( Number of students in class? \n ); scanf( %d, &maxnum); struct student students[maxnum]; } int i = 0; while (more_students && (i < maxnum)) readstudents (students[i++]); CSC230: C and Software Tools NC State Computer Science Faculty 6

Why Dynamic (cont d) Choice 3: Allocate memory dynamically using a standard library function (malloc or calloc) #include <stdio.h> #include <stdlib.h> int main(void) { struct student *sp; while (more_students) { sp = (struct student *) calloc (num, sizeof(struct student)); if (sp!= NULL) readstudents (sp); } } CSC230: C and Software Tools NC State Computer Science Faculty 7

The sizeof Operator Not a function call; a C operator returns number of bytes required by a data type Return value is of predefined type size_t #include <stdlib.h> size_t tsz1, tsz2, tsz3; int a; float b[100]; struct student { definition here } st; tsz1 = sizeof (a); /* 4 */ tsz2 = sizeof (b); /*? */ tsz3 = sizeof (st); /*? */ what are these sizes? CSC230: C and Software Tools NC State Computer Science Faculty 8

The calloc() Standard Library Function Syntax: void * calloc (size_t num, size_t sz) Generic pointer, must be cast to type of result (Sometimes optional on modern compilers) OS allocates (num * sz) bytes of contiguous storage (all bytes initialized to zeros) struct student * students; students = (struct student *) calloc (num, sizeof(struct student)); int * ip; ip = (int *) calloc (1, sizeof (int)); char *cp; cp = (char *) calloc (1000, sizeof (char)); CSC230: C and Software Tools NC State Computer Science Faculty 9

calloc() (cont d) Return value is starting address of the storage allocated If not enough memory available, returns NULL Could also be a unique pointer that could be passed to free() always check for this error common source of bugs failure to check return value cp = (char *) calloc (1000, sizeof (char)); if (cp == NULL) { printf( Cannot allocate memory; exiting\n ); exit (-1); } CSC230: C and Software Tools NC State Computer Science Faculty 10

The malloc() Std. Lib. Function Syntax: void * malloc (size_t sz) OS allocates sz bytes of contiguous storage ( uninitialized ) Returns starting address of storage common source of bugs malloc() does not initialize memory If size is 0, returns NULL or unique pointer that can be freed students = (struct student *) malloc ( num * sizeof(struct student)); ip = (int *) malloc (sizeof (int)); cp = (char *) malloc ( 1000 * sizeof (char)); CSC230: C and Software Tools NC State Computer Science Faculty 11

The realloc() Std. Lib. Function Syntax: void * realloc(void * ptr, size_t sz) Grows or shrinks allocated memory ptr must be dynamically allocated Growing memory doesn t initialize new bytes If can t expand, returns NULL Old memory is unchanged If ptr is NULL, behaves like malloc If sz is NULL, behaves like free Memory shrinks in place Memory may NOT grow in place If not enough space, will move to new location and copy contents Old memory is freed Update all pointers!!! CSC230: C and Software Tools NC State Computer Science Faculty 12

The free() Standard Library Function Syntax: void free (void * ptr) no way to check for errors! ptr must have been previously allocated by malloc() or calloc() no need to specify amount of memory to be freed; why not? Frees (for other uses) memory previously allocated free(students); free (ip); free (cp); Why bother freeing up memory? common source of bugs failure to free unused memory CSC230: C and Software Tools NC State Computer Science Faculty 13

Dynamic memory function summary void *malloc(size_t size); Give me size bytes, don t initialize them void *calloc(size_t nmemb, size_t size); Give me nmemb*size bytes, initialize them to 0 void *realloc(void *ptr, size_t size); Take this pointer and make the space it refers to bigger/smaller (moving it if necessary). void free(void *ptr); I m done using the memory here, you can have it back. CSC230: C and Software Tools NC State Computer Science Faculty 14

Dynamic Memory Allocation Mistakes These bugs can really be hard to find and fix may run for hours before the bug pops up, and in a place that appears to have no relationship to the actual cause of the error CSC230: C and Software Tools NC State Computer Science Faculty 15

Mistake M1: Invalid Pointers common source of bugs Problems? int i, j, result; result = scanf ( %d %d, i, &j); char *ptr; ptr = 'A'; *ptr = 'B'; CSC230: C and Software Tools NC State Computer Science Faculty 16

Invalid Pointers (cont d) common source of bugs Problems? int * f( void ) { int val; return &val; } why is this a problem? CSC230: C and Software Tools NC State Computer Science Faculty 17

Invalid Pointers (cont d) common source of bugs Problems? Fix? dynamically allocate and construct a linked list /* now list is no longer needed, * free memory */ for (p = head; p!= NULL; p = p->next) free(p); why is this a problem? CSC230: C and Software Tools NC State Computer Science Faculty 18

M2: Not Initializing Memory Problems? common source of bugs int * sumptr; int ival[100] = { initial values here }; int i; sumptr = (int *) malloc ( sizeof(int) ); for (i = 0; i < 100; i++) *sumptr += ival[i]; CSC230: C and Software Tools NC State Computer Science Faculty 19

common source of bugs M3: Stack Buffer Overflows void bufoverflow (void) { char buf[64]; } gets(buf); return; Problems? One of the biggest sources of security problems CSC230: C and Software Tools NC State Computer Science Faculty 20

M4: Writing Past End of common source of bugs Dyn. Allocated Memory int i, sz; int *ip, *jp; scanf ( %d, &sz); ip = (int *) calloc (sz, sizeof(int)); check for errors here jp = ip; for (i = 0; i <= sz; i++) scanf ( %d, jp++); why is this a problem? CSC230: C and Software Tools NC State Computer Science Faculty 21

M5: Freeing Unallocated Memory Problems? common source of bugs int i; int *ip; ip = &i; free(ip); why is this a problem? CSC230: C and Software Tools NC State Computer Science Faculty 22

Freeing Unallocated (cont d) Problems? int *ip; common source of bugs ip = (int *) calloc (1000, sizeof(int)); free(ip); free(ip); CSC230: C and Software Tools NC State Computer Science Faculty 23

common source of bugs M6: Memory Leaks Allocated memory is referenced using pointer returned by allocation If you lose pointers (free them, change to another address), you can no longer reference or free allocated memory Common problem in large, long-running programs (think: servers) over time, memory footprint of program gets bigger, bigger, CSC230: C and Software Tools NC State Computer Science Faculty 24

M6: Memory Leaks common source of bugs void leak (int n) { int * xp; xp = (int *) malloc (n * sizeof(int)); memory is used and then no longer needed return; } why is this a problem? CSC230: C and Software Tools NC State Computer Science Faculty 25

M6: Memory Leaks Valgrind software tool for detecting memory leaks on actual program executions Compile with g option Arguments: --leak-check=yes % gcc Wall std=c99 g program.c o program % valgrind leak-check=yes./program CSC230: C and Software Tools NC State Computer Science Faculty 26

==15703== Memcheck, a memory error detector ==15703== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==15703== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==15703== Command:./memory ==15703== ==15703== Invalid write of size 1 ==15703== at 0x40055E: f (memory.c:9) ==15703== by 0x40058E: main (memory.c:15) ==15703== Address 0x4c41043 is 0 bytes after a block of size 3 alloc'd ==15703== at 0x4A0577B: calloc (vg_replace_malloc.c:593) ==15703== by 0x400523: f (memory.c:6) ==15703== by 0x40058E: main (memory.c:15) ==15703== ==15703== Invalid read of size 1 ==15703== at 0x3AF5C480AC: vfprintf (in /lib64/libc-2.12.so) ==15703== by 0x3AF5C4F409: printf (in /lib64/libc-2.12.so) ==15703== by 0x400579: f (memory.c:10) ==15703== by 0x40058E: main (memory.c:15) ==15703== Address 0x4c41043 is 0 bytes after a block of size 3 alloc'd ==15703== at 0x4A0577B: calloc (vg_replace_malloc.c:593) ==15703== by 0x400523: f (memory.c:6) ==15703== by 0x40058E: main (memory.c:15) ==15703== String = abc ==15703== ==15703== HEAP SUMMARY: ==15703== in use at exit: 3 bytes in 1 blocks ==15703== total heap usage: 1 allocs, 0 frees, 3 bytes allocated ==15703== ==15703== 3 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==15703== at 0x4A0577B: calloc (vg_replace_malloc.c:593) ==15703== by 0x400523: f (memory.c:6) ==15703== by 0x40058E: main (memory.c:15) ==15703== ==15703== LEAK SUMMARY: ==15703== definitely lost: 3 bytes in 1 blocks ==15703== indirectly lost: 0 bytes in 0 blocks ==15703== possibly lost: 0 bytes in 0 blocks ==15703== still reachable: 0 bytes in 0 blocks ==15703== suppressed: 0 bytes in 0 blocks ==15703== ==15703== For counts of detected and suppressed errors, rerun with: -v ==15703== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 6 from 6) CSC230: C and Software Tools NC State Computer Science Faculty 27

Garbage Collection Some language run-time systems free up unused memory automatically for the programmer accomplished through "reachability analysis Java Student st = new Student("John Smith"); st = null; // space for student st is // automatically reclaimed CSC230: C and Software Tools NC State Computer Science Faculty 28

Exercise 18a: Crash ideone Write a program that allocates memory infinitely, 1kB at a time. Print a counter for each allocation. See how much you can allocate before ideone kills it. Don t run it on a shared NCSU system! CSC230 - C and Software Tools NC State University Computer Science Faculty Reminder: Go to course web page for link to exercise form. Paste code into ideone.com and submit the link. 29