473,804 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nullable/Notnull : syntax proposal

Hello,

I am proposing to create two new keywords for C, 'notnull' and
'nullable'.
Their meaning is a sort of 'design by contract', ensuring some rules
are
verified at compile-time. It should not require additional runtime-
checks
to be implemented.
I have posted my description of how it should work at this URL:
http://sarnold.free.fr/wordpress/?p=71
Please read it and tell me what you think of it.

Regards,
Stéphane A.

Nov 4 '07
20 2435
Eric Sosman <es*****@ieee-dot-org.invalidwrit es:
sa*****@free.fr wrote:
>Hello,

I am proposing to create two new keywords for C, 'notnull' and
'nullable'.
Their meaning is a sort of 'design by contract', ensuring some rules
are
verified at compile-time. It should not require additional runtime-
checks
to be implemented.
I have posted my description of how it should work at this URL:
http://sarnold.free.fr/wordpress/?p=71
Please read it and tell me what you think of it.

Your proposal should describe why the proposed feature
would be useful, why it would make life better for programmers
and/or improve the quality of programs. If the information
is already there, it is presented too subtly for me to grasp.
It is pretty obvious what "notnull" means. But as I said in a previous
post if the use is limited to allocation assignments better to do the
checks and relevant dumps in the allocation functions themselves.
>
By the way, the example with free() is not well-chosen.
The behavior of free() with a NULL argument is well-defined;
there is no need to assert that the argument cannot be NULL,
because it can be. Perhaps an example with fclose() or
strlen() would raise fewer eyebrows.
Nov 5 '07 #11
On Sun, 04 Nov 2007 20:12:30 +0100, jacob navia wrote:
Harald van Dijk wrote:
>On Sun, 04 Nov 2007 18:53:49 +0100, jacob navia wrote:
>>You can assert a parameter not being null in standard C by

int function(int MyTable[static 1]);

Almost, but not quite.
>>This means that the parameter MyTable has at least one element, i.e.
it can't be NULL.

It means you can't call function with the result of malloc(1), while
occasionally (okay, rarely) that's actually useful.

Can you explain what you say?

I didn't understand what you are saying.
Assuming sizeof(int) 1, and some free memory, malloc(1) is a non-null
pointer that doesn't point to at least one int. So if you pass that
pointer to a function defined as taking int[static 1], the behaviour is
undefined. If you could specify a function as needing a non-null pointer,
it would (presumably) be allowed, since it is a valid pointer which is not
a null pointer.
Nov 5 '07 #12
Harald van Dijk wrote:
On Sun, 04 Nov 2007 20:12:30 +0100, jacob navia wrote:
>Harald van Dijk wrote:
>>On Sun, 04 Nov 2007 18:53:49 +0100, jacob navia wrote:
You can assert a parameter not being null in standard C by

int function(int MyTable[static 1]);
Almost, but not quite.

This means that the parameter MyTable has at least one element, i.e.
it can't be NULL.
It means you can't call function with the result of malloc(1), while
occasionall y (okay, rarely) that's actually useful.
Can you explain what you say?

I didn't understand what you are saying.

Assuming sizeof(int) 1, and some free memory, malloc(1) is a non-null
pointer that doesn't point to at least one int. So if you pass that
pointer to a function defined as taking int[static 1], the behaviour is
undefined. If you could specify a function as needing a non-null pointer,
it would (presumably) be allowed, since it is a valid pointer which is not
a null pointer.
I think the "static 1" means that you have AT LEAST 1 int (in this case)

This means that passing a pointer to a buffer less than sizeof(int)
is illegal. Of course you can pass a non null pointer to a wrong
buffer but the interface requirements mean that the pointer points
to a buffer AT LEAST bigger than sizeof(int).

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Nov 5 '07 #13
On Mon, 05 Nov 2007 19:54:40 +0100, jacob navia wrote:
Harald van Dijk wrote:
>On Sun, 04 Nov 2007 20:12:30 +0100, jacob navia wrote:
>>Harald van Dijk wrote:
On Sun, 04 Nov 2007 18:53:49 +0100, jacob navia wrote:
You can assert a parameter not being null in standard C by
>
int function(int MyTable[static 1]);
Almost, but not quite.

