delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2013/08/06/08:45:09

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: "Rod Pemberton" <dont_use_email AT nohavenotit DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: _CRT0_FLAG_NULLOK
Date: Tue, 06 Aug 2013 08:36:46 -0400
Organization: Aioe.org NNTP Server
Lines: 143
Message-ID: <op.w1eankdn0e5s1z@->
References: <21e77579-1a40-4442-8111-fc976fba78fc AT googlegroups DOT com>
<b5o37qFldl9U1 AT mid DOT dfncis DOT de>
<f24e8cd3-83ca-4386-a319-a9adb4d74c68 AT googlegroups DOT com>
<3df2f50f-9543-47a7-8e40-a9be82ce5018 AT googlegroups DOT com>
<87fvuvny2v DOT fsf AT uwakimon DOT sk DOT tsukuba DOT ac DOT jp> <op.w020sren0e5s1z@->
<87ehaeonbp DOT fsf AT uwakimon DOT sk DOT tsukuba DOT ac DOT jp> <op.w04oetou0e5s1z@->
<87zjt1n2v6 DOT fsf AT uwakimon DOT sk DOT tsukuba DOT ac DOT jp> <op.w08sjo1c0e5s1z@->
<8761vmn519 DOT fsf AT uwakimon DOT sk DOT tsukuba DOT ac DOT jp>
NNTP-Posting-Host: CNsg4fVcCsvs3UaOgZtQCw.user.speranza.aioe.org
Mime-Version: 1.0
X-Complaints-To: abuse AT aioe DOT org
User-Agent: Opera Mail/12.16 (Linux)
X-Notice: Filtered by postfilter v. 0.8.2
Bytes: 6431
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sat, 03 Aug 2013 11:54:42 -0400, Stephen J. Turnbull  
<turnbull AT sk DOT tsukuba DOT ac DOT jp> wrote:

> Rod Pemberton writes:
>  > On Thu, 01 Aug 2013 06:04:45 -0400, Stephen J. Turnbull
>  > <turnbull AT sk DOT tsukuba DOT ac DOT jp> wrote:
>  >
>  > > Rod Pemberton writes:
>  > >
>  > >  > E.g., [C definition committees] could've specified a NULL pointer
>  > >  > always points to an emtpy string of just a null character.  Such  
>  > > > a specification is unlikely to break C, since a NULL pointer just
>  > >  > needs to have a unique address.  This would've allowed a NULL
>  > >  > pointer to safely function as an empty character string too.
>  > >
>  > > Which is an arbitrary decision,
>  >
>  > True.  But, how is that any different than specifying NULL in the
>  > first place?
>
> All I'm saying is, look at
>
>     char *a = NULL;
>     char *b = "This is a pen.\n";
>     char *c = b + strlen(b);
>     char *d = malloc(255);
>     char *e = "";
>
> and tell me "which of these things is not like the others?"
>

a - no explicitly placed '\0'
b - has '\0' - placed by compiler, required
c - if valid code - has '\0' - points to b's '\0'
d - no explicitly placed '\0' - need calloc()...
e - has '\0' placed by the compiler, required

Let's look at this too:

   char *f = b + strlen(b) + 1;

f - no explicitly placed '\0'

What about this?

   char *g = malloc(0);

That can return NULL _or_ an implementation defined pointer.
If NULL, it's the same as 'a'.  If implementation defined, it
could be similar to 'e', or just be a pointer, like NULL, but
non-NULL.

To get back to your question, of the five, there are three
pointers which appear to point to correctly terminated
C strings and two which don't.

In terms of '\0' for 'a' to 'e', 'a' and 'd' are the same,
'b' and 'e' are the same, 'c' is unlike the other *two*
categories...

So, to best answer you're question, 'a' and 'c' are unlike
'b' and 'e' which are unlike 'c'...

>  > No, it wouldn't likely be a pointer to arbitrary instance of some
>  > type other than a char.  That's the only type that would serve
>  > some purpose due to the way strings are implemented in C.
>  > Other types don't have this issue.
>
> Huh?  Now you're speaking in non sequiturs.

A char type is the only type that must be fixed up by the compiler
or the user with a '\0' to be a string in C.  Other types, like float,
int, etc are guaranteed to be initialized to zero when not initialized
in the code.  This is required.  I.e., use of NULL as a string is
the only situation which leads to a problem.

> How often do you see idioms like:
>
>     void footloose_and_fancy_free (double **dp) {
>         free (*dp);
>         *dp = NULL;
>         }
>

If a NULL is passed in for dp, dereferencing it will return a
bad value.  This is true whether there is a '\0' at NULL or not.
It's clear that the code is incorrect for not checking that
dp is NULL and exiting.

I.e., this doesn't seem to have anything to do with the conversation.

> And I personally find it useful that on most OSes (*NULL) kills your
> program, no matter what type of pointer NULL has been coerced to.
> This has saved me from doing something stupid many times.

I don't see why that wouldn't happen with a '\0' at NULL.  If it
truly is a problem, one could place multiple '\0' at NULL, upto
the size in bytes of the machine's native integer.  The zero address
would be trapped for virtual memory systems by the unmapped zero page.

>  > It would only add additional functionality to NULL for use
>  > as char pointers.
>
> It doesn't add any functionality.  See variable "e" above.

'e' can't function as 'a'.  'a' is NULL.  This is with or without
a '\0' at NULL's location.  With the new functionality, NULL would
still function as NULL.  However, if a string function didn't check
for *a being NULL, it could be used as a string.

> It doesn't add any functionality.  [...]  It simply allows
> programmers to make mistakes with strings they couldn't make
> with other pointer types.

The ability for NULL to function as NULL and also an empty
string has never existed in C.  So, if implemented, this means
that the functionality is additional.  It's debatable as to how
useful it is, but so are 'void' and 'void*', initializing unitialized
variables, or other simple tweaks to C that prevent novices from
making coding errors.

> If you want the functionality you talk about, it's easy enough to get
> it without forcing implementations to do something bizarre with NULL.
> Just poke a zero into memory somewhere and get a pointer to it.  Give
> that pointer a name:
>
>     char *empty_string = "";
>

But, then, it's not NULL...

I.e., it wouldn't solve the OP's issue of passing in NULL into a
string function which doesn't check that the pointer was NULL,
and error out, safely.


Rod Pemberton
-- 
"... to secure these rights, governments are instituted among men,
deriving their just powers from the consent of the governed; that
whenever any form of government becomes destructive of these ends,
it is the right of the people to alter or abolish it ..."
-Thomas Jefferson, Declaration of Independence, 1776

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019