Today
memory management
Memory basics
ask for memory as we need it let it go when we don't possible to write memory-efficient code (small memory footprint)
Memory basics
The catch(es):
Somewhat complex mechanism for doing so Manual (i.e. programmer-initiated) memory management Coding mistakes leads to ugly, hard-tofind, hard-to-replicate bugs No automatic garbage collection
One scenario
locate the file and format (jpg/tiff/bmp/png...) allocate enough memory to hold the image copy the image from disk to memory process deallocate the memory previously allocated
Graphically...
Main memory
find image
Secondary memory
Graphically...
Main memory find image calculate size
645KBytes
Secondary memory
Graphically...
Main memory find image calculate size allocate memory
645KBytes
dynamically allocate
Secondary memory
Graphically...
Main memory find image calculate size allocate memory
645KBytes
Secondary memory
unlike Java, where the compiler/interpreter takes care of it Programmers must clean up themselves what if not enough free memory?
Pointers
holds the address of a data object or a function/method handler to dynamically allocated memory pass arguments as reference
Pointers
declaration
int *i; char *c; and so on. i.e. the same pointer cannot refer to ints and chars
Pointers
simple usage:
int number = 10; int *i = &number; i contains the address of number *i contains the value stored in number
*i is a pointer to an int
Pointers
stray pointer:
i points to what?
Bad Pointer
int*i; intnum=10; ... *i=100;//Whereisthisgoingto???
This program:
will compile,will run, and maybe will not even crash depending on what i points to, it might bring down your program entirely always initialize with either a valid address or NULL
Allocation
usage:
int *i = new int; dynamically allocated space for one int using the C++ new operator find storage in free store (or heap) returns a pointer to the object created
if it does, then either returns NULL or throws an exception (std::bad_alloc) int *i = new int(10); note the bracket type int *i = new int[10];
new example
char*pCharArray; try{ pCharArray=newchar[256]; //sampleusage strcpy(pCharArray,"Arrayofcharacters"); } catch(std::bad_alloce) { std::cerr<<Allocationfailed.; }
Deallocation
an object created with new exists until operator delete is used to deallocate will not be implicitly called for an object created with a new
Deallocation
use delete() with new() (individual objects) use delete[]() with new[]() (arrays)
if not, the result is undefined deleting a NULL pointer has no effect. delete does not assign NULL to a pointer accessing a deleted pointer has undefined effects
delete example
char*pCharArray=0; try{ pCharArray=newchar[256]; } catch(std::bad_alloce) { std::cerr<<Allocationfailed.; } ... delete[]pCharArray; pCharArray=NULL;
memory holes?
voidSomeFunction(){ char*pCharArray=0; try{ pCharArray=newchar[256]; } catch(std::bad_alloce){ } ... } intmain(){ SomeFunction(); } std::cerr<<Allocationfailed.;
memory F.A.Q.
support for objects and type-safe pointers causes prorgam to crash bad idea.
double delete?
mixing new/malloc?