This means that the parameter MyTable has at least one element, i.e.
it can't be NULL.
It means you can't call function with the result of malloc(1), while
occasional ly (okay, rarely) that's actually useful.
Can you explain what you say?

I didn't understand what you are saying.

Assuming sizeof(int) 1, and some free memory, malloc(1) is a non-null
pointer that doesn't point to at least one int. So if you pass that
pointer to a function defined as taking int[static 1], the behaviour is
undefined. If you could specify a function as needing a non-null
pointer, it would (presumably) be allowed, since it is a valid pointer
which is not a null pointer.

I think the "static 1" means that you have AT LEAST 1 int (in this case)
Exactly.
This means that passing a pointer to a buffer less than sizeof(int) is
illegal.
Exactly.
Of course you can pass a non null pointer to a wrong buffer but
the interface requirements mean that the pointer points to a buffer AT
LEAST bigger than sizeof(int).
Which means int[static 1] is a stronger requirement than what
int *nonnull would mean if nonnull existed as a keyword.
Nov 5 '07 #14
<sa*****@free.f rwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.com.. .
I am proposing to create two new keywords for C, 'notnull' and
'nullable'.
Their meaning is a sort of 'design by contract', ensuring some rules
are verified at compile-time. It should not require additional runtime-
checks to be implemented.
I don't see the point if there's no runtime checking. A compile-time check
could only flag cases where a pointer _might_ be null, but any pointer
_might_ be null if you don't know where it came from.

For this to be useful, you'd need to do the checks at runtime; that
basically boils down to the compiler inserting an assert() for the
programmer either before each assignment and either before each function
call or after each function entry. If I wanted that (and I almost always
do), I'd put the necessary assert()s in myself, or use some sort of if/else
logic for more friendly results.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Nov 6 '07 #15
[comp.lang.c] Eric Sosman <es*****@ieee-dot-org.invalidwrot e:
sa*****@free.fr wrote:
>I am proposing to create two new keywords for C, 'notnull' and
'nullable'.
Your proposal should describe why the proposed feature
would be useful, why it would make life better for programmers
and/or improve the quality of programs. If the information
is already there, it is presented too subtly for me to grasp.
<topicality level="dubious" >

I may be mistaken, but I suspect that OP's motivation for proposing
this change stems from the @NotNull and @Nullable annotations provided
by certain Java IDE's. Within the context of an IDE, the "nullable"
and "notnull" keywords could have significant value, as the IDE could
warn you prior to compilation that

void foo( notnull void *bar ) {
/* ... */
}

void baz() {
void *qux = NULL;
/* Oops, forgot to initialize pointer */
foo( qux );
}

probably represents an error. It seems to me that it would be
slightly less distasteful to the Committee to propose an annotation
mechanism similar to Java's, where an annotation (possibly in the Java
style) could be prepended to any variable or definition, where the
meaning of any such annotation would be implementation-defined.

That said, Java and C are generally (IMVHO) developed in
different styles - Java developers are much more likely to develop
using IDE's that can benefit from such annotations than C developers
(if indeed there are any IDE's for C of that level of sophistication) .
I don't see a lot of use in a language change which would
realistically only benefit a small subset of C programmers.

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gma il.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.or g | Google groups, due to rampant unchecked spam.
Nov 6 '07 #16
Christopher Benson-Manica wrote On 11/06/07 11:07,:
[comp.lang.c] Eric Sosman <es*****@ieee-dot-org.invalidwrot e:

>>sa*****@free. fr wrote:
>>>I am proposing to create two new keywords for C, 'notnull' and
'nullable' .

> Your proposal should describe why the proposed feature
would be useful, why it would make life better for programmers
and/or improve the quality of programs. If the information
is already there, it is presented too subtly for me to grasp.


<topicality level="dubious" >

I may be mistaken, but I suspect that OP's motivation for proposing
this change stems from the @NotNull and @Nullable annotations provided
by certain Java IDE's. Within the context of an IDE, the "nullable"
and "notnull" keywords could have significant value, as the IDE could
warn you prior to compilation that

void foo( notnull void *bar ) {
/* ... */
}

void baz() {
void *qux = NULL;
/* Oops, forgot to initialize pointer */
foo( qux );
}

probably represents an error. [...]
Maybe I'm just being dense today (or this year, or
this life), but I still don't get it. Drop the silly
initialization, and a good compiler will *already* warn
about the probable error -- and not just for pointers,
either.

