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 7763987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:47:54+00:00 2026-06-01T14:47:54+00:00

During an interview I was requested (among other things) to implement the following function:

  • 0

During an interview I was requested (among other things) to implement the following function:

int StrPrintF(char **psz, const char *szFmt, ...);

similar to sprintf, except instead of the already-allocated storage the function must allocate it itself, and return in the *psz variable. Moreover, *psz may point to an already-allocated string (on the heap), which may potentially be used during the formatting. Naturally this string must be free by the appropriate means.

The return value should be the length of the newly created string, or negative on error.

This is my implementation:

int StrPrintF(char **psz, const char *szFmt, ...)
{
    va_list args;
    int nLen;

    va_start(args, szFmt);

    if ((nLen = vsnprintf(NULL, 0, szFmt, args)) >= 0)
    {
        char *szRes = (char*) malloc(nLen + 1);
        if (szRes)
            if (vsnprintf(szRes, nLen + 1, szFmt, args) == nLen)
            {
                free(*psz);
                *psz = szRes;
            }
            else
            {
                free(szRes);
                nLen = -1;
            }
        else
            nLen = -1;
    }

    va_end(args);
    return nLen;
}

The question author claims there’s a bug in this implementation. Not just a standard violation that may fail on particular esoteric systems, but a “real” bug, which by chance may fail on most systems.

It’s also not related to usage of int instead of memory-capability-suited type, such as size_t or ptrdiff_t. Say, the strings are of “reasonable” size.

I really have no clue of what the bug could be. All the pointer arithmetic is ok IMHO. I even don’t assume that two consequent invocations of vsnprintf produce the same result. All the variadic-handling stuff is also correct IMHO. va_copy is not needed (it’s the responsibility of the callee that uses va_list). Also on x86 va_copy and va_end are meaningless.

I’ll appreciate if someone can spot the (potential) bug.

EDIT:

After checking out the answers and comments – I’d like to add some notes:

  • Naturally I’ve built and run the code with various inputs, including step-by-step in debugger, watching the variables state. I’d never ask for help without trying things myself first. I saw no sings of problems, no stack/heap corruption, etc. Also I’ve run it in debug build, with the debug heap enabled (which is intolerant to heap corruption).
  • I assume that the function is called with valid parameters, i.e. psz is a valid pointer (not to confuse with *psz), szFmt is a valid format specifier, and all the variadic parameters are evaluated and correspond to the format string.
  • Calling free with NULL pointer is ok according to the standard.
  • Calling vsnprintf is ok with NULL pointer and size=0. It should return the resulting string length. MS-version, though not fully standard-compliant, does the same in this specific case.
  • vsnprintf won’t exceed the specified buffer size, including the 0-terminator. Means – it does not always places it.
  • Please put the coding style aside (if you don’t like it – fine with me).
  • 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. Editorial Team
    Editorial Team
    2026-06-01T14:47:56+00:00Added an answer on June 1, 2026 at 2:47 pm

    va_copy is not needed (it’s the responsibility of the callee that uses
    va_list)

    Not quite right. I didn’t find any such requirement for vsnprintf in the C11 standard. It does say this in a footnote:

    As the functions vfprintf, vfscanf, vprintf, vscanf, vsnprintf,
    vsprintf, and vsscanf invoke the va_arg macro, the value of arg after
    the return is indeterminate
    .

    When you call vsnprintf, the va_list can be passed by value or by reference (it’s an opaque type for all we know). So the first vsnprintf can actually modify va_list and ruin things for the second. The recommended approach is to make a copy using va_copy.

    And indeed, according to this article it doesn’t happen that way on x86 but it does on x64.

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

Sidebar

Related Questions

I have faced the following interview question. Consider this function declaration: void quiz(int i)
During sign-in I'm using following function to set cookies and session protected function validateUser($userid,
During .Net interview, I was asked what is function augmentation. Never heard it and
I was asked the following question during phone interview I had: Given the following
I was asked a question during an C Language interview. the question is: int
I came across this problem during an interview forum., Given an int array which
I was asked in an interview the following question. int countSetBits(void *ptr, int start,
I heard this today during interview for java developer. I had to list some
A friend of mine was asked, during a job interview, to write a program
I was asked this question recently during my job interview, and I couldn't answer

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.