473,789 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to specify a non-null pointer argument in C

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 #1
15 3808
jacob navia wrote:
Problem

You want to ensure that a pointer argument to a function
is non-null.
Solution

Compare the pointer to a null pointer, and don't call the function if
they compare equal.

Problem

You want to assume that a pointer function parameter 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.
This ensures nothing, but allows certain otherwise invalid
optimisations, which may well be what you wanted. A minor point,
though:

#include <stdlib.h>

void f(double data[static 1]) {}
void g(double *data) {
if(data != NULL)
f(data);
}

int main(void) {
double x;
g(&x + 1);

double *p = malloc(1);
g(p);
free(p);
}

Both potential calls to f would be valid if "static 1" only meant
"non-null" and nothing more.
I wasn't aware of this till a discussion in comp.std.c
Jan 20 '07 #2

jacob navia wrote:
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
How's the above to be extrapolated for the general case?

Isn't simply comparing against NULL a simpler approach?

Jan 20 '07 #3

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?

Jan 20 '07 #4
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!
Jan 20 '07 #5
santosh a écrit :
jacob navia wrote:
>>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


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]);
Jan 20 '07 #6
Harald van Dijk a écrit :
jacob navia wrote:
>>Problem

You want to ensure that a pointer argument to a function
is non-null.


Solution

Compare the pointer to a null pointer, and don't call the function if
they compare equal.

Problem

You want to assume that a pointer function parameter 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.


This ensures nothing, but allows certain otherwise invalid
optimisations, which may well be what you wanted. A minor point,
though:

#include <stdlib.h>

void f(double data[static 1]) {}
void g(double *data) {
if(data != NULL)
f(data);
}

int main(void) {
double x;
g(&x + 1);

double *p = malloc(1);
g(p);
free(p);
}

Both potential calls to f would be valid if "static 1" only meant
"non-null" and nothing more.

>>I wasn't aware of this till a discussion in comp.std.c

well, you are right. That bug is not catched.
Jan 20 '07 #7

"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 :)
Jan 20 '07 #8
"hagman" <go****@von-eitzen.dewrites :
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?
By invoking undefined behavior.

C99 6.7.5.3p7:

If the keyword static also appears within the [ and ] of the array
type derivation, then for each call to the function, the value of
the corresponding actual argument shall provide access to the
first element of an array with at least as many elements as
specified by the size expression.

This "shall" appears outside a constraint, so if it's violated the
result is undefined behavior.

You might as well just (attempt to) dereference the pointer inside the
function; that invokes UB in exactly the same circumstances. And in
either case, a compiler may, but is not required to, insert a check
(and to decide what happens if the check fails).

--
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.
Jan 20 '07 #9
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)

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'
Jan 20 '07 #10

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

Similar topics

11
4087
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
12
3266
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...
6
77511
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
4
1684
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.
1
1508
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,
2
4496
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?
0
1211
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">
0
1074
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...
3
2604
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...
2
1590
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.
0
9511
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
10410
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
9984
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
9020
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
7529
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
6769
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();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
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
2
3701
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.