Question: I have this piece of code which keeps throwing a 'buffer too small' error during debug.

```
        geoGraph.size=limit;
        CString xAxis ="X axis: ",yAxis="Y axis: ";

        for (int x = 0; x < limit; x++)
        {

            xAxis.Format(_T("%s%i  "),xAxis,(x+1));
            yAxis.Format(_T("%s%s  "),yAxis,dialog_test.str[x]);

        }

        xAxis.Format(_T("%s \n%s  "),xAxis,yAxis);// <---Error thrown

        d.SetWindowTextA(xAxis);

```

I came into the conclusion that the error was due to the fact that the Cstring xAxis is too small to contain the new text, am I correct and if so, how do I remedy it?

Thanks.

Edit: I'm curios as to why this error is only shown during debugging and not when I'm running the application with 'start without debugging' (I use VS2008).

Answer: 
The documentation for [CStringT::Format](https://msdn.microsoft.com/en-us/library/18he3sk6.aspx) contains the following remark:

> 
> The call will fail if the string object itself is offered as a parameter to Format. For example, the following code will cause unpredictable results:
> 
> 
> 
> ```
> CAtlString str = _T("Some Data");
> str.Format(_T("%s%d"), str, 123);
> // Attention: str is also used in the parameter list.
> 
> ```
> 
> 

To work around this, you can either create a copy for the argument, or - in this specific case - call the [CStringT::AppendFormat](https://msdn.microsoft.com/en-us/library/cfw3cb2a.aspx) class member instead:

```
geoGraph.size = limit;
CStringW xAxis = L"X axis: ",
         yAxis = L"Y axis: ";

for ( int x = 0; x < limit; ++x ) {
    xAxis.AppendFormat( L"%i  ", x + 1 );
    yAxis.AppendFormat( L"%s  ", dialog_test.str[x].GetString() );
}

xAxis.AppendFormat( L" \n%s  ", yAxis.GetString() );

```

Note in particular, that this implementation calls [CSimpleStringT::GetString](https://msdn.microsoft.com/en-us/library/k59cayc6.aspx) to retrieve a C-style string for use with the `%s` format specifier. Passing a `CString` object for a `%s` placeholder is not documented to work\*.

Also observe, that this implementation uses explicit wide character versions of [CString](https://msdn.microsoft.com/en-us/library/5bzxfsea.aspx) objects, as well as wide character string literals. All supported versions of Windows use Unicode (UTF-16) as their native encoding. Using Unicode in applications is thus a natural choice.

---

\* *All currently supported versions of ATL/MFC implement `CString` in a way that allows to pass a `CString` object anywhere a C-style character string is expected. This also works for variable argument lists. The latter is an implementation detail, and not part of the documented contract. It should not be relied upon.*
If your objective is to reuse the variables, you can do:

```
    geoGraph.size=limit;
    CString xAxis ="X axis: ",yAxis="Y axis: ";

    for (int x = 0; x < limit; x++)
    {

        xAxis.Format(_T("%s%i  "), CString(xAxis),(x+1));
        yAxis.Format(_T("%s%s  "), Cstring(yAxis),dialog_test.str[x]);

    }

    xAxis.Format(_T("%s \n%s  "), CString(xAxis),yAxis);

    d.SetWindowTextA(xAxis);

```

I had a similar situation and it worked, because I am now using a copy of the xAxis variable instead of it directly.