Docsity

Dynamic Data Structures: Structs, Linked Lists, and Dynamic Allocation in C, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Dynamic Data Structures: Structs, Linked Lists, and Dynamic Allocation in C and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Chapter 19Chapter 19

Dynamic Data Structures

2 Wright State University, College of Engineering CEG 320/

Data StructuresData Structures

 A data structure is a particular organization of data in memory.

  • We want to group related items together.
  • We want to organize these data bundles in a way that is convenient to program and efficient to execute.

 An array is one kind of data structure.

 In this chapter, we look at two more:

  • struct – directly supported by C
  • linked list – built from struct and dynamic allocation

4 Wright State University, College of Engineering CEG 320/

Declaring and Using a StructDeclaring and Using a Struct

 To allocate memory for a struct, we declare a variable using our new data

type.

struct flightType plane;

 Memory is allocated, and we can access

individual members of this variable:

plane.airSpeed = 800.0;

plane.altitude = 10000;

 A struct’s members are laid out

in the order specified by the definition.

plane.flightNum[0]

plane.flightNum[6]

plane.altitude

plane.longitude

plane.latitude

plane.heading

plane.airspeed

5 Wright State University, College of Engineering CEG 320/

Defining, Declaring, and typdefsDefining, Declaring, and typdefs

 You can both define and declare a struct at the same time.

struct flightType { char flightNum[7]; /* max 6 characters / int altitude; / in meters / int longitude; / in tenths of degrees / int latitude; / in tenths of degrees / int heading; / in tenths of degrees / double airSpeed; / in km/hr */ } toChicago;

 And you can use the flightType name to declare other structs.

struct flightType fromChicago;

 C provides a way to define a data type by giving a new name to a

predefined type.

typedef ;

7 Wright State University, College of Engineering CEG 320/

Array of StructsArray of Structs

 Can declare an array of structs:

 Flight planes[100];

 Each array element is a struct (7 words, in this case).

 To access member of a particular element:

 planes[34].altitude = 10000;

 Because the [] and. operators are at the same precedence, and both associate left-to-right, this is the same as:

 (planes[34]).altitude = 10000;

8 Wright State University, College of Engineering CEG 320/

Pointer to StructPointer to Struct

 We can declare and create a pointer to a struct:

Flight *planePtr;

planePtr = &planes[34];

 To access a member of the struct addressed by dayPtr:

(*planePtr).altitude = 10000;

 Because the. operator has higher precedence than *, this is NOT the

same as:

*planePtr.altitude = 10000;

 C provides special syntax for accessing a struct member

through a pointer:

planePtr->altitude = 10000;

10 Wright State University, College of Engineering CEG 320/

Dynamic AllocationDynamic Allocation

 Suppose we want our weather program to handle a variable number of

planes – as many as the user wants to enter.

  • We can’t allocate an array, because we don’t know the maximum number of planes that might be required.
  • Even if we do know the maximum number, it might be wasteful to allocate that much memory because most of the time only a few planes’ worth of data is needed.

 Solution:

Allocate storage for data dynamically, as needed.

11 Wright State University, College of Engineering CEG 320/

malloc()malloc()

 The Standard C Library provides a function for allocating memory at

run-time: malloc.

void *malloc(int numBytes);

 It returns a generic pointer (void*) to a contiguous region of memory

of the requested size (in bytes).

 The bytes are allocated from a region in memory called the heap.

  • The run-time system keeps track of chunks of memory from the heap that have been allocated.

 To use malloc, we need to know how many bytes to allocate. The

sizeof operator asks the compiler to calculate the size of a particular

type.

planes = malloc(n * sizeof(Flight));

 We also need to change the type of the return value to the proper kind of

pointer – this is called ā€œcasting.ā€

planes = Flight) malloc(n sizeof(Flight));

13 Wright State University, College of Engineering CEG 320/

free()free()

 Once the data is no longer needed, it should be released back into the

heap for later use.

 This is done using the free function, passing it the same address that was

returned by malloc.

void free(void*);

 If allocated data is not freed, the program might run out of heap memory

and be unable to continue.

  • Memory leaks…

14 Wright State University, College of Engineering CEG 320/

Dynamic Allocation Vs StaticDynamic Allocation Vs Static

 Ex: Linked list: A collection of pointer-connected nodes

with a clearly defined head and tail.

 A linked list can only be accessed sequentially.

  • To find the 5 th element, for instance, you must start from the head and follow the links through four other nodes.

 Advantages of linked list:

  • Dynamic size
  • Easy to add additional nodes as needed
  • Easy to add or remove nodes from the middle of the list (just add or redirect links)

 Advantage of array:

  • Can easily and quickly access arbitrary elements

Node 0 Node 1 Node 2

NULL