[ home / overboard ] [ spam ] [ soy / qa / raid / r ] [ int / pol ] [ a / an / asp / biz / mtv / r9k / tech / v / sude / x ] [ q / news / chive / rules / pass / bans / status ] [ wiki / booru / irc ]

A banner for soyjak.party

/tech/ - Soyence and Technology

Download more RAM for your Mac here
Catalog
Email
Subject
Comment
File
Password (For file deletion.)

File: 1778956638702v.png 📥︎ (97.45 KB, 888x970) ImgOps

 â„–32599[Quote]

I feel like the stack is so mystified because the professors teaching it don't understand it or anything low level themselves. I say this as I am just beginning to learn C
<tech space
When a function is called, a section of memory is "reserved" on top of what is called the stack. This section of memory is the function's stackframe. The stackframe contains all of the variables allocated by the function. The stack is made up of stack frames.
<tech space
After you understand this, the rest is easy. If you want to remove a variable from a stackframe, it has to be both a variable from the frame on the top of the stack and the variable on top of that stackframe.
<tech space
Pushing something to the stack is just adding something to the top, and popping is just removing the top item. You can't push or pop any random item. The top of the stack is just a pointer in a CPU register. Popping simply decrements the top stack pointer and pushing simply increments it and stores a value at said address. The list of elements in them are entirely defined by those registers. There is no such thing as removing a value within these bounds. You can write it to be zero, but you can't just remove it.
<tech space
I think the plate analogy is almost a misnomer because it's easy to visualize but it's easy to forget this fact. You can't remove something from the middle and have the stack collapse on itself.
<tech space
You can have multiple stackframes on top of one another if functions contain calls to other functions. Each call to a function without a return value adds another to the stack. You can need to push and pop single items on the top of the stack if a function dynamically allocates or deallocates variables.
<tech space
Please chime in if I said something retarded. I will also do my best to answer questions

 â„–32600[Quote]

The reason I said "reserved" in quotes was that there is no malloc going on. It's just that the stack pointer is increased and any values are stored in those bounds

 â„–32603[Quote]

just write some assembly and then computers make sense

 â„–32605[Quote]

File: ClipboardImage.png 📥︎ (14.97 KB, 810x304) ImgOps

File: ClipboardImage.png 📥︎ (4.07 KB, 473x93) ImgOps

i understood all of that before.

and if i am right in functions local variables are stored on the stack as well so that they can be modified via popping them to registers or moving something to %rbp + offset (which is a multiple of 4 or 8 depending on the system)

also
the reason why you don't return a pointer from function is because after it's stack frame is done and %rbp and %rsp are reset the address stored in the pointer will now point to the undefined region in memory (evendoe the data in it may still persist for a brief moment) which will make the os throw a segfault. very interesting stuff really about the scopes and variable lifetimes, be careful when returning a pointer.

 â„–32608[Quote]

>>32599 (OP)
It is wild that most programmers fundamentally don't understand the machine they are working with. If you don't understand the stack, heap, fragmentation, pointers, and the primitive data structures constructed of only the previously mentioned things (especially trees, lists, and arenas), you are NGMI.

It is as if we went from having mathematicians to replacing them with people who can use calculators, and calling themselves mathematicians.

 â„–32636[Quote]

bump, let's talk about the heap

 â„–32777[Quote]

So garbage collection is just going back through the stack and reassembling based on what is still in context? So what happens with manual memory allocation and deallocation? I do infra work and Elixir I never got into C but intend to once I start on my Z80 build

 â„–32778[Quote]

>>32777
I think garbage collection mostly has to do with the heap

 â„–32791[Quote]

>>32777
when function call ends, stack frame "clears" automatically, we go back to previous %rbp. The data is still there, but it will be overwritten by other stack frames.
Garbage collection refers to automatically deallocating allocated memory on the heap. In C, there is no garbage collection, instead you have to call malloc() and then free() to free the memory. If you don't call free(), there may be a situation where your program repeatedly calls malloc(), causing it to allocate memory more and more, never disposing of it, swiftly devouring your RAM. This is called a memory leak. In languages like Java the dynamically allocated memory will be disposed automatically by a garbage collector. In V language there is memory deallocation at compilation time, or something like that, I think it automatically injects free() where is needed, o algo.



[Return][Catalog][Go to top][Post a Reply]
Delete Post [ ]
[ home / overboard ] [ spam ] [ soy / qa / raid / r ] [ int / pol ] [ a / an / asp / biz / mtv / r9k / tech / v / sude / x ] [ q / news / chive / rules / pass / bans / status ] [ wiki / booru / irc ]