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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:01:00+00:00 2026-06-03T20:01:00+00:00

I am attempting to make a function that will make combining C Strings and

  • 0

I am attempting to make a function that will make combining C Strings and std::strings a little easier in my C++ WinAPI Application.

So instead of doing this:

TCHAR res[MAX_PATH];
_stprintf(res, _T("In functionX(): error occured where the variable values are %d, %u, %s, %c"), myInt, myUnsignedInt, myStr.c_str(), myChar);
MessageBox(NULL, res, _T("Error Occurred"), MB_OK);

I just have to do this(which makes it a little easier to merge different string types because I don’t have to keep declaring TCHAR arrays everywhere):

tstring res = concat(_T("In functionX(): error occured with the variable values %d, %u, %s, %c"), myInt, myUnsignedInt, myStr.c_str(), myChar);
MessageBox(NULL, (LPTSTR)res.c_str(), _T("Error Occurred"), MB_OK);

My Problem: My function concat(); fails when I pass more than 1 variable inside the parameter format and I dont know why?

// The following function call causes the error
tstring ou = concat(_T("In functionX(): Failed to create temp file - %s - %s\r\n"), (LPTSTR)tempFileRootDir.c_str(), tempFile); 

tstring WinFile::concat( TCHAR* strFormat, TCHAR* format, ... )
{
    // tstring is either a std::string or std::wstring depending on whether unicode is used
    // Post: Wrapper function to easily merge C++ strings with C Strings

    va_list arguments;
    va_start(arguments, format);
    TCHAR res[MAX_PATH];
    _stprintf(res, strFormat, format); 
    return tstring(res);
}

The error that occurs when I run the function in Microsoft Visual C++ is:

A buffer overrun has occurred in Application.exe which has
corrupted the program’s internal state. Press Break to debug the
program or Continue to terminate the program.

For more details please see Help topic ‘How to debug Buffer Overrun
Issues’.

  • 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-03T20:01:01+00:00Added an answer on June 3, 2026 at 8:01 pm

    It looks like your varargs handling is a bit off. And yes, by writing to a fixed size buffer, you’re asking for a stack overflow.

    To correct the first, you’ll need to use a varargs-accepting version of printf. For the second, you should count before you print:

    tstring WinFile::concat( TCHAR const * strFormat, ... )
    {
        va_list args;
    
        // Determine how much space to reserve.
        va_start(args, strFormat);
        size_t msg_len=_vsctprintf(strFormat, args);
        va_end(args);
    
        // Reserve space on heap.
        //
        // "_vscprintf returns the number of characters that would be generated
        // if the string pointed to by the list of arguments was printed ... 
        // [and] does not include the terminating null character."
        //
        // So we add space for a terminating null.
        std::vector<TCHAR> writebuffer(1+msg_len);
    
        // perform formatting
        va_start(args, strFormat);
        _vstprintf(&writebuffer[0], strFormat, args);
        va_end(args);
    
        // return a copy as a tstring.
        return tstring(&writebuffer[0], &writebuffer[msg_len]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to make a function in PHP that will evaluate a mathematical expression
I'm attempting to make a function that will allow users to delete posts they
Attempting to make a NSObject called 'Person' that will hold the login details for
So I am attempting to make a module that, when imported, will cause any
I am attempting to make a check that once clicked will check all the
I'm attempting to make a function that writes a string to the last line
I am attempting to write a interpreter that will turn a string like: vector(random(0,
I am attempting to write some jQuery code that will expand a paragraph when
I just started working with jqGrid this week. I'm attempting to make a generic
I'm attempting to make a table that has header rows that can be collapsed

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.