mirkokiefer
mirkokiefer

Reputation: 3537

weird variable arguments issue in C

I get an EXC_BAD_ACCESS error in the last statement when calling the following simplified function:

void test(char *param, ...) {
  va_list vl;
  va_start(vl, param);
  double a = va_arg(vl, double);
  double b = va_arg(vl, double);
  double *result = va_arg(vl, double*);
  *result = a*b;
  va_end(vl);
}

The function is called with:

double result;
test("blub", 3, 3, &result);

I'm using Xcode's clang compiler (Apple LLVM compiler 3.1).

Upvotes: 1

Views: 80

Answers (1)

Pavan Manjunath
Pavan Manjunath

Reputation: 28545

I think the problem is in you sending a double as 3 instead of 3.0. A normal 3 will be treated as integer but in the test function you are retrieving doubles which are bigger than an int on most platforms and you might end up reading wrong locations which inturn leads to EXC_BAD_ACCESS run time signal being generated

Upvotes: 4

Related Questions