Problem
You want to ensure that a pointer argument to a function
is non-null.
Solution
int fn(double data[static 1]);
This means that the array (that is passed as a pointer)
must have at least 1 element, i.e. can't be NULL.
I wasn't aware of this till a discussion in comp.std.c
Jan 20 '07
15 3810
jacob navia <ja***@jacob.re mcomp.frwrites:
Serve Laurijssen a écrit :
"jacob navia" <ja***@jacob.re mcomp.frwrote in message
news:45******** *************** @news.orange.fr ...
>Problem
You want to ensure that a pointer argument to a function is non-null.
Solution
int fn(double data[static 1]);
This means that the array (that is passed as a pointer) must have at least 1 element, i.e. can't be NULL.
I wasn't aware of this till a discussion in comp.std.c
what do you mean with ensure? I tried this code and I could easily
call this function with NULL. I didnt get a warning or error at
all. And of course this only happens at compile time right where the
compiler can actually analyse that it is called with NULL.
What about this then?
fn (malloc(1000000 * sizeof(double)) ;
Seems to me we're stuck with if (p == NULL) for a while :)
This would provoke an assertion failure (if malloc returns NULL)
What assertion failure? Passing a null pointer to fn() invokes
undefined behavior; there is *no* required or implied check.
The compiler would automatically test for NULL *before* pushing the
arguments. In this case you would have an assertion failed NOT in the
fn function but in the calling function, where the error actually
is!
This is MUCH better than testing for NULL in 'fn'
A compiler *could* perform such a check, but if you want your code to
be portably robust, your only option is to do the check in your code.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"jacob navia" <ja***@jacob.re mcomp.frwrote in message
news:45******** **************@ news.orange.fr. ..
>>>This means that the array (that is passed as a pointer) must have at least 1 element, i.e. can't be NULL.
I wasn't aware of this till a discussion in comp.std.c
How's the above to be extrapolated for the general case?
Isn't simply comparing against NULL a simpler approach?
Obviously this is for the compiler. In a debug setting, a
compiler and replace
fn(s)
with
assert(s),fn(s) ;
given a prototype
char *fn(char str[static 1]);
I agree its somewhat nice that there is a way to express that a pointer may
not be NULL and that a compiler can insert some assert then, but you just
cant rely on that all compilers will do this so the progger will have to
check himself too.
If you're planning to build this into your compiler, may I suggest that if
the code contains a manual NULL-pointer check that you omit the assert? :)
jacob navia schrieb:
hagman a écrit :
jacob navia schrieb:
>Problem
You want to ensure that a pointer argument to a function is non-null.
Solution
int fn(double data[static 1]);
This means that the array (that is passed as a pointer) must have at least 1 element, i.e. can't be NULL.
I wasn't aware of this till a discussion in comp.std.c
In what way then will
x = fn(p);
fail if p happens to be NULL?
The compiler can automatically insert a test in a
debug setting. For instance given this prototype
int fn(char string[static 1]);
when seeing a call
fn(s);
can replace that with
assert(s);
fn(s);
at each call site!
Maybe, but then it's at least no compile-time test, but rather a
runtime test.
Production code that might die with an "assertion failed: s in line
...." (in its debug version)
is not really more stable than code that dies with a segfault.
Thus I might as well have used the simpler idiom
int fn(char* string) {
assert(string);
...
}
Also, this is much clearer to me as the "char string[static 1]" trick
seems to suggest
to the unalert reader that the size of the array is exactly 1.
hagman a écrit :
Maybe, but then it's at least no compile-time test, but rather a
runtime test.
Production code that might die with an "assertion failed: s in line
..." (in its debug version)
is not really more stable than code that dies with a segfault.
It would crash not in the called function but in the calling function.
Big difference!
jacob navia <ja***@jacob.re mcomp.frwrites:
hagman a écrit :
Maybe, but then it's at least no compile-time test, but rather a
runtime test.
Production code that might die with an "assertion failed: s in line
..." (in its debug version)
is not really more stable than code that dies with a segfault.
It would crash not in the called function but in the calling function.
Big difference!
jacob, you *do* understand that it's not required to crash at all,
right? Given:
int fn(double data[static 1]);
the compiler is not obliged to insert any check at all.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson a écrit :
jacob navia <ja***@jacob.re mcomp.frwrites:
>>hagman a écrit :
>>>Maybe, but then it's at least no compile-time test, but rather a runtime test. Production code that might die with an "assertion failed: s in line ..." (in its debug version) is not really more stable than code that dies with a segfault. It would crash not in the called function but in the calling function. Big difference!
jacob, you *do* understand that it's not required to crash at all,
right? Given:
int fn(double data[static 1]);
the compiler is not obliged to insert any check at all.
Of course not. But it CAN do it legally.
This is a great progress, at least for me. I will change all headers
where the standard has
size_t strlen(const char *);
to
size_t strlen(const char[static 1]);
and I will insert tests for NULL when calling those functions
automatically! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Lasse Edsvik |
last post by:
Hello
I have a mailserver that requires windows authentication for sending email.
could you guys show me an example of how to specify user and password in
CDO/CDONTS?
i cant configure it to send with no authentication.
TIA
|
by: Jim Cochrane |
last post by:
I just google-searched this group and could not find any references to
this. I'm trying to figure out how to specify a three-part header with
html. For example,
left part center part right part
For old troffists, this is what the .tl construct does. But how would it
be done in html? I tried <div align = left>left part</div>
<div align = center> ..., which almost worked, except each part is on...
|
by: Tony Marston |
last post by:
The code
<a href="..." target="_blank">...</a>
will not validate as XHTML STRICT because of the 'target' tag, so how do I
achieve the same result by moving it to a CSS file? I cannot find anything
which allows me to specify 'target=' on an anchor tag.
--
Tony Marston
http://www.tonymarston.net
|
by: Alpine7 |
last post by:
How can I order the results of my query in non-linear fasion. I have a
field with these values: Reg S, 144A, US and want to order my results
by US, 144A, Reg S.
I would prefer not to create another field in the table if possible.
|
by: Andrew Biagioni |
last post by:
Hi all!
I'm trying to write a general-purpose trigger that determines what fields have
changed during an UPDATE, but I'm running into a problem.
I'm trying to dynamically select the value from a field in "old" and "new" (the
old and new values for the changed row), but I can't figure out how to do so.
I tried something like,
| |
by: Tony |
last post by:
Yes, I need to specify a font type so that the characters
will be evenly spaced when I write to a tab delimited text
file. So how does one specify a font type to write/print
and which font is best for evenness?
|
by: Rajiv Das |
last post by:
..Net 2.0
---------
Assume I am on a non-domain network. The proxy asks me to authenticate
whenever I want to access the net.
I have an app, through which I wish to access web sites using HttpWebRequest.
Now in the config file snippet below:
<system.net>
<defaultProxy useDefaultCredentials="false">
|
by: sylvain |
last post by:
I create a deployment kit with VS and I want to specify a Web Site different
then the Default Web Site.
There are two Web Site on our Web Server and they are using the same Port
(two different network card).
Is there a way to specify the Web Site to deploy my web application ? By
default, It's seem to deploy on the Default Web site. I know I can specify
the Port but on my case It's the same... Is there a way to specify the Web
Site on...
|
by: =?Utf-8?B?Q0QuU21hbGxleQ==?= |
last post by:
Is there a command line switch available for the EventViewer which will allow
me to specify which directory to view the available logs from?
I will have multiple workstations writing to a mirrored set of really large
flash drives. The workstations will write to their specific standard .evt
files. I need to be able to start EventViewer and specify which
workstations's logs to go view at startup.
Is this possible w/o creating my own...
|
by: zz12 |
last post by:
Hello. Would anyone know if there is an easy and clean way of presenting
maybe a time control field in html in an .asp page that allows a user to
specify a certain time of the day? Currently using an <input
name="SpecifyTime" type="Text"field but was wondering if there was a
better way of doing this as to where users won't mistype in entering non
time related characters? Thanks in advance.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |