#stack allocated variable works even out of scope...

102 messages · Page 1 of 1 (latest)

turbid stratus
#

this code works. it prints 6.
wtf?
shouldn't int num = 6; be deleted when Entity::Create() stops?
yet it works...

finite roostBOT
#

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.

brazen lion
#

It's undefined behavior, anything can happen.

turbid stratus
#

yea

#

but even with the debugger, the pointer does not get deleted

#

it's still there

#

and it prints 6 every single time

sturdy prism
#

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

turbid stratus
#

ok...

#

so it gets deleted?

sturdy prism
#

what do you think "deleted" means for primitive integral type like int

turbid stratus
#

popped out of the stack...?

sturdy prism
#

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)

turbid stratus
#

ok... so

#

stack is VERY different from heap...

sturdy prism
#

yes

turbid stratus
#

if i understand

#

ok

sturdy prism
#

and no

turbid stratus
#

AHHHHH

#

😦

sturdy prism
#

I mean they are both just chunks of allocated virtual memory

turbid stratus
#

ok

sturdy prism
#

but yes, the way they behave is very different

turbid stratus
#

like heap is arbitrary... freed or not freed

#

but stack doesnt have to free or to allocate i guess

sturdy prism
#

I mean neither does the heap

turbid stratus
#

it just.. stacks on top?

sturdy prism
#

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

turbid stratus
#

aight

brazen lion
#

@turbid stratus run with ASan / debug with UBSan to get notified of such issues.

turbid stratus
#

bro, its ok

#

i'll just watch a video on stack and heap

#

i guess thats the only way

brazen lion
#

That's not really going to help you here.

turbid stratus
#

bro what can help me understanding stack and heap, if not understanding stack and heap

turbid stratus
#

ok

#

thank you.

#

ahh ok talks about pcb

#

thats what i needed

#

i'll get studying 👍

finite roostBOT
#

@turbid stratus Has your question been resolved? If so, type !solved :)

turbid stratus
#

@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?

sturdy prism
#

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

sturdy prism
turbid stratus
#

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

sturdy prism
#

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

turbid stratus
#

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?

sturdy prism
#

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

turbid stratus
#

aight

#

thank you for the article, very well explained

sturdy prism
#

that neither math nor computer programs can ever be 100% proven

turbid stratus
#

you wanna make me cry?

sturdy prism
#

relatively decent pop-sci video on it

slim falcon
# turbid stratus thank you for the article, very well explained

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

turbid stratus
#

i tried it

slim falcon
#

cause, every local variable takes up memory from the stack frame

turbid stratus
#

now it does print a random number

#

i get it