Solved

va_arg problem

Posted on 2009-05-11
10
1,005 Views
Last Modified: 2013-11-05
I am trying to create a function with a variable argument list. But, the code I have written (see below) is not giving me the first argument of the variable argument list.

The output to the program should be:

1
2
3

but I am getting

2
3
0

where the last value is out of bounds for the ap pointer. I used gcc 4.3.2 and intel ia32 11.0 compilers on Mac OS X 10.5.6, and got the same results on both.

I found that if I add the following line before the first call to va_arg() that I will get the correct output:

ap -= sizeof(size_t);

but I should not need to manually change the pointer before using it.

What am I doing wrong and how can I fix it?

Thank you in advance.
#include <iostream>

#include <stdarg.h>
 

void vatest(const unsigned int n, const size_t args, ...) {

  size_t x;

  va_list ap;

  va_start(ap, args);
 

//  ap -= sizeof(size_t); // I should not need this line!

  for(unsigned int i = 0; i < n; ++i) {

    x = va_arg(ap, size_t);

    std::cout << x << std::endl;

  }
 

  va_end(ap);

}
 

int main() {

  vatest(3, 1, 2, 3);

  return 0;

}

Open in new window

0
Comment
Question by:gandalf79
  • 4
  • 2
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 24357459
There is no need to use 'args' in your argument list, in fact this is what is causing the error. The last non-variable argument is 'n', so use this in 'va_start()', e.g.
#include <iostream>

#include <stdarg.h>

 

void vatest(const unsigned int n, ...) {

  size_t x;

  va_list ap;

  va_start(ap, n);

 

  for(unsigned int i = 0; i < n; ++i) {

    x = va_arg(ap, size_t);

    std::cout << x << std::endl;

  }

 

  va_end(ap);

}

 

int main() {

  vatest(3, 1, 2, 3);

  return 0;

}
 

Output:
 

$ vatest

1

2

3

Open in new window

0
 
LVL 9

Expert Comment

by:wellhole
ID: 24357470
You're passing the parameters wrong. Get rid of argument args and use n as the size for va_start.
0
 
LVL 2

Author Comment

by:gandalf79
ID: 24357628
Thanks for the quick response. I made the following changes and it work perfectly now.

void vatest(const unsigned int n, ...) {
  // ...
  va_start(ap, n);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 24357641
Um, may I ask what was wrong with my - a bit more complete - answer?
0
 
LVL 2

Author Comment

by:gandalf79
ID: 24357818
I am sorry, I accidentally over looked your solution. When I looked at it, I thought your code was my code. I will attempt to rectify the situation.
0
 
LVL 2

Author Comment

by:gandalf79
ID: 24357864
Please, I am looking but I don't see how I can modify the accepted answer. =(

Had I not overlooked your solution, I would have given you credit for the question since yours is more complete. Thank you for your understanding.
0
 
LVL 2

Author Closing Comment

by:gandalf79
ID: 31580262
Thank you for your help and the quick response.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 67
VS2015 compilation and missing DLLs 9 114
C++ Language error 28 186
Path to  STL Map header file 1 41
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now