Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng

Similar presentations


Presentation on theme: "Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng"— Presentation transcript:

1 Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

2 Overview Memory Dynamic memory Caching algorithms Memory problems Other memory functions File IO

3 Memory

4 Hal Stdin Stdout Stderr File Device

5 Memory Von Neumann architecture Linier Program code and data

6 Memory Address

7 Memory Hex address 0x 0x3F8 – 0x3FF serial I/O 0x300 – 0x31F Ethernet 0x220 – 0x233 sound card -0xAFD45C

8 Memory On board CPU Cache RAM/ ROM Disk/ tape

9 Memory Direct Memory Access Paging / caching

10 Memory Variable storage class Automatic Declared at start of a block Memory allocated as needed Stack Register Like automatic but stored in CPU registers (if possible) Faster code External Global variables allocated for the life time of the program Not a good idea! Heap Static Limited scope, Allocated for life time of the program Heap

11 Memory Data Alignment Word size access Structure size Padding Alignment n-bytes Speed vs memory usage

12 Dynamic Memory Dynamic memory functions in stdlib.h malloc realloc calloc free Return type void or void*

13 Dynamic Memory void* malloc(size_t size) size_t - unsigned integer (min 16 bits) size - number of bytes void* - the address of the first byte of the allocated memory If null then memory allocation failed Always check for null!!!! (assert)

14 Dynamic Memory void* realloc(void *ptr, size_t size) Resizes an already allocated block Returns pointer to block Use the retuned pointed as the block may have moved in memory Again, check for null pointers ALWAYS

15 Dynamic Memory void* calloc(size_t num, size_t size) Like malloc but allocates for an array size- size of array element num – number of elements Check for null pointer

16 Dynamic Memory void free(void* ptr) Returns no longer used memory back to the heap ptr – pointer to a block of memory previously allocated by a dynamic memory function

17 Caching algorithms fast CPUCache Memory slow

18 Caching algorithms Maximise fast memory access Minimise slow memory access Pages Speed Size

19 Caching algorithms Least recently used Most recently used

20 Caching algorithms t eff = t cache + (1 – h)t main (1 – h) = probability of a miss (miss ratio) Small decrease in h results in large increase of t eff

21

22 Memory problems Memory leaks Failing to free memory Keep track of allocations Over stepping the bounds Going beyond the limits of an allocated block Running out of memory Check for null pointers

23 Other memory functions malloc.h 8086 memory structure Tiny (max 64k) Small (max 128k) Medium (code >64k) Compact (data > 64k) Large (code + data > 64k) Huge (arrays > 64k)

24 Other memory functions void* alloca(size_t size) void* _malloca(size_t size) Allocates from the stack (don’t use free!) void* far _fmalloc(size_t size)

25 Other memory functions Big endian / little endian Big – most significant byte first Little – least significate byte first 0x23AFDE3B 10000 10001 10002 10003 23 AF DE 3B DE AF 23

26 File IO C standard Buffered file system Formatted UNIX standard Unbuffered Unformatted

27 File IO Streams and files Constant interface regardless of device - stream Abstraction Device being used File Data in Data out

28 File IO Types of streams Text Binary Text stream Sequence of characters Lines of text (new line character) Binary streams Sequence of bytes One-to-one correspondence (may have null bytes on the end)

29 File IO Control / management functions Input functions Output functions

30 File IO stdio.h file* fp Structure defining the file

31 File IO Control / management functions FILE* fp = fopen(const char* filename, const char* mode) Filename can include path specification Mode r – open for read w - create for write a - append r+, w+, a+ - read/write b – for binary files int fclose(FILE* fp)

32 File IO fflush() remove() rewind() fseek() feof() ferror()

33 File IO int fflush(FILE* fp) Flushes an opened file If fp = null, all open files are flushed 0 if successful else EOF (-1)

34 File IO int remove(char* filename) Deletes a file given by filename 0 if successful else returns a non zero integer

35 File IO void rewind(FILE* fp) Sets file position indicator to the start of the file int fseek(FILE* fp, long number_of_bytes, int origin) Random IO Origin SEEK_SET – beginning of the file SEEK_CUR – current position SEEK_END – end of file number_of_bytes – from origin

36 File IO int feof(FILE* fp) Binary date – false end of file True if at end of file, false otherwise int ferror(FILE* fp) True if an error has occurred otherwise false Check after each file operation

37 Questions?


Download ppt "Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng"

Similar presentations


Ads by Google