[subexp-daq] NURDLIB - working with arrays of variable length (e. g. for different channel numbers)

Hans Toshihide Törnqvist hans.tornqvist at chalmers.se
Wed Apr 24 13:22:29 CEST 2024


Dear Günter,

On 2024-04-23 23:40, Weber, Guenter Dr. wrote:
> Dear friends,
> 
> I am having trouble to correctly implement the possibility of various 
> numbers of channels in caen_v7nn.c.
> 
> For example in INIT_FAST, I wanted to change
> 
> uint16_t threshold_array[32];
> 
> to
> 
> uint16_t* threshold_array;
> threshold_array = calloc(a_v7nn->number_of_channels, sizeof(uint16_t));
> 
> However, at some points in the code, the length of the array is 
> determined by something like
> 
> sizeof array / sizeof array[0]
> 
> and this fails if we just have the pointer.

This seems to happen in SET_THRESHOLDS, which just passes on the length 
to set_thresholds. This is a weird contraption, one of many reasons I 
refer to this code as festering... Just remove the macro and call the 
function directly:

set_thresholds(a_v7nn, threshold_array, a_v7nn->number_of_channels);

The worst offender to me is that I have tried hard to only have byte 
size interfaces, no array lengths. Mixing the two in a code-base is a 
very common source of bugs...

Hope that helps!

Cheers,
Hans

> I found some explanations and possible solutions here: 
> https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c <https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c>
> 
> However, I assume this is much too complicated and I should just try to 
> implement this differently. Maybe this issue came already up for some 
> other module and was solved long time ago.
> 
> Could you give me a hint?
> 
> Thank you very much!
> 
> Best greetings
> 
> Günter


More information about the subexp-daq mailing list