Why ellipsis are dangerous in C++

Here we will be understanding and discussing ellipsis and their dangers in C++. C++ provides a special specifier that allows a variable number of parameters to pass through a function. Normally, we pass definite parameters in the functions that we already know. However, the ellipsis (which are denoted by ‘…’) helps any number of parameters to pass through the functions by specifying the number of parameters.

Form of a function using ellipses:

return_type function_name( no_of_arguments,...)

At least there should be one normal parameter in the functions using ellipses specifying the number of parameters passed by ellipses. The ellipses should always be the ending parameter.

Header file needed: <cstdarg>

Ellipses are accessed through va_list which is initialized using va_start()va_argument is used to get parameters from ellipses.

Let’s understand ellipses through an example:

#include <iostream>

using namespace std;
double findsum( int n, ...) { double sum=0;

va_list list; //declare list to acess eliipses

va_start(list,n); //initialize the list using va_start where 1st parameter is the list and 2nd is the last non ellipses paramter

for(int i=0;i<n;i++) //parameters fetched through va_i

return sum;

int main()
cout<<" Sum: "<<findsum(5,8,9,67,89,23);
return 0;
sum: 196

Why are ellipses dangerous

1. The compiler suspends type checking

Normally, the compiler does the type check for the regular functions, wherein it checks that the data type of the function arguments matches the data type of its parameters.
For eg:  If the parameter passed is an integer, however, the function was expecting a character, so the compiler will show an error.

However, the compiler suspends the type checking for ellipses as they don’t have a defined data type. Hence, sometimes the passed parameters may not make any sense and have incorrect output.

For eg: if in the above code we call the function as:

int main()
    double a=4.2;
    cout<<" Sum: "<<findsum(5,8,9,67,a,23);
    return 0;

The output will be:

sum: -1.99208e+09

Hence, the ellipses don’t consider the data type of parameters and can print wrong outputs.

2. The ellipses don’t keep track of the number of arguments passed

We need to specify the number of arguments we are passing through ellipses, otherwise, it may take garbage values. Moreover, if we pass more or fewer parameters that specified then the output can be incorrect.

For eg: If we call the findsum() function as: findsum(4,1,2,3). The output will not be 6.
Here we specified we will be passing 4 arguments through ellipses, however, we only passed 3. So, we take the first three from va_arg, but it takes a fourth garbage value. This makes the output incorrect.

Leave a Reply

Your email address will not be published. Required fields are marked *