Jump to content


Global variables in NQC


9 replies to this topic

#1 Duq

Duq

    Posts: 376
    Joined: 04-September 09
    Member: 7300
    Country: Ireland

Posted 19 August 2012 - 01:04 AM

I couldn't find a topic on this subject (although search is not easy with a search engine not allowing 3-letter words like 'NQC'...)
Anyway, after dusting off the RCX I'm giving NQC a go and I seem to have a problem with a global variable. I can init a global variable and I can set its value in task main. However when I start a second task from main and I try to change the value of my global var in the second task then nothing happens...
So what I'm doing is this:

int x;

task main () {
  x = 10;
  start task_2;
}

task task_2() {
  x = 20;
}

I would expect x to be 20 after running this but it's 10.
Any idea what I'm doing wrong?
I've got CDO. It's like OCD, but with the letters in alphabetical order. As they should be!

My pictures: Flickr!   My Lug: brick.ie.   My store: STUD

#2 Saberwing40k

Saberwing40k

  • Technic catch of the day


    Posts: 350
    Joined: 06-December 11
    Member: 22823
    Country: Luna

Posted 19 August 2012 - 02:01 AM

I have no idea what your problem would be, but you could try searching "Not Quite C", or "Global Variables"
Posted Image
Rumble is Red,Frenzy is Blue,
On Decepticon Cypertron,
Poem writes you!


My current project: ETF HTX-380, on Brickshelf, and Eurobricks.

#3 Ultimatemau

Ultimatemau

    Posts: 7
    Joined: 19-August 12
    Member: 30306
    Country: The Netherlands

Posted 19 August 2012 - 10:47 AM

View PostDuq, on 19 August 2012 - 01:04 AM, said:

I couldn't find a topic on this subject (although search is not easy with a search engine not allowing 3-letter words like 'NQC'...)
Anyway, after dusting off the RCX I'm giving NQC a go and I seem to have a problem with a global variable. I can init a global variable and I can set its value in task main. However when I start a second task from main and I try to change the value of my global var in the second task then nothing happens...
So what I'm doing is this:

int x;

task main () {
  x = 10;
  start task_2;
}

task task_2() {
  x = 20;
}

I would expect x to be 20 after running this but it's 10.
Any idea what I'm doing wrong?

int x is declared globally so in task task_2() you are overwriting x from x = 10 to x = 20. We would expect to get 20 in this case. What's the programming language you are using? Can't you return a value from that function like int task_2() { return 20; }?

Edited by Ultimatemau, 19 August 2012 - 10:49 AM.


#4 Duq

Duq

    Posts: 376
    Joined: 04-September 09
    Member: 7300
    Country: Ireland

Posted 20 August 2012 - 01:14 PM

View PostUltimatemau, on 19 August 2012 - 10:47 AM, said:

int x is declared globally so in task task_2() you are overwriting x from x = 10 to x = 20. We would expect to get 20 in this case. What's the programming language you are using? Can't you return a value from that function like int task_2() { return 20; }?
That's exactly the problem; I would expect the value to be changed to 20 but that doesn't happen.
I'm writing this in NQC in Bricx Command Center 3.3.
I've changed the program to do things a different way but I'd still like to know if this is a known issue or whether I'm doing something wrong...
I've got CDO. It's like OCD, but with the letters in alphabetical order. As they should be!

My pictures: Flickr!   My Lug: brick.ie.   My store: STUD

#5 Jan

Jan

    Posts: 124
    Joined: 18-May 10
    Member: 10754

Posted 20 August 2012 - 02:14 PM

View PostDuq, on 20 August 2012 - 01:14 PM, said:

That's exactly the problem; I would expect the value to be changed to 20 but that doesn't happen.
I'm writing this in NQC in Bricx Command Center 3.3.
I've changed the program to do things a different way but I'd still like to know if this is a known issue or whether I'm doing something wrong...

I have no experience with this program whatsoever, but could it be that the language is very sensitive,
and that using the word "task" (task_2) to declare the second function gives the problem.

#6 chorlton

chorlton

    Posts: 114
    Joined: 21-February 11
    Member: 16169
    Country: Scotland

