2016.

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
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.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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{
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Pointers Applications
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
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,
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 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
+ 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.
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;
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
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.
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.
CSE 220 – C Programming malloc, calloc, realloc.
PROGRAMMING II( Dynamic Memory II)
C Programming Types & Dynamic memory & bits & others
Memory allocation & parameter passing
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Day 03 Introduction to C.
Introduction to Programming
Dynamic Memory Allocation Strings
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 03 Introduction to C.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Dynamic Memory Allocation
Some examples.
CS157: Dynamic Memory Dynamic Memory 11/9/201804/25/06.
14th September IIT Kanpur
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory Allocation CS 217.
Functions, Part 2 of 3 Topics Functions That Return a Value
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Outline Defining and using Pointers Operations on pointers
Qsort.
CSC215 Homework Homework 06 Due date: Oct 30, 2016.
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.
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
Arrays, Pointers, and Strings
Programming in C Advanced Pointers.
Dynamic Memory – A Review
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

2016

Memory management v2 calloc()

Don’t take memory for granted! Memory is a limited resource Last time we used to allocate memory without hesitation, but what happens if we don’t have enough? #include <stdio.h> #include <stdlib.h> int main(void) { int *ptr1, *ptr2; ptr1 = (int*)malloc(sizeof(int) * 100); ptr2 = (int*)malloc(sizeof(int) * 5000000000000); printf("ptr1 adr = %p\nptr2 adr = %p", ptr1, ptr2); free(ptr1); free(ptr2); return 0; } 2016

Don’t take memory for granted! Memory is a limited resource Last time we used to allocate memory without hesitation, but what happens if we don’t have enough? Memory is allocated as one continuous block 2016

Sample 1 – checking for allocation #include <stdio.h> #include <stdlib.h>   int main(void) { int *ptr; ptr = (int*)malloc(sizeof(int) * 50000000000000000); if (!ptr) printf("Not enough memory"); exit(1); } free(ptr); return 0; 2016

calloc(); calloc() function prototype: void* calloc (size_t num, size_t size); Return type void* - void pointer to the beginning of the allocated block 1. parameter num - (size_t) – how many elements 2. parameter size - (size_t) – how many bytes per block Compared to malloc(), the memory will be zero-initialized In code: ptr = (int*)calloc(n, sizeof(int)); 2016

Sample 2 - calloc #include <stdio.h> #include <stdlib.h>   #define N 5 int main(void) { int *ptr, i; ptr = (int*)calloc(N, sizeof(int)); if (!ptr) printf("Not enough memory"); exit(1); } for (i = 0; i < N; i++) printf("%d\n", *(ptr + i)); free(ptr); return 0; 2016

Initial values for memory #include <stdio.h> #include <stdlib.h>   #define N 5 int main(void) { int *ptr1 = (int*)malloc(N * sizeof(int)); int *ptr2 = (int*)calloc(N, sizeof(int)); int i; printf("\t%10s\t%10s\n", "Malloc", "Calloc"); for (i = 0; i < N; i++) printf("%4d\t%10d\t%10d\n", i, *(ptr1 + i), *(ptr2 + i)); free(ptr1); free(ptr2); return 0; } 2016

sizeof() vs strlen() #include <stdio.h> #include <string.h>   int main(void) { char arr[10] = "Hello!"; printf("%d\n", sizeof("Hello!")); printf("%d\n", strlen("Hello!")); printf("%d\n", sizeof(arr)); printf("%d\n", strlen(arr)); printf("%d\n", sizeof(char)); printf("%d\n", sizeof(char*)); return 0; } 2016

sizeof() vs strlen() with functions void Print1(char *arr){ printf("1: %d %d\n", sizeof(arr), strlen(arr)); } void Print2(char arr[]){ printf("2: %d %d\n", sizeof(arr), strlen(arr)); void Print3(char arr[10]){ printf("3: %d %d\n", sizeof(arr), strlen(arr)); int main(void){ char arr[10] = "Hello!"; printf("0: %d %d\n", sizeof(arr), strlen(arr)); Print1(arr); Print2(arr); Print3(arr); return 0; 2016

Allocation memory to structure members (pseudo, single structures) typedef struct identity { int num; char *name; } identity; identity *person; person = (identity*)calloc(1, sizeof(identity)); person->name = (char*) calloc(strlen(buf) + 1, sizeof(char)); } free(person->name); free(person); 2016

Lab task Read N (user specified) records, allocate memory using calloc() Structure of a record: Identification (integer) (auto_increment) First name (string) Last name (string) Age (integer) You must allocate memory to names (string members) separately. Memory allocated must be exact. Find and display people within the user specified age range (x <= age <= y) Free everything before exiting (including memory allocated for the char* member of the structure) Check for leaks using valgrind!!! 2016

Advanced Picture on the first slide: Advanced for lab task Make the text from the picture on the first slide compile and run without any warnings or errors. The program must output the message sent by the duck using the help from willy. Advanced for lab task Sort by last name before output. If the last names match, sort by first name Make the search criteria selectable (younger than, older than, in between) How would you go on about reading all of the records (unknown count?) 2016