Session #6 Memory Allocation and leaks Structures

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Dynamic memory allocation
Introduction to Programming Lecture 39. Copy Constructor.
Pointers in C Rohit Khokher
Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
CEG 221 Lesson 2: Homogeneous Data Types Mr. David Lippa.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
CSE 220 – C Programming malloc, calloc, realloc.
Pointers to pointers & multi-dimensional arrays
Pointers verses Variables
Memory allocation & parameter passing
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
(Numerical Arrays of Multiple Dimensions)
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Day 03 Introduction to C.
ENEE150 Discussion 07 Section 0101 Adam Wang.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 03 Introduction to C.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Visit for more Learning Resources
Programming Languages and Paradigms
Pointers.
14th September IIT Kanpur
Buy book Online -
Programming and Data Structures
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Multidimensional Arrays
By Hector M Lugo-Cordero September 17, 2008
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Introduction to Problem Solving and Programming
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Multidimensional array
Pointers and Arrays Beyond Chapter 16
Dr Tripty Singh Arrays.
C (and C++) Pointers April 4, 2019.
C Programming Lecture-8 Pointers and Memory Management
Structures In C Programming By Rajanikanth B.
C Programming Pointers
READING AND PRINTING MATRICES (with functions)
C Structures and Memory Allocation
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Arrays and Pointers.
Presentation transcript:

Session #6 Memory Allocation and leaks Structures C Tutorial Session #6 Memory Allocation and leaks Structures

Dynamically allocate memory Review: malloc and free malloc allocates a chunk of memory and returns the starting address of the memory. free frees memory. Note: you are responsible for releasing the memory allocated by malloc function.

An example of malloc char *name = (char *)malloc(namelen * sizeof(char)); name Size: namelen

Sample code int main(){ char name[] = “I am a string.”; char *temp; temp = (char *)malloc( (strlen(name)+1) * sizeof(char)); strcpy(temp, name); … free(temp); }

Caution: memory leak char str1[] = “string one”; char str2[] = “string two”; char *temp; temp = (char *)malloc((strlen(str1) +1) * sizeof(char)); strcpy(temp, str1); temp = (char *)malloc((strlen(str2) +1) * sizeof(char)); strcpy(temp, str2); Free(str2);

Two dimensional dynamic array Use pointer to pointers. Syntax: <type> **name; Example: int **arr; arr is a pointer to an integer pointer. It can be used as a 2-dim array.

Sample usage Suppose we need a 2-dim array to store a matrix. However, neither the column size nor the row size of the matrix is known. Thus, we need to use a 2-dim dynamic array. Code: int **matrix; Question: how do we allocate 2-dim memory.

Matrix example int **matrix; matrix = (int **)malloc(row * sizeof(int *)); for (i = 0; i< col; i++) { matrix[i] = (int *)malloc(col *sizeof(int)); } Caution: we need a for loop to free those memory.

Dynamically change array size Sometimes we need to change array size in the middle of programs. The realloc function. Syntax: realloc(baseadd, newsize);

Example of using realloc char f_name[] = “George”; char l_name[]= “Bush”; char *name = (char *)malloc((strlen(f_name)+1) * sizeof(char)); strcpy(name, f_name); name = (char *)realloc(name, (strlen(f_name)+strlen(l_name)+1) * sizeof(char)); strcat(name, l_name);

Introduction to structures Choosing a good way to represent data is very important. Example: an inventory of book. struct book { char title[max]; char author[max]; float value; } struct book library; //declare a book variable.

Gaining access to structure members struct book library; Gets(library.title); Scanf(“%f”, &library.value);

Array of structures You can define an array of structures: struct book books[10]; Later, you can access it like regular arrays: books[0].value // the value of the first element. books[4].title //the title of the 5th element.