Dynamic memory allocation

Similar documents
Memory Management. CSC215 Lecture

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Fundamental of Programming (C)

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

Dynamic memory allocation (malloc)

Understanding Pointers

Processes. Johan Montelius KTH

A process. the stack

Array Initialization

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

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

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

Quick review pointer basics (KR ch )

POINTER AND ARRAY SUNU WIBIRAMA

Lab 3. Pointers Programming Lab (Using C) XU Silei

ESC101N: Fundamentals of Computing End-sem st semester

Arrays and Pointers. CSE 2031 Fall November 11, 2013

211: Computer Architecture Summer 2016

Secure Software Programming and Vulnerability Analysis

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

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

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

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

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

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

Arrays and Pointers in C. Alan L. Cox

Content. In this chapter, you will learn:

Lecture 8 Dynamic Memory Allocation

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

Memory Allocation in C

Subject: Fundamental of Computer Programming 2068

Memory (Stack and Heap)

Memory Management. CS449 Fall 2017

Procedural programming with C

Lecture 04 FUNCTIONS AND ARRAYS

Arrays and Memory Management

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

int marks[10]; // fixed size and fixed address No change in Memory address.

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

CSCI 171 Chapter Outlines

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

Recitation: C Review. TA s 20 Feb 2017

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

Recitation 2/18/2012

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Answer all questions. Write your answers only in the space provided. Full marks = 50

C programming basics T3-1 -

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

211: Computer Architecture Summer 2016

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

UNIT III (PART-II) & UNIT IV(PART-I)

C Structures & Dynamic Memory Management

Intermediate Programming, Spring 2017*

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

Kurt Schmidt. October 30, 2018

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

PRINCIPLES OF OPERATING SYSTEMS

Dynamic Data Structures. CSCI 112: Programming in C

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

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

MODULE 5: Pointers, Preprocessor Directives and Data Structures

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

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

Dynamic Memory Allocation and Command-line Arguments

Advanced Pointer Topics

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CS 11 C track: lecture 5

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

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

LSN 3 C Concepts for OS Programming

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

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

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

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

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

Computer Programming Unit 3

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

de facto standard C library Contains a bunch of header files and APIs to do various tasks

14. Memory API. Operating System: Three Easy Pieces

High Performance Programming Programming in C part 1

Dynamic Allocation of Memory

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

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

Run-time Environments

Final CSE 131B Spring 2004

Run-time Environments

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

C PROGRAMMING Lecture 5. 1st semester

CS201- Introduction to Programming Current Quizzes

Lecture 04 Introduction to pointers

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

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

Programs in memory. The layout of memory is roughly:

Transcription:

Dynamic memory allocation

outline Memory allocation functions Array allocation Matrix allocation Examples

