Presentation is loading. Please wait.

Presentation is loading. Please wait.

2016.

Similar presentations


Presentation on theme: "2016."— Presentation transcript:

1 2016

2 Memory management v2 calloc()

3 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) * ); printf("ptr1 adr = %p\nptr2 adr = %p", ptr1, ptr2); free(ptr1); free(ptr2); return 0; } 2016

4 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

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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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


Download ppt "2016."

Similar presentations


Ads by Google