#stack allocated variable works even out of scope...
102 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
It's undefined behavior, anything can happen.
yea
but even with the debugger, the pointer does not get deleted
it's still there
and it prints 6 every single time
it's undefined behaviour the compiler is allowed to do anything
generally, the compiler is lazy and does the least amount of anything it can
so doing nothing in this case
is leaving the variable on the stack
as it was
what do you think "deleted" means for primitive integral type like int
popped out of the stack...?
sure, but, the trick to most stacks is that the memory is preallocated
and nothing is actually popped
all that happens is the "where" on the stack the end of it is, is moved
the memory is still valid
(from a machine/OS level viewpoint anyway)
yes
and no
I mean they are both just chunks of allocated virtual memory
ok
but yes, the way they behave is very different
like heap is arbitrary... freed or not freed
but stack doesnt have to free or to allocate i guess
I mean neither does the heap
it just.. stacks on top?
this is all implementation details
it's either undefined behavior in C++
or defined behaviour in the C++ standard
how that's achieved will differ from machine to machine
and sometimes that will have consequences
aight
@turbid stratus run with ASan / debug with UBSan to get notified of such issues.
bro, its ok
i'll just watch a video on stack and heap
i guess thats the only way
That's not really going to help you here.
bro what can help me understanding stack and heap, if not understanding stack and heap
here's a decent resource with pictures
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
ok
thank you.
ahh ok talks about pcb
thats what i needed
i'll get studying 👍
@turbid stratus Has your question been resolved? If so, type !solved :)
@sturdy prism
sorry sir
i'm reading and understanding this well
i like this article
just one thing
when was bothering me was that it does not create a segfault reading from deallocated stack memory
is that true?
a segfault happens when you try to read a bad virtual memory segment
it's not guaranteed to happen when you read a bad heap value either
like these are implementation details
they will depend on:
- your specific machine
- your specific processor
- your specific operating system
- your specific compiler
- your specific build options
- other things
in general on many desktop computers often (but not always) the stack is allocated once (per thread) and not deallocated
so is it true? it depends
how can we program if we don't even know what we're programming
lol
it's all a maybe
maybe it will work, maybe it will segfault, but don't try to improvise! you can't know if it is 100% like this.
well
its ok
no, it's not
you were told from the very beginning
this is undefiend behaviour
which means DO NOT DO THIS
in big flashing words
if you do only defined behaviour
the compiler and standard very clear on exactly what they do
aight
so basically
if we do things right is all good
but if we do things wrong the computer cannot tell us 100% if they are wrong
we just have to know it.. right?
I mean there are a huge variety of tools we have for detecting, preventing and mitigating errors
for example, the compiler probably told you this code would cause a problem
but at the end of the day, yes, the computer will do exactly what we told it
even if that thing is something wrong or stupid or involves a massively complicated mess of rules no single human can fully grasp
also, if you ever get higher up in math or computer science you will learn that we have some interesting proofs about this
that neither math nor computer programs can ever be 100% proven
you wanna make me cry?
Not everything that is true can be proven. This discovery transformed infinity, changed the course of a world war and led to the modern computer. This video is sponsored by Brilliant. The first 200 people to sign up via https://brilliant.org/veritasium get 20% off a yearly subscription.
Special thanks to Prof. Asaf Karagila for consultation on...
relatively decent pop-sci video on it
if you declare
char FillTheStackFramelmfaooooooooo [] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
then, the raw piece of memory where numb_1 is pointing to will likely be a the piece of memory where one of the chars from the array I wrote is stored
i tried it
cause, every local variable takes up memory from the stack frame