C library - va_end() macro



The C stdarg library va_end() macro is used to perform the clean-up an ap object (variable of type va_list) initialized by va_start or va_copy. It may modify ap, so that it is no longer usable.

The va_end should be called after using a function that uses va_start or 'va_copy'. It must not be called without a corresponding 'va_start' or 'va_copy'. Doing so returns undefined behaviour.

This macro is used for memory management and furthermore prevents memory leaks.

Syntax

Following is the C library syntax of the va_end() macro −

void va_end(va_list ap)

Parameters

This macro accepts a single parameter −

  • ap − It is a va_list type variable that will be initialize by 'va_start'. This variable is used to traverse the list of arguments.

Return Value

This macro does not returns any value.

Example 1

The following is the basic c example that demonstrate the use of va_end().

#include <stdio.h>
#include <stdarg.h>
int sum(int count, ...) {
   va_list args;
   int tot = 0;
   
   // Set the va_list variable with the last fixed argument
   va_start(args, count);
   
   // Retrieve the arguments and calculate the sum
   for (int i = 0; i < count; i++) {
      tot = tot + va_arg(args, int);
   }
   
   // use the va_end to clean va_list variable
   va_end(args);
   
   return tot;
}

int main() {
   // Call the sum, with number of arguments
   printf("Sum of 3, 5, 7, 9: %d\n", sum(4, 3, 5, 7, 9));
   printf("Sum of 1, 2, 3, 4, 5: %d\n", sum(5, 1, 2, 3, 4, 5));
   return 0;
}

Output

Following is the output −

Sum of 3, 5, 7, 9: 24
Sum of 1, 2, 3, 4, 5: 15

Example 2: Count number of arguments

The following c example demonstrate the use of va_end() in a variadic function.

#include <stdio.h>
#include <stdarg.h>
int cnt(int count, ...) {
   va_list args;
   int num_of_arg = 0;

   // Set the va_list variable with the last fixed argument
   va_start(args, count);

   // Retrieve the arguments and count the arguments
   for (int i = 0; i < count; i++) {
      num_of_arg= num_of_arg + 1;
   }    
   // use the va_end to clean va_list variable
   va_end(args);

   return num_of_arg;
}

int main() {
   // Call the sum, with number of arguments
   printf("Number of arguments: %d\n", cnt(4, 3, 5, 7, 9));
   printf("Number of arguments: %d\n", cnt(5, 1, 2, 3, 4, 5));
   return 0;
}

Output

Following is the output −

Number of arguments: 4
Number of arguments: 5

Example 3: Finding the largest number

Let's see another example, we use the va_end() to clean-up the va_list variable after finding the largest number.

#include <stdarg.h>
#include <stdio.h>

int Largest_num(int n, ...) {
   va_list ap;
   va_start(ap, n);
   int max = va_arg(ap, int);
   for (int i = 0; i < n - 1; i++) {
      int temp = va_arg(ap, int);
      max = temp > max ? temp : max;
   }
   // use va_end to clean up va_list variable
   va_end(ap);
   return max;
}

int main() {
   printf("%d\n", Largest_num(2, 1, 2));
   printf("%d\n", Largest_num(3, 1, 2, 3));
   printf("%d\n", Largest_num(4, 6, 2, 3, 4));
   return 0;
}

Output

Following is the output −

2
3
6
Advertisements