memory and files dr. andrew wallace phd beng(hons) euring [email protected]

37
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

Upload: dorothy-mason

Post on 15-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

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

Memory and FilesDr. Andrew Wallace PhD BEng(hons) EurIng

[email protected]

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

Overview

• Memory

• Dynamic memory

• Caching algorithms

• Memory problems

• Other memory functions

• File IO

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

Memory

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

Memory

• Hal

• Stdin

• Stdout

• Stderr • File• Device

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

Memory

• Von Neumann architecture

• Linier

• Program code and data

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

MemoryAd

dres

s

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

Memory

• Hex address• 0x<hex number>• 0x3F8 – 0x3FF serial I/O• 0x300 – 0x31F Ethernet• 0x220 – 0x233 sound card -0xAFD45C

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

Memory

On board CPU

Cache RAM/ROM

Disk/tape

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

Memory

• Direct Memory Access• Paging / caching

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

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

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

Memory

• Data Alignment• Word size access

• Structure size• Padding

• Alignment• n-bytes

• Speed vs memory usage

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

Dynamic Memory

• Dynamic memory functions in stdlib.h

• malloc

• realloc

• calloc

• free

• Return type void or void*

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

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)

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

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

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

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

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

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

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

Caching algorithms

fast

CPU Cache

Memory

slow

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

Caching algorithms

• Maximise fast memory access

• Minimise slow memory access

• Pages

• Speed

• Size

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

Caching algorithms

• Least recently used

• Most recently used

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

Caching algorithms

teff = tcache + (1 – h)tmain

(1 – h) = probability of a miss (miss ratio)

Small decrease in h results in large increase of teff

Page 21: Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se
Page 22: Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

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

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)

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

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)

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

Other memory functions

• Big endian / little endian• Big – most significant byte first• Little – least significate byte first

• 0x23AFDE3B

10000100011000210003

23AFDE3B

3BDEAF23

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

File IO

• C standard• Buffered file system• Formatted

• UNIX standard• Unbuffered• Unformatted

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

File IO

• Streams and files

• Constant interface regardless of device - stream• Abstraction

• Device being used• File

Data in Data in

Data outData out

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

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)

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

File IO

• Control / management functions

• Input functions

• Output functions

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

File IO

• stdio.h

• file* fp• Structure defining the file

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

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)

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

File IO

• fflush()

• remove()

• rewind()

• fseek()

• feof()

• ferror()

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

File IO

• int fflush(FILE* fp)

• Flushes an opened file

• If fp = null, all open files are flushed

• 0 if successful else EOF (-1)

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

File IO

• int remove(char* filename)

• Deletes a file given by filename

• 0 if successful else returns a non zero integer

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

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

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

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

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

Questions?