Posted 20 August 2012 - 02:57 PM

It all looks OK. I don't have an RCX block to test and have loaned out my NXT.

Cheeky question, but is the code you've posted exactly the same as what you're trying to achieve? If you've simplified the example for posting then could there be a typo in your original?

The NQC Progrmaming Guide also mentions that there's a limit of 32 global variables, but doesn't say what the behaviour if exceeded is. Could you be hitting anything like that?
Part-timer

#7 Duq

Duq

    Posts: 376
    Joined: 04-September 09
    Member: 7300
    Country: Ireland

Posted 20 August 2012 - 04:16 PM

View Postchorlton, on 20 August 2012 - 02:57 PM, said:

It all looks OK. I don't have an RCX block to test and have loaned out my NXT.

Cheeky question, but is the code you've posted exactly the same as what you're trying to achieve? If you've simplified the example for posting then could there be a typo in your original?

The NQC Progrmaming Guide also mentions that there's a limit of 32 global variables, but doesn't say what the behaviour if exceeded is. Could you be hitting anything like that?
Thanks for the replies.
Nothing wrong with that cheeky question. I have indeed simplified the code but I've checked the original code for typos (long live Notepad++). It's a small program that only uses a handful of variables so I'm nowhere near the limit of 32.
I've got CDO. It's like OCD, but with the letters in alphabetical order. As they should be!

My pictures: Flickr!   My Lug: brick.ie.   My store: STUD

#8 Toastie

Toastie

  • Good Spirited


    Posts: 294
    Joined: 27-July 10
    Member: 12260
    Country: Germany

Posted 22 August 2012 - 08:50 PM

View PostDuq, on 20 August 2012 - 04:16 PM, said:

Thanks for the replies.
Nothing wrong with that cheeky question. I have indeed simplified the code but I've checked the original code for typos (long live Notepad++). It's a small program that only uses a handful of variables so I'm nowhere near the limit of 32.

Sorry for bumping this up:

Duq,

has this been resolved? I pulled out one RCX1.0, one RCX2.0, and a SCOUT PBrick (The RCX' are running the same firmware, firm0328.lgo. That is to my knowledge the last one TLC has released - the SCOUT just came with one firmware).

All PBricks do show the result "20" after running the program. Once the start task(2) is commented, "10" is indeed the result. I used the "Watching the brick" feature.

I have initialized the variable x ("int x=0;") - does that make any difference? Could timing be an issue? Sometimes multi tasking behaves a little weird on these bricks. A delay of 1 ms before starting task 2 may be worth a try.

Anyways, on my 3 PBricks your code runs fine.

Regards,
Thorsten

#9 Duq

Duq

    Posts: 376
    Joined: 04-September 09
    Member: 7300
    Country: Ireland

Posted 22 August 2012 - 10:58 PM

Thanks for testing that Thorsten. I'm planning to have another look at this next week. I'll post my updates here.
I've got CDO. It's like OCD, but with the letters in alphabetical order. As they should be!

My pictures: Flickr!   My Lug: brick.ie.   My store: STUD

#10 Aqualize

Aqualize

    Posts: 83
    Joined: 31-December 10
    Member: 15038
    Country: Sweden

Posted 23 August 2012 - 01:04 AM

I've never programmed using NQC but I had a look at the guide
http://bricxcc.sourc...c/NQC_Guide.pdf

Tasks aren't very well explained. But tasks are some form of multi-tasking. Perhaps you have some race condition here. How did you check that x was 10 or 20?

int x;

task main () {
x = 10;
start task_2;
// x Check here?
}

task task_2() {
x = 20;
}

If you checked where I put the comment (which seams logical as it is the main task. So if the check was placed there that code could very well be executed before task_2 had begun. If you want some linear flow you should use functions instead of tasks. If a function you call doesn't start any tasks you should have the contract that what happens in the function happens before the return of the execution to the caller (and what it continues to do).

So you can probably write it as
int x;

task main () {
x = 10;
myFunction();
// x will be 20 here
}

void myFunction() {
x = 20;
}


But as I said, I never (I think, perhaps I have in real time system course) programmed in NQC. Only using my programming skills to reason about the problem at hand.



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users