As far as I can see, the only "use case" is for
variations on

char *p = malloc(strlen(s ) + 1);
/* no NULL check here */
strcpy (p, s);

This is a class of error I can't recall having made (I
make others instead), so I'm not especially attracted by
machinery that helps me solve a problem I don't have.

A way to make assertions about values might be a
useful thing, but I think it should be in a more general
framework than just NULL-or-not.

--
Er*********@sun .com
Nov 6 '07 #17
Christopher Benson-Manica said:

<snip>
void baz() {
void *qux = NULL;
/* Oops, forgot to initialize pointer */
Wrong. :-) qux has in fact been initialised.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 6 '07 #18
On Tue, 06 Nov 2007 12:15:22 -0500, in comp.lang.c , Eric Sosman
<Er*********@su n.comwrote:
>Drop the silly
initialization , and a good compiler will *already* warn
about the probable error -- and not just for pointers,
either.
Mind you, a compiler has no obligation to warn, and additionally
warnings can be ignored by the programmer / build engine, especially
when your code is packed with spurious warnings generated from your
implementation' s headers. OTOH the OP's suggestion seems to be
intended to generate a fatal error message?

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Nov 6 '07 #19
[comp.lang.c] Eric Sosman <Er*********@su n.comwrote:
Christopher Benson-Manica wrote On 11/06/07 11:07,:
>void baz() {
void *qux = NULL;
/* Oops, forgot to initialize pointer */
foo( qux );
}
Maybe I'm just being dense today (or this year, or
this life), but I still don't get it. Drop the silly
initialization, and a good compiler will *already* warn
about the probable error -- and not just for pointers,
either.
You're right - sorry. <ot>The Java IDE I use (IntelliJ IDEA)
suggests initializing variables in this fashion, a habit that has
merits and drawbacks, but in any case doesn't really fit the C
paradigm.</ot It wasn't the best example.
As far as I can see, the only "use case" is for
variations on
char *p = malloc(strlen(s ) + 1);
/* no NULL check here */
strcpy (p, s);
This is a class of error I can't recall having made (I
make others instead), so I'm not especially attracted by
machinery that helps me solve a problem I don't have.
malloc() might not be a good example, since its behavior is
standardized and well-known to all with access to a man page. OTOH
user functions (possibly written by other developers) don't enjoy that
luxury, and might profitably be "annotated" (to borrow a term) to
return "NULLable" or "not NULL".
A way to make assertions about values might be a
useful thing, but I think it should be in a more general
framework than just NULL-or-not.
I agree (as I alluded to in my last paragraph). It seems to me that
implementations would have to retain the right to ignore such
assertion suggestions (much as they retain the right to ignore the
register keyword), and in any case I don't believe that such additions
to C would provide the benefits that they do in other langusages.

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gma il.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.or g | Google groups, due to rampant unchecked spam.
Nov 7 '07 #20

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

Similar topics

19
2981
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $ Last-Modified: $Date: 2003/09/22 04:51:49 $ Author: Nicolas Fleury <nidoizo at gmail.com> Status: Draft Type: Standards Track
10
1832
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in the declaration, eg: int? age; I don't like that much, I think it would be much more consistent to use a new keyword, such as "nullable". But anyways...
8
4856
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked one of these syntax formats to work; // if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
3
2105
by: rubikzube* | last post by:
Hi. I'm trying to write a snippet of code to detect if a variable is a nullable<t> struct, and I can't figure out the right syntax or if such a thing is even possible... Below are the results that I got when I attempted to perform some simple tests. Nullable<int> i = 32; bool isNullableClass = i is Nullable; // false
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.