On Sep 13, 4:08 am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.com> wrote:
> <jeff_j_dun...@yahoo.com> wrote in message
> : In the following function, pleas assume that the Date object is well
> : written. What I really want to know is if my char buff is being
> : handled safely.
>
> : int iMonth=0, iDay=0, iYear=0;
> : d.GetDate(iYear, iMonth, iDay);
> : sprintf(buff, "%d/%d/%d", iMonth, iDay, iYear);
As you say, this is dreadful code because it will
buffer overflow if unexpected values comes from
GetDate. The buff should be at least 3 * INT_BYTES + 3
in length (where INT_BYTES is the number of characters
needed to print INT_MIN). snprintf is a good solution.
> Some ideas for dealing with fixed-size char buffers in C++:
>
> You can take a fixed size char array by reference:
> void fbDateToStr( const IBPP::Date &d, char (&buff)[11] )
> This will check that the caller provides an array of the
> exact desired size.
You can achieve the same thing in C with: char (*buff)[11]