#Lifetimes on the stack

42 messages ยท Page 1 of 1 (latest)

lone saddle
#

Would this piece of code ever not work? I'm curious to know if I could do this safely without any worries of trash overwriting the array's contents, so far it has never failed but I'm wondering if it ever will.

cyan spruceBOT
#

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.

delicate frost
urban hedge
#

Paste code as text.

lone saddle
lone saddle
#
typedef struct
{
    float x;
    float y;
    float z;
} vec3;
delicate frost
#

the code is fine then

lone saddle
#

This is the struct I'm using rn

#

Nice, would it not work if it was a double?

urban hedge
delicate frost
delicate frost
barren thistle
lone saddle
#

Gotcha, as long as it's native types it would work I assume?

delicate frost
#

'native type'?

lone saddle
#

My bad, not sure what the correct name is

urban hedge
lone saddle
#

Basically the types that come out of the box with the language

barren thistle
#

So unless you have another code somewhere that invades another function's stack and overwrites them, your code should be 100% fine

delicate frost
urban hedge
#

OK I took that as picking one out of those two.

lone saddle
delicate frost
#

you're copying the vec3 into the array

lone saddle
#

Aaaah

#

Alright, that's what I needed to hear, makes a lot more sense now

#

So it inserts a copy into the array

#

Didn't know that, thank you

delicate frost
#

yep = with structures will copy the structures (i.e. it copies all of the members)

lone saddle
#

Gotcha, that answers my question, I was worried it would've copied the reference somehow, which would mess up the code once out of scope

#

Not the code, the array I mean

barren thistle
#

The compiler is allowed to optimize away that a by just directly writing data into vecs, but in essence you can think of it as a copy

lone saddle
#

That's nice, thank you all :)

delicate frost
barren thistle
#

There's really no way to tell what the compiler does with your code I think lmao

#

Just don't do UB and the compiler won't ever break your code

delicate frost
#

yeah, you shouldn't really concern yourself with optimizations when thinking about the semantics of the language; i.e. it does a copy but the way it reaches the result of copying (or the entire program in general) is up to the compiler

lone saddle
#

I think compiler optimizations are beyond my skillset right now ๐Ÿ’€๐Ÿ’€๐Ÿ’€ But I'll have to learn them soon enough considering my 3d engine project

#

It's running well but I doubt it will with millions of polygons

#

Either way, thank you so much for the help :)

#

I really appreciate it