I know that gdb allows for an already declared variable to be set using the set command.
Is it possible for gdb to dynamically declare a new variable inside the scope of a given function?
You can dynamically allocate some space and use it to store a new variable. Depending on what you mean by "scope of the current function" it may not be what you want.
But here is how it looks like, when you have function func() that takes a pointer to an output parameter:
set $foo = malloc(sizeof(struct funcOutStruct))
call func($foo)
p *$foo
call (void) free($foo)
func($foo) from gdb did not work (Undefined command: "func".), but calling set $garbage = func($foo) worked. In addition, I had to use malloc instead of alloc.call - it executes an expression and discards the result. And fixed malloc.free is use like in ``` (gdb) call free(0) 'free' has unknown return type; cast the call to its declared return type (gdb) call (void) free(0) ```call func($foo) part? It would be strange for gdb to require an explicit type to be specified there. But, at the same time, it is unclear to me, why it would need it for the free call.For C (and probably C++) code, that would be very hard, since doing so in most implementations would involve shifting the stack pointer, which would make the function's exit code fail due to it no longer matching the size of the stack frame. Also all the code in the function that accesses local variables would suddenly risk hitting the wrong location, which is also bad.
So, I don't think so, no.
set $foo = ...and later reference$foo. Obviously such variables are in no way visible to the running code, however.