[ 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



[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 ]