Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda