nullable types, null and(?) NULL?

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
aclassifier
Respected Member
Posts: 483
Joined: Wed Apr 25, 2012 8:52 pm
Contact:

nullable types, null and(?) NULL?

Post by aclassifier »

A nullable type may be called with an instance of a type or null that seems to be a reserved word [1]. I have compiled something to play with below.

My "problem" is that I have some large chunk of code where I have happily used NULL as ((void*)0) and it compiles and runs correctly, without the error message error: invalid initialization of reference for argument. I have not been able to recreate that situation in the code below, though.

USE_S defines if we actually use an object (=1). And if we don't (=0) then we can get the error with WORKS (=0) or OK with WORKS (=1).

Code: Select all

// #include  <xs1.h> // Not needed
#include <stdlib.h> // Defines NULL not correct to use here

void fr (int &?y) { // XMOS Programming Guide, XM004440A p47
    if (!isnull(y)) {
        // We know y is not null so can use it here
    }
}

void fa (int (&?a)[5]) { // XMOS Programming Guide, XM004440A p47
    if (!isnull(a)) {
        // We know a is not null so can use it here
    }
}

typedef struct s_t {
    int val;
} s_t;

#define USE_S 0
#define WORKS 1 // 0 or 1 valid if USE_S is 0

void fs (s_t &?s) {
    #if (USE_S==1)
        // We know s is not null so can use it here
    #else
       // s is always valid
    #endif
}

int main() {
    int y;
    fr (y);
    fr (null);

    int a[5];
    fa (a);
    fa (null);

    #if (USE_S==1)
        s_t s; // Only defined when needed
        #define MY_S s
    #else
        #undef  NULL
        #if (WORKS==1)
            #define MY_S null
        #else
            #define NULL ((void*)0) // Same as in "stdlib.h". Same problem:
            #define MY_S NULL // "error: invalid initialization of reference for argument"
        #endif
    #endif

    fs (MY_S); // s(ok), null(ok) or NULL(err)

    return 0;
}
[1] Reserved words - my list. Disclaimer


--
Øyvind Teig
Trondheim (Norway)
https://www.teigfam.net/oyvind/home/
User avatar
CousinItt
Respected Member
Posts: 360
Joined: Wed May 31, 2017 6:55 pm

Post by CousinItt »

null is a keyword and the name of a type; see Programming XC on XMOS devices.

The same document says
If an object contains a reference to null, it is invalid to reference the object except as the argument to a function taking a nullable parameter, as the operand of the isnull operator, or as the operand of the sizeof operator.
I don't see that null has (or should have) any relation to the standard C definition of NULL.
User avatar
aclassifier
Respected Member
Posts: 483
Joined: Wed Apr 25, 2012 8:52 pm
Contact:

Post by aclassifier »

CousinItt, thanks!
If an object contains a reference to null, it is invalid to reference the object except as the argument to a function taking a nullable parameter, as the operand of the isnull operator, or as the operand of the sizeof operator.
Which page in that document: Programming XC on XMOS Devices?
That document, on my machine only the Index is searchable. Not that I don't believe you! And then
I don't see that null has (or should have) any relation to the standard C definition of NULL.
Me neither. But if that ((void*)0)passes through by the compiler (which it certainly does in that case of mine, but not in the example code that I gave), then I guess or even infer that isnull(object) really only tests on zero so it would work?

Maybe I should file this as a Ticket?
--
Øyvind Teig
Trondheim (Norway)
https://www.teigfam.net/oyvind/home/
User avatar
CousinItt
Respected Member
Posts: 360
Joined: Wed May 31, 2017 6:55 pm

Post by CousinItt »

See A.7.7.3 Nullable Declarators in Programming XC, on page 88 in the pdf version I have.

The null keyword is in the list in A.1.4, and A.1.5.3 indicates null is a type.

By all means submit a ticket if you want to. Just don't hold your breath.
User avatar
aclassifier
Respected Member
Posts: 483
Joined: Wed Apr 25, 2012 8:52 pm
Contact:

Post by aclassifier »

Thanks! So the chapter is A.7.7.3 Nullable Declarators

I keep sending Tickets and they do handle them in due course, I guess, relative to the seriousness. I guess this one would be low to lower seriousness. But still it might be worth having removed, if it is as I infer.
--
Øyvind Teig
Trondheim (Norway)
https://www.teigfam.net/oyvind/home/
Post Reply