You seem to be initializing this struct with constants. In such a case,
I would use neither the heap nor the stack, but a third option: the
“read-only data” section of the program. At global scope:
const adc_continuous_config_t cfg = {
.sample_freq_hz = 4500,
.conv_mode = ADC_CONV_SINGLE_UNIT_1,
.format = ADC_DIGI_OUTPUT_FORMAT_TYPE1,
};
Rationale: The constants you are using to fill the struct have to be
stored somewhere in the flash when the program is not running. Although
I do not know the details of the machine language of the ESP32, it seems
to me the compiler has only two options to compile your example code. It
could say:
- load a CPU register with the immediate value
4500
- store that register at the address of
cfg->sample_freq_hz
- load a CPU register with the immediate value
ADC_CONV_SINGLE_UNIT_1
- etc.
Then the values are stored as immediate operands of machine
instructions. The other option would be to store an anonymous version of
the initialized struct in the read-only data section of the program, and
issue a memcpy() to initialize the allocated space at run time.
If you instead store the initialized struct as a global constant, it
will go straight into the read-only data section of the program (like in
the second compiler strategy), and you will use it from there. No extra
copy needed. No extra machine instruction for initializing space
allocated on the heap or stack.
Caveat: I assume adc_continuous_config() expects its second
parameter to be of type “pointer to const data”. If it expects it to
be a pointer to modifiable data, then this will not work.