Re: Query on pointers
- From: Kenneth Brody <kenbrody@xxxxxxxxxxx>
- Date: Sun, 06 May 2007 15:58:40 -0400
mark_bluemel@xxxxxxxxx wrote:
On 4 May, 15:23, Kenneth Brody <kenbr...@xxxxxxxxxxx> wrote:
Also, your 7-byte buffer for the 8-byte string "Jupiter" (with the
'\0' terminator) "works" only by luck. The runtime library is
probably allocating more than 7 bytes, in order to handle proper
alignment, so you happen to have additional memory after your 7
bytes, and the 8th byte happens to be zero. Otherwise, the final
printf() wouldn't have stopped after the 7 characters "iupJter".
It would have kept going until it hit a '\0' or crashed.
I think, indeed I'm fairly sure, you are mistaken.
The snprintf() call will move "Jupite" into the buffer allocated, and
then add the null character to make it a legal C string.
Either the original poster is mistaken about the output from turboc,
or it's broken.
Well, some further investigation shows that the FreeBSD man pages say:
The snprintf() and vsnprintf() functions will write at most size-1
of the characters printed into the output string (the size'th
character then gets the terminating `\0'); [...] The output is
always null-terminated.
It also says that snprintf() is C99.
My C90 implementation doesn't even have snprintf(). However, it does
have an _snprintf(), which says:
The _snprintf function formats and stores count or fewer characters
and values (including a terminating null character that is always
appended unless count is zero or the formatted string length is
greater than or equal to count characters) in buffer.
So, it appears that a C99 implementation would put "Jupite". However,
a pre-C99 implementation is not required to do so. Given the fact
that the OP said the output included the 'r' in "iupJter", my guess is
that it is not a C99 implementation, and is not nul-terminating the
string.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@xxxxxxxxx>
.
- References:
- Query on pointers
- From: ajinkya . coep
- Re: Query on pointers
- From: Kenneth Brody
- Re: Query on pointers
- From: mark_bluemel
- Query on pointers
- Prev by Date: Re: gets() is dead
- Next by Date: Re: gets() is dead
- Previous by thread: Re: Query on pointers
- Next by thread: Re: Query on pointers
- Index(es):
Relevant Pages
|