Re: sprintf counterparts?
- From: "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@xxxxxxxxx>
- Date: Wed, 18 Apr 2007 15:47:16 +0200
Hi Norman!
char szBuf[1024];No, DON'T DO IT LIKE THIS.
_snprintf(szBug, 1024, ...);
szBuf[1024-1] = 0;
You're defeating the purpose of _snprintf by hard coding the size of the string in places where it is not necessary.
What if someone comes along later and decides that allocating 1024 bytes is wasteful and changes only the "char" statement to, say, 256?
This was just an example...
Always try to write code so that values which depend on the compile-time size of a buffer really do use the compile-time size of the buffer.
char szBuf[1024];
/* lots of other stuff here */
_snprintf(szBug, sizeof szBuf, ...);
szBuf[sizeof szBuf-1] = 0;
It also does not work if your "szBuf" was allocated via "new/malloc"...
So again... it was just a example to demonstrate how to use _snprintf and demonstarte that you *must* terminate the string with NUL.
Greetings
Jochen
.
- References:
- sprintf counterparts?
- From: Jack
- Re: sprintf counterparts?
- From: Jochen Kalmbach [MVP]
- Re: sprintf counterparts?
- From: Norman Bullen
- sprintf counterparts?
- Prev by Date: Re: running Application as Service kills Network connections
- Next by Date: Re: atlprint.h errors
- Previous by thread: Re: sprintf counterparts?
- Next by thread: Re: sprintf counterparts?
- Index(es):
Relevant Pages
|