Using temporary adresses

In my code I have chosen to use temporary variables to declare shape and body definitions, as such I get the warning:
Quote:
warning: taking address of temporary

While this does not concern me, I feel I should make sure, and ask if there is any reason why I shouldn't use temporary variables to define bodies and shapes?
When you have a temporary variable, such as one defined inside a function scope, and return the address of that variable there is no guarantee what you'll get back when calling that function.

Sure, you'll get an address to where that temporary variable was located, depending somewhat on circumstances. If you're lucky, it will still have the same value, but it might also have been overwritten by something else since the variable had gone out of scope. I'm not entirerly sure what the differences are between variables on the stack vs the heap in this case, but I feel both cases are a bit risky.
I am aware of everything you just said... however, my only concern is whether or not these variables are actually used beyond the declaration. If they are not then there would be no reason for me not to use temporary variables.
Maybe you should post a bit of code as an example.
So you have asked, so it is given...
Code to define Boxes.
Code:
b2BoxDef Define_Box(float width,float height,float density=0){
   b2BoxDef boxdef;
   boxdef.extents.Set(width,height);
   boxdef.density=density;
   return boxdef;
}
Code to define Bodies.
Code:
b2BodyDef Define_Body(b2ShapeDef* Shape,float X,float Y,float Rotate=0,bool CantRotate=0,float VelocityX=0,float VelocityY=0,float AngleVelocity=0,float LinearDamping=0,float AngleDamping=0,bool AllowSleep=1,bool Asleep=0){
   b2BodyDef bodydef;
   bodydef.AddShape(Shape);
   bodydef.position.Set(X,Y);
   bodydef.rotation=Rotate*0.0174532925;
   bodydef.linearVelocity.Set(VelocityX,VelocityY);
   bodydef.angularVelocity=AngleVelocity;
   bodydef.linearDamping=LinearDamping;
   bodydef.angularDamping=AngleDamping;
   bodydef.allowSleep=AllowSleep;
   bodydef.isSleeping=Asleep;
   bodydef.preventRotation=CantRotate;
   return bodydef;
}
Previous code in use.
Code:
object[0].body=pworld.CreateBody(&Define_Body(&Define_Box(16.0,16.0,1.0),16.0,10.0,0.0));
If you want to make your code more correct, then modify it to look like this:

Code:
const b2BoxDef &box = Define_Box(16.0,16.0,1.0);
const b2BodyDef &body = Define_Body(&box,16.0,10.0,0.0);
object[0].body=pworld.CreateBody(&body);


I don't really recommend doing it this way, you should instead find a better way to return a non-temporary. However, this will at least stop your compiler from warning you.

Temporary variables have a limited lifespan and your code is incorrect. It may compile, and it may even work right now, but it is not guaranteed to work at any point. There is data on the stack that looks like the data you entered, but at any moment it could be overwritten by something else, and your address pointing there will be left pointing to garbage.

Binding a temporary variable to a const reference (as above) will guarantee that your temporary lives as long as the const reference does (until the end of the enclosing scope).
It took me a moment, but I think I understand what your saying. Even so, I still have not learned anything I didn't know... what I was asking for would basically require someone who has crawled through the code of Box2D to tell me whether or not it was even possible for something to go wrong as a result of using temporary variables... if the variables are in any way used after the process of making the object in the world, then there is a problem, if not than my use of them is perfectly fine.
Your use of them is not perfectly fine. In fact, inside of Define_Body you're using a pointer to a b2BoxDef that is pointing to an invalid object.
❗ Insight has come to mind. Are you trying to tell me that by the time the Body gets to the world, that the Box its referencing no longer exists? Either which way, your statement has made me realize a potential flaw which would require me to do it your way... however, as far as I can tell, after putting the object in the world the variables holding the definitions are no longer needed right? This is what I have been trying to get around... this whole time.

Update:
Exparamentation since changing the code to store the definitions in variables has been errored... as such I have come to the conclusion that my attempts to completely disgard the definitions were in fault... and it seems that the definitions matter even after creating the object.
@mos: Sorry, I've spent so long programming Java that I've forgotten a lot of this stuff. I would think that the following would be safe (same as your suggestion, except binding to plain old local variables instead of const refs):
Code:
b2BoxDef box = Define_Box(16.0,16.0,1.0);
b2BodyDef body = Define_Body(&box,16.0,10.0,0.0);
object[0].body=pworld.CreateBody(&body);

since the box and body variables would still be "alive" until they are used (that is, it would be safe as long as CreateBody doesn't cause pworld to hang on to that pointer and merely copies away whatever it actually needs). Are you saying that in this case, it's possible, for instance, for "body" to be out of scope by the time CreateBody is called?

I thought local variables were block scoped? I think I must be misunderstanding something here...

Original thread: https://box2d.org/forum/viewtopic.php?t=330