Inefficient use of sprintf
The function sprintf
, snprintf
, or
swprintf
copies strings instead of the more efficient
strcpy
, strncopy
, or
wcsncpy
Since R2021b
Description
This checker is triggered when you use sprintf
,
snprintf
, or swprintf
to copy strings.
Risk
The functions sprintf
, snprintf
, or
swprintf
are complex function with a variable argument list. Before
executing these functions, the compiler parses the argument list to determine the argument
types, which adds overhead to the code. Handling the different input formats that these
functions support makes the function difficult to optimize. For instance, even if you want
to copy only strings by using sprintf
, the function must still support
copying integers. Using these functions for copying strings make your code
inefficient.
Fix
To fix this defect, refactor your code and use dedicated functions such as
strcpy
, strcat
, strncopy
,
wcsncpy
or their variants to copy strings.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
Group: Performance |
Language:C | C++ |
Default: Off |
Command-Line Syntax:
INEFFICIENT_SPRINTF |
Impact: Medium |
Version History
Introduced in R2021b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)