CSCI206 - Computer Organization & Programming

Slides:



Advertisements
Similar presentations
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Advertisements

Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Pointers Applications
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lecture 13 Static vs Dynamic Memory Allocation
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
© 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.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
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.
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
Lesson One – Creating a thread
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
Day 03 Introduction to C.
Dynamic Memory Allocation
Programming Languages and Paradigms
Dynamic Memory Allocation
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
CSCI206 - Computer Organization & Programming
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Topic 3-b Run-Time Environment
Dynamic Memory A whole heap of fun….
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
Chien-Chung Shen CIS/UD
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
EECE.2160 ECE Application Programming
Dynamic Memory A whole heap of fun….
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CSCI206 - Computer Organization & Programming Structures, Pointers, and the Heap Dynamic Memory with Malloc and Free zyBook: 9.7, 9.8, 9.9, 9.10, 9.14, 9.15

Structures This only defines a type. It does not allocate storage. Don’t forget the semicolon; This only defines a type. It does not allocate storage.

Structures type definition create 10 instances (allocate memory) the “.” selection operator selects from structs

Structure Initializer

Pointers to Structures struct employee{ char name[10]; int id; float wage; }; struct employee john = {"John", 42, 99999}; struct employee *pEmployee = &john; printf ("Hi %s\n", (*pEmployee).name); printf ("Hi %s\n", pEmployee->name); dereference then select select from reference

Pointers and the Heap The heap memory segment is used for shared dynamic memory. In C, we use the heap with malloc and free. Dynamic (runtime) allocation Static (compile time) allocation

malloc The malloc() function allocates size bytes (on the heap) and returns a pointer to the allocated memory. malloc does not initialize memory void *malloc(size_t size); size_t is an unsigned integer type used by the system libraries. It could be 32-bit or 64-bits. It is used for consistency in the system libraries.

What is a void pointer? There is one malloc function that is used to allocate memory for any datatype, so it returns an untyped void pointer. typically these pointers are cast to the appropriate type by the caller: int *pi = (int*) malloc(sizeof(int)); Note that pi is a pointer to int, doesn’t hold memory until malloc or the like is called.

Typecasting pointers struct employee a; struct student *b; b = (struct student*) &a; b now accesses the memory allocated to a as a struct student object. It is up to the programmer to determine if this is correct.

malloc can fail! If your system is out of heap memory, malloc will return NULL. The programmer is responsible for checking if the returned pointer is NULL before using the memory!

Arrays and malloc Array elements are stored in contiguous blocks of memory and can be accessed using pointer arithmetic. #define MAX_E 512 struct employee{ char name[10]; int id; float wage; }; struct employee *employees = (struct employee *) malloc(MAX_E * sizeof(struct employee)); struct employee employees[MAX_E]; // same as above but allocated on stack/data segment

free your memory Memory allocated by malloc is reserved on the heap as long as your program is running. It does not go out of scope when functions exit (like stack memory). Your program is responsible for tracking all allocated memory! (don’t lose the returned pointers!) void *p = malloc(1000); You must save the return value from malloc!

Using free When you are done, call free with the malloced pointer. Memory is marked “free” and could be re-allocated by other malloc calls later. After calling free your program must not use any pointers to freed memory! int *i = malloc(sizeof(int)); free (i); // free i *i = 42; // i is no longer valid!