Memory allocation functions (#include <stdlib.h>) malloc() Allocates a specified number of bytes in memory. Returns a pointer to the beginning of the allocated block. calloc() Similar to malloc(), but initializes the allocated bytes to zero. This function allows you to allocate memory for more than one object at a time. realloc() Changes the size of a previously allocated block. free() Frees up memory that was previously allocated with malloc(), calloc(), or realloc().

Memory allocation functions void *malloc(size_t size); // size: size of the memory block, in bytes void *calloc(size_t nmemb, size_t size); // the 1 st arg: number of the objects to reserve the memory, the 2 nd arg: size of each object. void *realloc(void *ptr, size_t size); //changes the size of the memory block pointed by ptr. The function may move the memory block to a new location (whose address is returned by the function). void free(void *ptr); // deallocate the memory block pointed by ptr

Array allocation #include <stdio.h> #include <stdlib.h> int main() { int n; int *list; printf("how many numbers are you going to enter?"); scanf("%d", &n); list = (int *) malloc( n * sizeof(int) ); // list = (int *) calloc( n, sizeof(int) ); if(list==null) { printf("can not allocate memory for the array...\n"); return -1; } return 0; }

Matrix allocation int **mat; int n,m; printf( Please enter number of rows ); scanf( %d, &n); printf( Please enter number of columns ); scanf( %d, &m); mat = (int **) malloc( n * sizeof(int *) ); if(mat == NULL) { printf( Can not allocate memory for the array...\n ); return -1; } for(i = 0; i < n; i++) { mat[i] = (int *)malloc(m * sizeof(int) ); }

Example-1 Write a simple program ask number of elements in the array allocate necessary space ask for elements sort the array

Example-2 Write a simple program ask maximum possible size of a string allocate necessary space for this string ask maximum possible size of another string allocate necessary space for the 2 nd string read the two strings, sequentially, find the 2 nd string within the 1 st one, return the starting position of str2 in str1; return -1 if not found.

Functions

Outline Passing arguments pass by reference, pass by value Declarations and calls definition, allusion, function call Examples Recursion The main function Function pointers

Passing arguments Because C passes arguments by value, a function can assign values to the formal arguments without affecting the actual arguments If you want a function to change the value of an object, you must pass a pointer to the object and then make an assignment through the dereferenced pointer. remember scanf function!!!

Declarations and calls Definition Actually defines what the function does, as well as number and type of arguments Function Allusion Declares a function that is defined somewhere else Also specifies what kind of value the function returns. Function Call Invokes a function, causing program execution to jump to the next invoked function. When the function returns, execution resumes at the point just after the call

Function definition A very simple example no arguments no return A relatively complex example a function to calculate factorial n void simplefunction1 ( void ) { printf( \nthis is simplefunction1\n ); } int factorial( int n) { int i,f=1; for(i=2;i<=n;i++) f = f * i; return f; }

Function allusion void simplefunction1( void ); // prototype of the function simplefunction1(); // alternative to the above allusion extern float simplefunction2(); // no input argument, returns float int factorial( int ); // takes integer, returns integer void sortarray(int *, int); float *mergesort(float *, int, float *, int, int *); // takes 1 int-pointer, 1 int, returns nothing

Function call function name ( ), argument printf( Hello World\n ); printf( Result is %d\n, factorial(10)); scanf( %s, str); x = factorial(n) / factorial(m); printf( %d : %d : %s : %d\n, i, j, line, rc); matt = transpose(mat, rows, cols); Number of the arguments in the function-definition and function-call should be consistent. When we call a function, argument types should be consistent and in the same order as they defined.

Order of functions In order to use a function you must define it beforehand. In order to use your own function in the main() function, you should define it before the main() in the same file It is also possible to use function allusion (function prototype) You can write the prototype of your function before the main() function and use it anywhere (main() or any other function of yours)

Function arguments

Passing arrays as function parameter Several ways to do it Do NOT forget no boundary checking! remember your motivation to create a function Using actual array size void myfunction( int ar[5] ) Using array and a size parameter void myfunction( int ar[], int size ) Using a pointer and an integer void myfunction( int *ar, int size )

How to return an array from a function We don't return an array from functions, rather we return a pointer holding the base address of the array to be returned. We must, make sure that the array exists after the function ends! you can NOT return local arrays! SOLUTION : dynamic memory allocation + pointers

EXAMPLE -1 Create a sort function for one dimensional arrays Use any type of sorting algorithm

EXAMPLE -2 Write a function that compresses a sparse matrix The function should take a matrix as a parameter The function should return a new matrix 3 x n or n x 3

RECURSION A recursive function is one that calls itself. An example is given on the right side It is important to notice that this function will call itself forever. Actually not forever, but till the computer runs out of stack memory It means a runtime error Thus, remember to include a stop point in your recursive functions. void recurse () { static count = 1; printf( %d\n, count); count++; recurse(); } main() { recurse(); }

Recursion When a program begins executing in the function main(), space is allocated on the stack for all variables declared within main(), Figure 14.13(a) If main() calls a function, func1(), additional storage is allocated for the variables in func1() at the top of the stack Figure 14.13(b) Notice that the parameters passed by main() to func1() are also stored on the stack. When func1() returns, storage for its local variables is deallocated, and the top of the stack returns to the 1 st position Figure 14.13(c) As can be seen, the memory allocated in the stack area is used and reused during program execution. It should be clear that memory allocated in this area will contain garbage values left over from previous usage.

Recursion A few examples to solve with recursion Factorial n! Fibonacci numbers F n+1 = F n + F n-1 Binary search Depth-first search int fact( int n ) { if( n <= 1 ) return 1; else return n*fact(n-1); } main() { printf( 5! is %d\n, fact(5)); }

Recursıon A few examples to solve with recursion Factorial n! Fibonacci numbers F n+1 = F n + F n-1 Binary search Depth-first search

MAIN() FUNCTION All C programs must contain a function called main(), which is always the first function executed in a C program. When main() returns, the program is done. The compiler treats the main() function like any other function, except that at runtime the host environment is responsible for providing two arguments argc number of arguments that are presented at the command line argv an array of pointers to the command line arguments main(int argc, char *argv[]) { while(--argc > 0 ) printf( %s\n, *++argv); exit(0); }

MAIN() FUNCTION A better way to handle command line arguments getopt The getopt() function parses the command-line arguments. Its arguments argc and argv are the argument count and array as passed to the main() function on program invocation. The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1. If there are no more option characters, getopt() returns -1. #include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; while ((c = getopt (argc, argv, "abc:"))!= -1) switch (c)... { case 'a': aflag = 1; break; default: abort (); } http://www.gnu.org/software/libc/manual/html_node/example-of-getopt.html#example-of-getopt