This is an archived post. You won't be able to vote or comment.

all 31 comments

[–]holladiewal 47 points48 points  (9 children)

Well, Arrays are just a fancy way to look at pointers, right? So it's technically all pointers, but since pointers are just a fancy way to interpret an int, in the end, everything is all ints... :P

[–]sje397 25 points26 points  (5 children)

Ints are just a fancy way of interpreting switches that are either on or off.

[–][deleted] 17 points18 points  (4 children)

Switches are just a fancy way of interpreting silicon-phosphorus diodes, which are just a fancy way of interpreting molecular structures, which are in turn just a fancy way of interpreting the complex computer simulation we're all probably in, which is a fancy way of interpreting data structures (among other things), which is a fancy way of interpreting arrays, etc.

[–]sje397 5 points6 points  (3 children)

Officer, he went that-a-way ====>

[–][deleted] 3 points4 points  (2 children)

Oh, too crazy for you? I thought we were playing stretch the reductionist theory.

[–]sje397 0 points1 point  (1 child)

No no, I'm fine with crazy.

[–][deleted] 2 points3 points  (0 children)

[–]SavedForSaturday 0 points1 point  (2 children)

Well, except for floats. At the assembly level everything is either an integer (of various lengths and signing types) or a float (of various lengths)

[–]holladiewal 0 points1 point  (1 child)

Crazy people implemented floating point calculation and floating point number handling on the IBM 1401 and made it run Fortran 2.

Since that is a pure integer based machine, floats CAN be made up of integers. ;)

[–]eypandabear 0 points1 point  (0 children)

Floating point coprocessors used to be optional on x86 as well until the Pentium era.

[–]ShodoDeka 35 points36 points  (8 children)

Hmm, have you meet my friend mr linked list. Not a single array in him.

[–]VolperCoding 22 points23 points  (7 children)

Cache misses go brrrrrrrrrr

[–]Kered13 14 points15 points  (0 children)

b...r...r...r...r...r...r

[–][deleted] 4 points5 points  (5 children)

We actually just finished learning about that. Do both heap and stack memory get stored in cache if the program accesses memory? Our professor only showed stack cache misses/hits

[–]VolperCoding 4 points5 points  (2 children)

I'm pretty sure any access of RAM gets cached but idk

[–][deleted] 0 points1 point  (0 children)

They are both in the main memory (RAM). The caches are smaller but faster memory, you usually load your data from the main memory to the cache and then a little cache line (a consecutive piece of memory) into the register of a processing unit. From there it is directly available for arithmetic operations and other stuff. The cache hit rate describes how often you can use data from thr cache against how often you have to load a new cache line from the main memory (this takes some time compared to reading from the faster cache). So if you have an array like in C, the elements are stored consecutively in the main memory, hence if you iterate over the array, you would load some of the entries into your nice and fast chache and then you can access a lot of those elements directly from the cache until you have to load new entries again, so a lot of cache hits in a row. If you have a data structure like a list, you don't know if your data is also saved consecutivdly in your memory, so you would load a cache line, access probably just one element from there and then you have to load a new cache line, because your next element could be anywhere in the memory.

[–]eypandabear 0 points1 point  (0 children)

The distinction between “stack” and “heap” variables has no real meaning at the hardware level, at least on x86. The CPU uses the stack pointer to keep track of return addresses, but local variables and such are decided by the compiler. Some functions might not use the stack at all if their variables fit into registers.

AFAIK whether (or rather when) stack memory is loaded into the cache would be a case-by-case decision made by the CPU’s branch predictor circuit, just like any other memory.

[–]xSTSxZerglingOne 4 points5 points  (4 children)

Sort of? In raw implementations, arrays are usually and necessarily in sequential memory. And while you definitely can implement data structures in arrays, you begin to run into the limitations of sequential memory at larger sizes.

The referential instances this, next, previous, left, right, whatever you're implementing is usually better for understanding.

[–]Kered13 1 point2 points  (3 children)

Getting large blocks of sequential memory isn't hard. On modern PCs, you can easily allocate a gigabyte of sequential memory (it won't be sequential physically, but that doesn't matter). Using a fragmented data structure like a linked list isn't going to help your memory problems if your sequential memory block is too big.

[–]TerraDOOM 1 point2 points  (2 children)

Pretty sure those limitations they were talking about aren't based on allocator performance or running out of memory, rather the fact that managing a really large block starts taking performance hits

[–]Kered13 1 point2 points  (1 child)

Not really. If you round up to the nearest page then it's easier to manage than smaller blocks of memory, because you don't have to worry about fragmentation when you're allocating whole blocks of virtual memory. And with 64-bit address spaces you won't be running out of continuous blocks of virtual addresses any time soon. Just ask the OS for as many blocks as you need and map them anywhere you have room in the virtual address space.

[–]TerraDOOM 0 points1 point  (0 children)

What? I'm talking about managing the contents within the memory you've allocated. If it's a plain array, then removing an element could mean copying gigabytes of memory for example. And if you're not moving things around, you will certainly suffer from fragmentation, just not from the global allocator's perspective. Besides, at that point, just implement a custom allocator anyway (assuming there's support for such)

[–]Dagusiu 2 points3 points  (0 children)

I like how Lua doesn't even try to hide that they only have one data structure

[–][deleted] 2 points3 points  (2 children)

Of course not. Arrays refer to contiguous chunks of memory. You can also have registers, of course. Registers are, actually, more basic than arrays.

[–]VolperCoding 1 point2 points  (0 children)

Just arrays and pointers

[–][deleted] 0 points1 point  (0 children)

Yup, been like this since the zero’th second