56474862da7b417cd7c7fc463de04751 by 2024
Author:2024
Language: eng
Format: epub
Published: 2024-06-09T13:23:35+00:00
Dive 11
The Array and Pointer Arithmetic
Real programmers count from zero.
Anonymous programmer
The array is the first data structure every programmer meets. It is essential and basic and is the prototype for every other data structure that attempts to usurp it. The array is basic because itâs just a section of memory, almost without any additional structure.
The Storage Mapping Function
Most C programmers know about arrays, but it is worth recalling what lies behind. An array is a set of identical data types accessed by an index. In C
the only type of index allowed is an integer and the storage is contiguous â
i.e. with no gaps. This is because of the way that an array is implemented using a storage mapping function, SMF.
Suppose you have an array of five integers and each is four bytes. Assume that the ints of the array are laid out in a contiguous block of memory: If each int takes four bytes, then the first int is stored at base, the address of the start of the array. The second is stored at base+4, the third is stored at base+8 or base+2*4. You can see that in general the ith element is stored at: address = base + (i-1)*4
You can get a slight simplification by starting to count the elements from 0: address = base + i*4
Now element 0 is at base, element 1 at base+4 and so on.
And, yes, this is the reason that programmers count from zero not one.
119
Going beyond the simple int array, you can see that, in general, if the repeated element in the array takes B bytes to store then the SMF for the array is:
address = base + i*B
The same idea works for multi-dimensional arrays.
For example, a two-dimensional array with n rows and m columns can be implemented using the SMF:
address = base + i *m *B +j*B
as m*B is the size of a single row â this is often called the âstrideâ because itâs the amount you have to jump to move to the same element in the next row.
Thatâs it, thatâs all there is to arrays. Things really only get more complicated when you need an array that is bigger than the available memory or when there is some special requirement to be able to reorganize the array efficiently. Large arrays are generally broken down into pages, each of which will fit into memory. In this case the SMF is factored into an expression that gives the page number and the offset within the page. Arrays that need to be reorganized are generally implemented as linked lists, another major use case for pointers.
In C, an array is a contiguous block of storage that fits into memory i.e. does not need to use secondary storage.
Index Notation
All you need to implement an array is a block of memory and a pointer to the start. You can then use a storage mapping function to access any particular element. This is easy, but most computer languages, C included, provide some syntactic sugar to make working with arrays possible, even before you meet pointers.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26268)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(18797)
Shot Through the Heart by Mercy Celeste(18682)
The Fifty Shades Trilogy & Grey by E L James(18566)
The Subtle Art of Not Giving a F*ck by Mark Manson(13888)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(13837)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(12754)
Scorched Earth by Nick Kyme(12502)
Drei Generationen auf dem Jakobsweg by Stein Pia(10735)
Suna by Ziefle Pia(10666)
Scythe by Neal Shusterman(10010)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9277)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9092)
This is Going to Hurt by Adam Kay(8716)
Dirty Filthy Fix: A Fixed Trilogy Novella by Laurelin Paige(7330)
How to Make Love to a Negro Without Getting Tired by Dany LaFerrière(6733)
He Loves Me...KNOT by RC Boldt(6621)
Unleashing the Power of UX Analytics: Proven techniques and strategies for uncovering user insights [Team-IRA] [True PDF] by Jeff Hendrickson(6271)
Interdimensional Brothel by F4U(6081)
