I only recently learned that I can actually use references to compound literal arrays in C, which I find useful, but I don't quite understand how it works.
For instance, say that I use the feature to avoid having to declare a variable for a call some socket interface function where I don't care about the return name length, like this:
int sockfamily(int fd)
{
struct sockaddr_storage ss;
getpeername(fd, (struct sockaddr *)&ss, (socklen_t [1]){sizeof(ss)});
return(ss.ss_family);
}
Clearly, sizeof(ss)
needs to actually be stored on the stack in order for a pointer to it to be passed to getpeername
, and space on the stack must therefore be allocated and reserved for that purpose, but what is the lifetime of this allocation? How long can I trust it to remain allocated?
Looking at the assembly output of GCC, I observe that if I put the call to getpeername
in a loop, the allocation does not survive for multiple iterations of the loop, but what other conditions, then, might cause it to cease to exist?