Re: Query on pointers



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>

.



Relevant Pages

  • Re: give me some tips
    ... A text stream is an ordered sequence of characters composed into ... terminating new-line character is implementation-defined. ... I don't know of any implementations that actually require the trailing ... overwritten by the next shell prompt. ...
    (comp.lang.c)
  • snprintf() return value
    ... output characters beyond the n-1st are discarded rather ... "The snprintf function returns the number of characters that would have ... or a negative value if an encoding error ... BC++ 5.5.1 (Windows): 10 ...
    (comp.lang.c)
  • Re: How to use Delete in right way
    ... > This code corrupts the heap, because strlen returns the number of ... > excluding the terminating null. ... but this breaks most non-trivial applications because win32 heap ...
    (microsoft.public.vc.debugger)
  • Re: K&R2 , exercise 7.6
    ... characters including the terminating newline. ... The second declaration says that pc is a pointer to type char and ... The %s format specifier looks for a char * ...
    (comp.lang.c)
  • Re: snprint rationale?
    ... It allows you to find out by how much to enlarge your final string ... What is troubling me about this is that snprintf ... Returning 0 on error and the hypothetical number of written characters ... being written to the array, and a null character is written at the end ...
    (comp.lang.c)