Bifalgorithm post · Computer science one · Arrays of Structures

How can you combine contiguous structure storage with indirect access?

How can you combine contiguous structure storage with indirect access? (Computer science one · Arrays of Structures) — "It is also possible to take a “hybrid”…

Ideas in this flowchart

  • A hybrid approach stores structures contiguously while allowing indirect access through a pointer array.
  • The structures are stored together in contiguous memory.
  • A pointer array provides indirect references to the stored structures.
  • This approach is analogous to the contiguous two-dimensional integer array created earlier.
  • The two-dimensional array used one call to malloc.
  • Pointers were then set up for each row.
  • The same storage strategy can be applied to structures.
  • The process begins by declaring and allocating a dynamic array as before.
  • The array size is set with n = 10.
  • rosterData allocates space for 10 Student structures using malloc.
Feed/

How can you combine contiguous structure storage with indirect access?

No cover

Channel

Computer science one

1 page

"It is also possible to take a “hybrid” approach to storing structures in arrays that stores them both contiguously, but also allows you to reference them indirectly through a pointer array. This is essentially what we did in Section 20.4.1 when we created a contiguous 2-dimensional array of integers. We made a single call to malloc and then had to setup the pointers to each “row.” To do this with structures, we can do something similar. First, we declare and allocate a dynamic array as before.\n\nint n = 10;\n\nStudent *rosterData = (Student *) malloc(n * sizeof(Student));"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.