473,383 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

interpreting a null pointer as an empty (null string)

Which C libraries (current and historical) interpret a null pointer as
a pointer to a null (that is, empty) string?
Nov 14 '05 #1
11 2072


Dennis Allison wrote:
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.

--
Ñ
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch

Nov 14 '05 #2
On Fri, 05 Mar 2004 19:13:35 GMT, Nick Landsberg <hu*****@NOSPAM.att.net>
wrote:


Dennis Allison wrote:
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.


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.

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...
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
Leor Zolman <le**@bdsoft.com> writes:
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.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
Nov 14 '05 #4
On Fri, 05 Mar 2004 12:14:06 -0800, Ben Pfaff <bl*@cs.stanford.edu> wrote:
Leor Zolman <le**@bdsoft.com> writes:
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.


I didn't mean to imply they invented it, or even claimed they did. I'm just
happy the have it. Perhaps they're even due a few kudos just for being
willing to implement it even though they didn't "have" to, and/or in spite
of the fact someone might have chosen to come along and use it against them
(you know, MS-as-thief-of-other-people's-good-ideas and all that...)
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #5
Leor Zolman wrote:
.... 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.


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
CBFalconer <cb********@yahoo.com> spoke thus:
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.


I'm not sure why you'd get criciticism - it sounds like very
convenient behavior to me. Oh wait, that's why you're getting
criticism ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7
On Fri, 05 Mar 2004 21:51:28 GMT, CBFalconer <cb********@yahoo.com> wrote:
Leor Zolman wrote:

... 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.


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.


That's the power -- both beautiful and terrible -- of separation between
language and library...anyone can choose to use the standard string
functions or to use yours...or even (saints have mercy) put the standard
names on your implementations and sneak them into the library ;-)
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #8
On Fri, 05 Mar 2004 19:41:55 GMT, Leor Zolman <le**@bdsoft.com> wrote
in comp.lang.c:
On Fri, 05 Mar 2004 19:13:35 GMT, Nick Landsberg <hu*****@NOSPAM.att.net>
wrote:


Dennis Allison wrote:
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.


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.


Nothing non-standard about it at all. The standard no longer applies
once a program invokes undefined behavior, and passing a null pointer
to any standard library function that does not specifically state that
it accepts them (such as realloc(), free(), strto*()) is specifically
undefined behavior.

Some programmers might prefer a guaranteed unmistakeable crash to
bring a coding defect to their attention, but an implementation that
does this is no more or less than conforming than one that treats a
null pointer as pointing to a '\0' character.

At most it's a QOI issue.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
On Fri, 05 Mar 2004 19:13:35 GMT, Nick Landsberg
<hu*****@NOSPAM.att.net> wrote:
Dennis Allison wrote:
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.


VAX/VMS C still did this in the late 80's, IIRC.
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.


Porting others' VAX C code to Unix and OSF/1 was a real pain.

--
Sev
Nov 14 '05 #10

"Leor Zolman" <le**@bdsoft.com> wrote in message news:1d********************************@4ax.com...
On Fri, 05 Mar 2004 19:13:35 GMT, Nick Landsberg <hu*****@NOSPAM.att.net>
wrote:

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...


It's a barely useful hack if you're forced to use some minimal
functionality operating environment, and it was established
before MSVC was dreamt of. Many implementations before and after
that just protect a page at 0 so you get a memory access exception
when you try to access it. I'd rather be dropped into a debugger
or given a core file that shows me exactly which piece of code
attempted the access than given a message that says "some piece
of code that you executed in the last few minutes wrote to 0".
Nov 14 '05 #11
# > 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

Most unices nowadays leave page zero unmapped, so that as soon as you
load or store null, you get an error, rather than waiting some time
later.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
You hate people.
But I love gatherings. Isn't it ironic.
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: mike79 | last post by:
Hi all, I have a the following simple piece of code which has taken me hours to try and sort out the problem, but still unable to find what is wrong. void main( void ) { char (*string);...
8
by: sugaray | last post by:
Hi, I just came upon this code snippet which parses a string stored in buf with comma(,) as delimiter and store each substring into args, the question I'm having here is that I don't get why in the...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
44
by: sam_cit | last post by:
Hi Everyone, I tried the following program unit in Microsoft Visual c++ 6.0 and the program caused unexpected behavior, #include <stdio.h> #include <string.h> int main() {
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
6
by: db2admin | last post by:
hello, db2 does not recognize '' as null? say i have T1(C1 INT,C2 CHAR(4)) and i insert (1,'') , it would not be same as (1,NULL) what db2 consider '' as? how does it store value '' as if not...
6
by: linq936 | last post by:
Hi, I have the following code: #include <stdio.h> int main(void){ char* str1 = "abc"; char* str2 = '\0'; if ( strstr(str1, str2) == NULL ){ printf("yes\n");
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.