lists - Example: Recursive Data Representations We can...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Recursive Data Representations We can define structures with pointer fields that refer to the structure type containing them. struct list { int data; struct list *next; } The pointer variable next is called a link. Each structure is linked to a succeeding structure by way of the field next . The pointer variable next contains an address of either the location in memory of the successor struct list element or the special value NULL . data next 2 Example: struct list a, b, c; a.data = 1; b.data = 2; c.data = 3; a.next = b.next = c.next = NULL; a.next = &b; b.next = &c; a.next->data has value 2 a.next->next->data has value 3 b.next->next->data error !! b a c NULL NULL NULL 3 2 1 b a c NULL 3 2 1 data data data next next next
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3 Static and Dynamic Variables Static Variables: - They are created during compilation. (Fixed memory is reserved for them.) - They cannot be allocated/deallocated during the execution of the program. - Names are associated with them. int x; char y[10]; int z[100]; Dynamic Variables: - They are created (allocated) and deallocated during the execution of the program. - no names are associated with them. The only way to access them is to use pointers. - They don’t exist during compilation. Once they are created they contain data and must have a type like any other variable. Thus we can talk about creating a new dynamic variable of type x and setting a pointer to point to it, or returning a dynamic variable of type x to the system (deallocation). 4 A Conceptual View of Memory PROGRAM MEMORY RAM main called and standard functions global program heap system stack DATA MEMORY
Background image of page 2
5 Dynamic Data For example : We must maintain a list of data At some moments, the list is small, so we want to use only a little memory At other moments, the list is larger, so we need to use more memory Declaring variables in the standard way won’t work here because we don’t know how many variables to declare We need a way to allocate and deallocate data dynamically (i.e., on the fly) 6 Dynamic Memory Allocation Creating and maintaining dynamic data structures requires dynamic memory allocation – the ability for a program to obtain more memory space at execution time to hold new values, and to release space no
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

lists - Example: Recursive Data Representations We can...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online