Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a variadic function with custom sentinel value?

The documentation on gnu's C states that if function has __attribute__((sentinel)) it must have NULL as a last argument to the function.

Is it possible to have any other value as marker for ending the argument list?

like image 224
Eimantas Avatar asked Oct 03 '11 17:10

Eimantas


People also ask

What is a sentinel value in programming?

1 Sometimes the endpoint of input data is not known. ... 2 If the input is from a user, the user could enter a specific value to stop collecting data. ... 3 A sentinel value is simply a value that is used to terminate the while loop or do-while loop and stop getting inputs. 4 A sentinel value is usually used with while and do-while loops.

What is variadic function in C programming?

Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed. The variadic function consists of at least one fixed variable and then an ellipsis (…) as the last parameter.

How to access values of passed arguments in a variadic function?

Values of the passed arguments can be accessed through the header file named as: This enables access to variadic function arguments. This one accesses the next variadic function argument. This makes a copy of the variadic function arguments. This ends the traversal of the variadic function arguments.

How does Microsoft sentinel work with log analytics?

Microsoft Sentinel gives you two tools to control this process: The Logs ingestion API allows you to send custom-format logs from any data source to your Log Analytics workspace, and store those logs either in certain specific standard tables, or in custom-formatted tables that you create.


1 Answers

You can use whatever value you want to mark the end of the argument list, provided you know enough about it grab to it from the argument list. The compiler won't normally check that the value you choose is actually passed when the function is called, though, so you'll need take care with it.

Essentially you just create a variadic function in the usual way, and inside the function you stop reading arguments when you read one that matches your sentinel -- there's nothing particularly special to be done, but you need to know what types of arguments to read.

For example:

#include <stdarg.h>

/* Count arguments up to the number 5 */
int countArgsTilFive(int first, ...)
{
  int res = 1;
  va_list ap;
  if (first == 5) return 0;
  va_start(ap,first);
  while (va_arg(ap,int) != 5) res++;
  va_end(ap);
  return res;
}

...will count all its arguments that occur before 5. Bad things could happen if you don't pass it a 5 somewhere in the list, though, or if you pass it arguments that aren't int.

Another example with pointers, where a sentinel node is passed as the first, and again as the last, argument:

/* Count arguments, excluding the first, until the first occurs again */
int countPtrs(void *sentinel, ...)
{
  int res = 0;
  va_list ap;
  va_start(ap,sentinel);
  while (va_arg(ap,void *) != sentinel) res++;
  va_end(ap);
  return res;
}
like image 142
Dmitri Avatar answered Oct 20 '22 23:10

Dmitri