D
Dennis Allison
Which C libraries (current and historical) interpret a null pointer as
a pointer to a null (that is, empty) string?
a pointer to a null (that is, empty) string?
Dennis said:Which C libraries (current and historical) interpret a null pointer as
a pointer to a null (that is, empty) string?
There was at least one implementation (pre C-89), which
took great pains into putting 0x0 at location zero
in the data segment.
Thus, the NULL pointer (on that implemenation a zero)
would always point to a location with the contents '\0'
for a char *, and all zero bits for other pointers to
that location.
Needless to say, things broke in myriad ways when
code was ported to another implementation.
Leor Zolman said:Anyway, as a postscript to Nick's remarks, I'm compelled to give out kudos
to the early MSVC team for establishing what I think was an excellent
little "hack" in their runtime system in debug mode: They place a magic
value at location zero, and after program execution check to see if it has
changed. If so, a nice diagnostic ("NULL pointer assignment...") is
emitted. This has probably saved me countless hours of head-scratching over
runtime meltdowns...
This has a much older history than MSVC. It was definitely in
even fairly early versions of Turbo C for DOS, and I don't
remember any claims that they invented the idea.
Leor said:.... snip ...
I get the feeling the OP was asking if there are/were any string-
handling libraries that always check for a special-case of 0 when
handed a char *, and do some reasonable thing in that case.
I don't know, but if there were then that would have to be
considered some sort of non-standard extension, and it might even
be offensive to folks using the string lib functions because it
implies extra special-case overhead that proper use of those
pointers would have rendered unnecessary.
CBFalconer said:My implementations of strlcat and strlcpy (available on my site)
do just that, and have drawn criticism for it. Thus "strlcpy(s,
NULL, size);" will create an empty string in s. My attitude is to
avoid crashes whenever I can give a NULL argument a reasonable
interpretation.
My implementations of strlcat and strlcpy (available on my site)
do just that, and have drawn criticism for it. Thus "strlcpy(s,
NULL, size);" will create an empty string in s. My attitude is to
avoid crashes whenever I can give a NULL argument a reasonable
interpretation.
And even /that/ is more to the tune of "the platform tries to minimize the
potential damage in the case when a C program (user code or lib function)
mistakenly treats a null pointer as if it were a valid pointer to
something".
I get the feeling the OP was asking if there are/were any string-handling
libraries that always check for a special-case of 0 when handed a char *,
and do some reasonable thing in that case.
I don't know, but if there were then that would have to be considered some
sort of non-standard extension, and it might even be offensive to folks
using the string lib functions because it implies extra special-case
overhead that proper use of those pointers would have rendered unnecessary.
There was at least one implementation (pre C-89), which
took great pains into putting 0x0 at location zero
in the data segment.
Thus, the NULL pointer (on that implemenation a zero)
would always point to a location with the contents '\0'
for a char *, and all zero bits for other pointers to
that location.
Needless to say, things broke in myriad ways when
code was ported to another implementation.
Leor Zolman said:Anyway, as a postscript to Nick's remarks, I'm compelled to give out kudos
to the early MSVC team for establishing what I think was an excellent
little "hack" in their runtime system in debug mode: They place a magic
value at location zero, and after program execution check to see if it has
changed. If so, a nice diagnostic ("NULL pointer assignment...") is
emitted. This has probably saved me countless hours of head-scratching over
runtime meltdowns...
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.