Upcoming SlideShare
×

3,268 views

Published on

Published in: Education, Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
3,268
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
167
0
Likes
2
Embeds 0
No embeds

No notes for slide

8. 8. 8 code which goes into an infinite loop, often the problem is that step (3) has been forgotten.Calling Length()Heres some typical code which calls Length(). It first calls BuildOneTwoThree() to makea list and store the head pointer in a local variable. It then calls Length() on the list andcatches the int result in a local variable.void LengthTest() { struct node* myList = BuildOneTwoThree(); int len = Length(myList); // results in len == 3}Memory DrawingsThe best way to design and think about linked list code is to use a drawing to see how thepointer operations are setting up memory. There are drawings below of the state ofmemory before and during the call to Length() — take this opportunity to practicelooking at memory drawings and using them to think about pointer intensive code. Youwill be able to understand many of the later, more complex functions only by makingmemory drawings like this on your own.Start with the Length() and LengthTest() code and a blank sheet of paper. Trace throughthe execution of the code and update your drawing to show the state of memory at eachstep. Memory drawings should distinguish heap memory from local stack memory.Reminder: malloc() allocates memory in the heap which is only be deallocated bydeliberate calls to free(). In contrast, local stack variables for each function areautomatically allocated when the function starts and deallocated when it exits. Ourmemory drawings show the caller local stack variables above the callee, but anyconvention is fine so long as you realize that the caller and callee are separate. (Seecslibrary.stanford.edu/102/, Pointers and Memory, for an explanation of how localmemory works.)
9. 9. 9Drawing 1 : Before Length()Below is the state of memory just before the call to Length() in LengthTest() above.BuildOneTwoThree() has built the {1, 2, 3} list in the heap and returned the head pointer.The head pointer has been caught by the caller and stored in its local variable myList.The local variable len has a random value — it will only be given the value 3 when thencall to Length() returns. Stack Heap LengthTest() myList len -14231 1 2 3The headpointer forthe list is len has astored in the randomlocal variable value until Nodes allocated in the heapmyList. it is via calls to malloc() in assigned. BuildOneTwoThree().
10. 10. 10Drawing 2: Mid LengthHere is the state of memory midway through the execution of Length(). Length()s localvariables head and current have been automatically allocated. The current pointerstarted out pointing to the first node, and then the first iteration of the while loopadvanced it to point to the second node.Stack HeapLengthTest() myList len -14231 1 2 3Length() head currentNotice how the local variables in Length() (head and current) are separate from thelocal variables in LengthTest() (myList and len). The local variables head andcurrent will be deallocated (deleted) automatically when Length() exits. This is fine— the heap allocated links will remain even though stack allocated pointers which werepointing to them have been deleted.ExerciseQ: What if we said head = NULL; at the end of Length() — would that mess up themyList variable in the caller? A: No. head is a local which was initialized with a copyof the actual parameter, but changes do not automatically trace back to the actualparameter. Changes to the local variables in one function do not affect the locals ofanother function.ExerciseQ: What if the passed in list contains no elements, does Length() handle that caseproperly? A: Yes. The representation of the empty list is a NULL head pointer. TraceLength() on that case to see how it handles it.