Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 170129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:45:12+00:00 2026-05-11T12:45:12+00:00

Below is code which includes a variadic function and calls to the variadic function.

  • 0

Below is code which includes a variadic function and calls to the variadic function. I would expect that it would output each sequence of numbers appropriately. It does when compiled as a 32-bit executable, but not when compiled as a 64-bit executable.

#include <stdarg.h> #include <stdio.h>  #ifdef _WIN32 #define SIZE_T_FMT '%Iu' #else #define SIZE_T_FMT '%zu' #endif   static void dumpargs(size_t count, ...) {      size_t i;     va_list args;      printf('dumpargs: argument count: ' SIZE_T_FMT '\n', count);      va_start(args, count);      for (i = 0; i < count; i++) {          size_t val = va_arg(args, size_t);         printf('Value=' SIZE_T_FMT '\n', val);     }     va_end(args); }  int main(int argc, char** argv) {      (void)argc;     (void)argv;      dumpargs(1, 10);     dumpargs(2, 10, 20);     dumpargs(3, 10, 20, 30);     dumpargs(4, 10, 20, 30, 40);     dumpargs(5, 10, 20, 30, 40, 50);      return 0; } 

Here is the output when compiled for 64-bit:

dumpargs: argument count: 1 Value=10 dumpargs: argument count: 2 Value=10 Value=20 dumpargs: argument count: 3 Value=10 Value=20 Value=30 dumpargs: argument count: 4 Value=10 Value=20 Value=30 Value=14757395255531667496 dumpargs: argument count: 5 Value=10 Value=20 Value=30 Value=14757395255531667496 Value=14757395255531667506 

Edit:

Please note that the reason the variadic function pulls size_t out is because the real-world use of this is for a variadic function that accepts a list of pointers and lengths. Naturally the length argument should be a size_t. And in some cases a caller might pass in a well-known length for something:

void myfunc(size_t pairs, ...) {     va_list args;     va_start(args, count);      for (i = 0; i < pairs; i++) {         const void* ptr = va_arg(args, const void*);         size_t len = va_arg(args, size_t);         process(ptr, len);     }     va_end(args); }  void user(void) {     myfunc(2, ptr1, ptr1_len, ptr2, 4); } 

Note that the 4 passed into myfunc might encounter the problem described above. And yes, really the caller should be using sizeof or the result of strlen or just plain put the number 4 into a size_t somewhere. But the point is that the compiler is not catching this (a common danger with variadic functions).

The right thing to do here is to eliminate the variadic function and replace it with a better mechanism that provides type safety. However, I would like to document this problem, and collect more detailed information as to exactly why this problem exists on this platform and manifests as it does.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:45:12+00:00Added an answer on May 11, 2026 at 12:45 pm

    So basically, if a function is variadic, it must conform to a certain calling convention (most importantly, the caller must clean up args, not the callie, since the callie has no idea how many args there will be).

    The reason why it starts happening on the 4th is because of the calling convention used on x86-64. To my knowledge, both visual c++ and gcc use registers for the first few parameters, and then after that use the stack.

    I am guessing that this is the case even for variadic functions (which does strike me as odd since it would make the va_* macros more complicated).

    On x86, the standard C calling convention is the use the stack always.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

EDIT: After re-reading my post I think I am being a little bit unclear
We have some solution that we built against a MOSS farm one of which
In my Groovy program, I have a list of lines and want to select
Thanks for reading this. I am dynamically generating some data which includes a select

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.