Introduction to C Programming CE00312-1 Lecture 18 Dynamic Memory Allocation and Ragged Arrays.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
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:
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Introduction to C Programming CE Lecture 13 Strings in C.
Introduction to C Programming CE
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Introduction to C Programming CE Lecture 14 Strings and Searching.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lecture 13 Static vs Dynamic Memory Allocation
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Dynamic memory allocation and Pointers Lecture 4.
Introduction to Programming 3D Applications CE Lecture 12 Structure Pointers and Memory Management in C.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
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.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Pointers *, &, array similarities, functions, sizeof.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Introduction to Programming 3D Applications CE Lecture 10 Pointers in C.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSC Programming for Science Lecture 34: Dynamic Pointers.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Day 03 Introduction to C.
Introduction to Programming
Pointers Department of Computer Science-BGU יום שלישי 31 יולי 2018.
Day 03 Introduction to C.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Dynamic Memory Allocation
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Lecture 18 Arrays and Pointer Arithmetic
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 Chapter 11 Copyright © 2008 W. W. Norton & Company.
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Dynamic Memory – A Review
Chapter 16 Pointers and Arrays
Presentation transcript:

Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays

Allocation of Static Memory A declaration such as char name[1000][21], allocates sufficient memory for 1000 names of 21 characters each. This storage of characters is reserved at compilation time, i.e. before the program begins execution. This wastes space as most names are rather less than 20 letters. It also restricts names to a maximum of 20 characters. We can resolve this wastage and restriction by allocation of memory as and when required, during execution of the program.

Dynamic Allocation using malloc function A standard function, malloc(n) allocates n bytes (for n characters) in memory and returns a pointer to it. For example: the following code Prompts the user for the size of the string Reads the size Allocates memory for a string of this size Reads the string

Example using malloc int size; char *s; printf(“\nhow many characters?\n”); scanf(“%d”, &size); s = malloc(size+1);// one extra for ‘\0’ printf(“type string\n”); gets(s); allocates only enough memory for the expected string.

Use of malloc for any data type malloc can be used to allocate memory for any. * ; // pointer to // some other code here = ( *)malloc(n * sizeof( )); allocates sufficient memory for n values of and returns a pointer which must be casted as the same pointer type as the.

Using malloc for an array of integers For example: To allocate storage for 10 integers int *p; p = (int *) malloc (10 * sizeof(int)); This is machine-independent, as sizeof returns size of an integer for any machine.

Ragged Arrays Declare 6 variable length names – array of string pointers char *name[6] = {“abdul”, “abraham”, “al “, “bill”, “fred”, “jean-pierre”}; We can declare functions on ragged arrays: void print_list(char *table1[ ], int n) so that it can deal with strings of any length.

Ragged array example Read a list of strings (one on each line) into an array where the size of each element is precisely the length of each string, ie the strings are of variable length - a ragged array. #include "stdio.h" #include "string.h" #include "stdlib.h"// for malloc //prototypes void sort(char *[], int); int read_strings(char *[], int); void print_strings(char *[], int);

Main function int main(void) { char *list[1000]; // array of 1000 string pointers int n;// actual number of strings n = read_strings(list, 1000); // read n strings, max of 1000 sort(list, n);// sort n strings print_strings(list, n);// print n strings return (0); }

print_strings function void print_strings(char *list[], int n) { int i; printf("\nalphabetical order is\n\n"); for (i=0; i<n; i++) { printf("%s\n", list[i]); }

void sort(char *a[], int n) {// array of n strings as pointers int i, j; char *temp;// string pointer for (j = n -1; j > 0; j--) {// each pass of j comparisons for (i=0; i < j; i++) {//each comparison if (strcmp(a[i], a[i+1]) > 0) {// swap the POINTERS temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; }// else no swap }// end of pass }// end of all passes } // end sort

int read_strings(char *list[], int max) { int i = 0; char line[81];// store for each line printf("\ntype one string per line\n"); while (gets(line) != NULL && i < max) {// while not end of input list[i] = malloc (strlen(line)+1); // list[i] big enough // for line + 1 for ‘\0’ strcpy(list[i], line); // copy line into list[i] i++;// next element } return i; // actual number of strings }