Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Is it safe to completely ignore variadic arguments to a C function?

I'm have a function that roughly looks like so:

typedef struct SomeType {
  ...
} SomeType;

void TakesArgs(SomeType *t1, ...) {
     // iterates through arguments
}

// usage:
TakesArgs(&a, &b, &c);

Do I run any strange risks with memory (or otherwise) if I were to change TakesArgs to a no-op, while still leaving all calling code unchanged?

void TakesArgs(SomeType *t1, ...) {
    return;
}

// usage unchanged: 
TakesArgs(&a, &b, &c);

In other words, will skipping the va_list/va_start dance that was executed in the original implementation have any strange side effects?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Yes, it is perfectly safe. You are not required to read all variadic arguments just for the sake of reading them. You are not required to even start the variadic argument reading sequence inside TakesArgs.

Under the hood it usually means that the burden of performing any argument-passing maintenance tasks is bestowed upon the calling code (they way it works in what is usually recognized as "traditional" C calling convention). The callee does not have to do anything.

over 4 years ago · Santiago Trujillo Report

0

[Edited in response to comments.]

There are systems where by default it is the callee's job to pop arguments. That would obviously get you into trouble. But, it's precisely for this reason that variadic functions have to be properly declared, and the declaration has to be properly visible to callers. The reason is that on those systems, variadic functions have to use a non-default calling sequence where the caller pops the arguments, because only the caller can be sure how many there are. So you should be safe as long as your functions are properly declared.

over 4 years ago · Santiago Trujillo Report

0

I believe it's safe, though the standard isn't quite explicit on this point.

N1570 7.16 paragraph 3 says:

If access to the varying arguments is desired, the called function shall declare an object (generally referred to as ap in this subclause) having type va_list.

Note the beginning of that sentence: "If access to the varying arguments is desired". The implication is that if access to the varying arguments is not desired, there's no need to declare a va_list object -- which would make it impossible to invoke va_start, va_arg, or va_end.

A possible counterargument is that the description of va_end (N1570 7.16.1.3) says:

If there is no corresponding invocation of the va_start or va_copy macro, or if the va_end macro is not invoked before the return, the behavior is undefined.

However, given the context, I believe that applies if va_end is not invoked after va_start is invoked.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!