When vectors are allocated, do they use memory on the heap or the stack?

vector<Type> vect; will allocate the vector, i.e. the header info, on the stack, but the elements on the free store (“heap”). vector<Type> *vect = new vector<Type>; allocates everything on the free store. vector<Type*> vect; will allocate the vector on the stack and a bunch of pointers on the free store, but where these point is … Read more

How are multi-dimensional arrays formatted in memory?

A static two-dimensional array looks like an array of arrays – it’s just laid out contiguously in memory. Arrays are not the same thing as pointers, but because you can often use them pretty much interchangeably it can get confusing sometimes. The compiler keeps track properly, though, which makes everything line up nicely. You do … Read more

What are the dangers when creating a thread with a stack size of 50x the default?

Upon comparing test code with Sam, I determined that we are both right! However, about different things: Accessing memory (reading and writing) is just as fast wherever it is – stack, global or heap. Allocating it, however, is fastest on stack and slowest on heap. It goes like this: stack < global < heap. (allocation … Read more

Stack vs heap allocation of structs in Go, and how they relate to garbage collection

It’s worth noting that the words “stack” and “heap” do not appear anywhere in the language spec. Your question is worded with “…is declared on the stack,” and “…declared on the heap,” but note that Go declaration syntax says nothing about stack or heap. That technically makes the answer to all of your questions implementation … Read more

tech