Where are the variables in this case (stack, heap)?

In the library GMP (GNU Multiple Precision) is a type definition to work with integers:
typedef struct {
 int _mp_alloc; 
 int _mp_size;
 mp_limb_t *_mp_d;
} __mpz_struct;

typedef __mpz_struct mpz_t[1];

When you declare a variable of type mpz_t var; inside the function a pointer to a structure will be clear on the stack, and the array pointed to by the _mp_d, in the heap. And where will be posted _mp_alloc, _mp_size and the actual value of the pointer _mp_d? This question is linked to another: if the function return (__mpz_struct *)var, then when you exit it (and thus clearing the stack) whether these values are overwritten? Are there any guarantees to this effect in the standard or is it dependent on the implementation of compilers?
June 14th 19 at 20:21
1 answer
June 14th 19 at 20:23
You are absolutely right. This is an array of one element, and with this arrangement, it will be in the stack. With all its fields: alloc, size, and mp_d. When you exit from a function there are chances that it will be overwritten, and so can not act.

Who overwritten? Whatever. Though the driver wished to use your stack. Even a subsequent call to any function.

On the other hand, and marked the structure of the __mpz_struct two podcherknu that it is not used.
__mpz_struct* foo()
 mpz_t var;
 return (__mpz_struct *)var;
// C:\TestApps\RetLocal\main.cpp/15/warning: address of local variable 'var' returned [-Wreturn-local-addr]|

And all in a heap. Just remember to clean up using delete[].
__mpz_struct* bar()
 __mpz_struct* var = new mpz_t;
 return var;
why clean up delete[], if the memory was allocated using new (not new[]) - will.DAmo commented on June 14th 19 at 20:26
Because typedef'ω hiding array. - Humberto_Lynch68 commented on June 14th 19 at 20:29

Find more questions by tags LinuxGCCCProgramming