samedi 1 juin 2019

Using the number of statements in a fucntion as a constant for memory allocation

I have a function which executes a bunch of tests. Whenever a new test is created, the function gets one or two more lines. And - the result is pushed back into an array So it goes something like this (simplified):

void foo(int *results) {
    auto index { 0 };

    results[i++] = test_1(some, args, here);
    results[i++] = test_1(some, other_args, here);

    results[i++] = test_2(some, args, here);

    results[i++] = test_3(some, args, here);

    // etc. etc.
}

void bar() {
    auto results = new int/* magic */];
    foo(results);
}

I want to use the number of statements in this function to allocate space for the results (the line in bar()). I cannot use a dynamically-reallocated structure like an std::vector or a list etc. - since I am precluded from allocating any memory due to hardware restrictions.

Now, I could just manually count the lines - and this would work. But then whenever I add another test I would have to remember to update the magical constant.

Is there some way to do the counting with the result usable for the "magic" expression?

Note: Since I'm a scrupulous man with no dignity, I am willing to stoop to the use of macros.

Aucun commentaire:

Enregistrer un commentaire