Re: [Vala] vala-list Digest, Vol 133, Issue 6



Hi,

Thank you for your answer.  In fact, the_datum is a plain int.  So it cannot be NULL (error: Equality 
operation: `null'
and `int' are incompatible).

However, I found a workaround.  It seems that valac assumes out parameter to extern library functions are 
always
initialized, but not when they are called conditionally.

So, by changing a little the code, I got this to compile:

// Always return true
extern bool lib_initialized ();

// data is always set when work returns true
extern bool lib_work (out int data);

int main(string[] args) {
        int the_datum;
        if (lib_initialized ()) {
                bool has_datum = lib_work (out the_datum);
                if (has_datum) {
                        stdout.printf ("Hello, %d!\n", the_datum);
                }
        }
        return 0;
}

Le samedi 16 février 2019 à 12:52 -0200, Fernando Silva via vala-list a écrit :
I think checking if (the_datum != null) should do

Em sáb, 16 de fev de 2019 10:00, <vala-list-request gnome org escreveu:

Send vala-list mailing list submissions to
        vala-list gnome org

To subscribe or unsubscribe via the World Wide Web, visit
        https://mail.gnome.org/mailman/listinfo/vala-list
or, via email, send a message with subject or body 'help' to
        vala-list-request gnome org

You can reach the person managing the list at
        vala-list-owner gnome org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of vala-list digest..."


Today's Topics:

   1. error: use of possibly unassigned local variable (Vivien Kraus)


----------------------------------------------------------------------

Message: 1
Date: Fri, 15 Feb 2019 21:28:45 +0100
From: Vivien Kraus <vivien planete-kraus eu>
To: vala-list gnome org
Subject: [Vala] error: use of possibly unassigned local variable
Message-ID:
        <40a1141df0d06b59ed0f87f3a6cafac285f45b4a camel planete-kraus eu>
Content-Type: text/plain; charset="UTF-8"

Hello list,

I am facing an inconvenient problem.

Suppose I have a C library, that I want to use from vala:

// Always return true
extern bool lib_initialized ();

// data is always set when work returns true
extern bool lib_work (out int data);

int main(string[] args) {
        int the_datum;
        bool has_datum = (lib_initialized () && lib_work (out the_datum));
        if (has_datum) {
                stdout.printf ("Hello, %d!\n", the_datum);
        }
        return 0;
}

If I am to compile this (0.42.5, -C only, not link), I get an error
message:

test.vala:11.3-11.44: error: use of possibly unassigned local variable
`the_datum'
                stdout.printf ("Hello, %d!\n", the_datum);
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)

The workaround is obvious: initialize the_datum.  However, I like to run
my code under valgrind to detect when reading
from an uninitialized variable (I make lots of mistakes ^^), and so if
there is a bug in lib_work and I initialize
the_datum, then I will not be able to notice the problem.

Is it possible to instruct valac that it should not worry?  Or is it
possible to change this error to a simple warning?

Best regards

Vivien




------------------------------

Subject: Digest Footer

_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list


------------------------------

End of vala-list Digest, Vol 133, Issue 6
*****************************************

_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]