Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:15:17+00:00 2026-06-09T22:15:17+00:00

I have a variadic function that requires a double-NULL terminator. Contrived, Simplified Example void

  • 0

I have a variadic function that requires a double-NULL terminator.

Contrived, Simplified Example

void makeLine(char *name, ...) {
    ... accepts (x,y) coordinates until (0,0) is found,
    ... assume that any coordinate except (0,0) is legal
            [i.e. (0,1) or (1,0) should not trigger a stop condition]
}

And I’d like to annotate it with __attribute ((sentinel))__ from gcc. But it seems that with the sentinel attribute, only one of the arguments can be required NULL as opposed to the desired sentinel(0,1) [last two required to be NULL].

Is there a way to ask gcc to enforce a double-NULL, or…

Given that the NULL can only be enforced for one of the two parameters, would you decorate this function with sentinel(0) or sentinel(1) and why? Which of the two positions would be more likely to catch bugs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:15:19+00:00Added an answer on June 9, 2026 at 10:15 pm

    Probably the only way to achieve compilation error in case of any of TWO sentinels is missing – is to use C99 variadic macros:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    
    #pragma GCC diagnostic error "-Wformat"
    
    void checkSentinel0(char * name, ...) __attribute__ ((sentinel(0)));
    void checkSentinel1(char * name, ...) __attribute__ ((sentinel(1)));
    
    void checkSentinel0(char * name, ...) {
        (void)name;
    }
    void checkSentinel1(char * name, ...) {
        (void)name;
    }
    
    #define MY_VARIADIC(name, ...) do {checkSentinel0(name, __VA_ARGS__);\
                                       checkSentinel1(name, __VA_ARGS__);\
                                           myVariadic(name, __VA_ARGS__);}\
                                   while(0);
    
    void myVariadic(char * name, ...) {
        // your code messing with coordinates and etc.
        (void)name;
    }
    
    int main () {
    
     MY_VARIADIC("test", 1,2,3,4, NULL, NULL); // Ok
    
     MY_VARIADIC("test", 1,2,3,4, 14,     15); // not compilable
     MY_VARIADIC("test", 1,2,3,4, NULL,   15); // not compilable
     MY_VARIADIC("test", 1,2,3,4, 15,   NULL); // not compilable
    
     return 0;
    }
    

    So if user is only exposed to your macro MY_VARIADIC then he/she will get error in case forgetting to terminate argument list with two null’s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class with a variadic template member function that I am
I have a function that takes variadic arguments, that I obtain from func_get_args() .
I have a variadic function: LogWrite(FILE * fp, int level, const char * filename,
I have a function that takes variadic arguments. These arguments are parameter-value pairs, so
I have a variadic function which takes a float parameter. Why doesn't it work?
I'm writing a 64bit operating system using g++, and I have a variadic function
I have some code that converts variadic parameters into a va_list , then passes
Let's say I have a variadic function foo(int tmp, ...) , when calling foo
C++0x thread library or Boost.thread define a non-member variadic template function that locks all
I have a variadic template function: template<typename T, typename ArgType> vector<T> createVector(const int count,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.