Upload
dorothy-mason
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
Memory and FilesDr. Andrew Wallace PhD BEng(hons) EurIng
Overview
• Memory
• Dynamic memory
• Caching algorithms
• Memory problems
• Other memory functions
• File IO
Memory
Memory
• Hal
• Stdin
• Stdout
• Stderr • File• Device
Memory
• Von Neumann architecture
• Linier
• Program code and data
MemoryAd
dres
s
Memory
• Hex address• 0x<hex number>• 0x3F8 – 0x3FF serial I/O• 0x300 – 0x31F Ethernet• 0x220 – 0x233 sound card -0xAFD45C
Memory
On board CPU
Cache RAM/ROM
Disk/tape
Memory
• Direct Memory Access• Paging / caching
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
Memory
• Data Alignment• Word size access
• Structure size• Padding
• Alignment• n-bytes
• Speed vs memory usage
Dynamic Memory
• Dynamic memory functions in stdlib.h
• malloc
• realloc
• calloc
• free
• Return type void or void*
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)
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
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
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
Caching algorithms
fast
CPU Cache
Memory
slow
Caching algorithms
• Maximise fast memory access
• Minimise slow memory access
• Pages
• Speed
• Size
Caching algorithms
• Least recently used
• Most recently used
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
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
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)
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)
Other memory functions
• Big endian / little endian• Big – most significant byte first• Little – least significate byte first
• 0x23AFDE3B
10000100011000210003
23AFDE3B
3BDEAF23
File IO
• C standard• Buffered file system• Formatted
• UNIX standard• Unbuffered• Unformatted
File IO
• Streams and files
• Constant interface regardless of device - stream• Abstraction
• Device being used• File
Data in Data in
Data outData out
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)
File IO
• Control / management functions
• Input functions
• Output functions
File IO
• stdio.h
• file* fp• Structure defining the file
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)
File IO
• fflush()
• remove()
• rewind()
• fseek()
• feof()
• ferror()
File IO
• int fflush(FILE* fp)
• Flushes an opened file
• If fp = null, all open files are flushed
• 0 if successful else EOF (-1)
File IO
• int remove(char* filename)
• Deletes a file given by filename
• 0 if successful else returns a non zero integer
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
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
Questions?