Associated each new operation doesnt just allocate

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ptr var = new DataType[size]; new DataType[size Object Programming Object Programming The value of ptr may or may not be set to 0 by ptr may this action. It may still point to the old memory which has now been given up. which Such a pointer is called a dangling pointer. Such dangling Set such a pointer to 0 to ensure that we do not Set continue to use the deleted space. continue For arrays of memory allocated by new we use For new delete DBase; Get used to deleting dynamically allocated memory once their usage is complete. memory Avoid memory leaks. void func() void func { int* ptr; ptr } automatic variable ptr = new int[100]; . . . ptr is destroyed 100 ints leaked 100 int unless we delete ptr unless ptr Object Programming How does delete know the amount of memory delete know associated with a pointer? associated Each new operation doesn't just allocate the Each new operation memory requested. It tacks on a couple of bytes to let delete know how much there is. delete This is the overhead of new. This new Object Programming Be also aware that there are rules about what sort of address certain types must start on. Usually it has to at least be even. Usually For example char *c1, *c2; c1 = new char; c2 = new char; yields addresses 4 bytes apart. Object Programming This new power we've unleashed is more than just a way of dynamically allocating space. just Its strengths lie in the ability to define new data Its structures beyond the traditional scalars and arrays. arrays. Let's look at another example to illustrate this new Let's window of opportunity. window Object Programming Overhead is implementation dependent. Programming Object A pizza restaurant allocates tables on a pizza first-come, first-served basis. Customers enter give their names and party size take a seat and wait The receptionist keeps a list of customers in the The order they arrive. order W hen a table becomes available, the name on When the top of the list is served. Object Programming To computerise this process we create a struct: struct W e then declare one single pointer to the customer at the top of the list customer CustPtr Head; struct Cust { char name[30]; int partysize; Cust *next; }; Initially, the list is empty. So we set Head = 0; typedef Cust* CustPtr; CustPtr next is a pointer to the next customer on the list. So, what sort of things do we want to do with So, such a list? such Object Programming When a customer arrives W hen add the customer to the bottom of the list When a table becomes available get the first name off the list and remove it from the list Object Programming 0 Algorithms: Algorithms: Initialise set Head to 0 Add create a new customer link it to the tail of the list So we have three functions: initialise add remove Head Head Object Programming Cust1 0 Remove if there is a customer on the list get the customer's name and party size make the head point at the next customer delete the memory used by the old head return the name and party size Object Programming Cust1 0 Head Head tmp tmp Initialise Add Add Object Programming...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online