View unanswered posts | View active topics It is currently Fri Aug 29, 2014 10:27 am






Reply to topic  [ 11 posts ] 
Problem with data in variables 
Author Message
Rookie

Joined: Mon Sep 24, 2012 8:32 am
Posts: 13
Post Problem with data in variables
Example:
Code:
task main()
{
int i;
int zid[10];
   for(i=0;i<10;i++){
      zid[i]=i;
      nxtDisplayCenteredTextLine(4, "ZID[%d]=%d",i,zid[i]);
    wait1Msec(500);
   }
}
}

After i start the program the "zid" var. get 10 value in array.
Result:
ZID[0]=0
ZID[1]=1
ZID[2]=2
ZID[3]=3
...
...

After that i stop the program and comment this row //zid[i]=i;
And start program again.
Code:
task main()
{
int i;
int zid[10];
   for(i=0;i<10;i++){
      //zid[i]=i;
      nxtDisplayCenteredTextLine(4, "ZID[%d]=%d",i,zid[i]);
    wait1Msec(500);
   }
}
}

And i get the value in the array????
Result:
ZID[0]=0
ZID[1]=1
ZID[2]=2
ZID[3]=3
....
...

How is this possible??


Tue Oct 23, 2012 5:08 am
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Problem with data in variables
It is possible because no where in the second program did you set the values in the array to anything other than what was already in it.
C does not automatically zero out an array for you.
Old robotC might have, and maybe it still should, in which case it is a bug, but C does not and it is probably a good habit to get used to always initializing data as you expect it and not depending on the compiler, unless it is very specific about such.

Take a look at memset(pToBuffer, nValue, nNumbOfBytes);

The description reads "This is a useful function for initializing the value of an array to all zeros."

_________________
Mike aka Spiked3
http://www.spiked3.com


Tue Oct 23, 2012 6:20 am
Profile
Rookie

Joined: Mon Sep 24, 2012 8:32 am
Posts: 13
Post Re: Problem with data in variables
Spiked3 thanks for answer.
I don't need zeros in my array.
The problem is that each time I re-start the program in its array have the same values ​​that I previously enrolled in the array. So it does not happen in a normal c.
Why are the values ​​in the array is not initialized by the type of the variable, regardless of whether they are zero or something else?
This looks to me more like a variable after stopping the program has not died.
Greetings.


Tue Oct 23, 2012 6:33 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Problem with data in variables
No, Mike is right, C does not automatically initialize the values in arrays (unless they're defined as global or static). When an array is declared, a block of memory is simply allocated, and the elements of the array will contain whatever was in that memory space before. If you have not zeroed out the array, it will contain the same values. I don't exactly how ROBOTC handles this either, but it is always good practice to initialize your arrays in C.

_________________
sudo rm -rf /


Tue Oct 23, 2012 8:26 am
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 602
Post Re: Problem with data in variables
ROBOTC will zero out global variables at program runtime, but will not clear out local/stack variables.

Since before 3.5, all variables were "scope limited globals", everything way typically zero'd out. This is no longer the case.

I would suggest that you use memset if you wish to zero out your array without having to manually specify an initialization value.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Tue Oct 23, 2012 9:45 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Problem with data in variables
Also, just want to eliminate the obvious... Did you recompile and download the code after you change it? You didn't say in your original post.


Tue Oct 23, 2012 11:49 am
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Problem with data in variables
zerroTC wrote:
Spiked3 thanks for answer.
I don't need zeros in my array.
The problem is that each time I re-start the program in its array have the same values ​​that I previously enrolled in the array. So it does not happen in a normal c.
Why are the values ​​in the array is not initialized by the type of the variable, regardless of whether they are zero or something else?
This looks to me more like a variable after stopping the program has not died.
Greetings.


A programming language could act the way you describe, and many do, however C being a very old language that was one small step above assembler language, usually does not, and as just explained, RobotC does not. There is no death to a variable, like there would be in C# or Java, and actually even in those languages the variables are cleared when the memory they use is allocated to something new, not when the program ends (and this creates a huge security risk that is often exploited on other computers). I would not even depend on recompiling and downloading to behave the same way each time as I have seen cases where it did not.

Just as a side funny; we used to power off and on computers real quick and download a program to graphically display what was in memory. It made for some neat, often repeatable patterns - the good old days.

_________________
Mike aka Spiked3
http://www.spiked3.com


Tue Oct 23, 2012 12:44 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Problem with data in variables
I have not been writing compilers for a long time, but as I recall, initialization of variables is implementation specific that you should not count on one way or the other. Having said that, the array variable in your main is considered local variable. Local variables are allocated on the stack (at least on most platforms and I believe in most recent RobotC too). Stack variables are not initialized unless you did it explicitly in the code. So what you saw was expected.


Tue Oct 23, 2012 12:54 pm
Profile
Rookie

Joined: Mon Sep 24, 2012 8:32 am
Posts: 13
Post Re: Problem with data in variables
Quote:
Also, just want to eliminate the obvious... Did you recompile and download the code after you change it? You didn't say in your original post.

Thanks for the question, yes I did compile and download the program


Thu Oct 25, 2012 7:30 am
Profile
Rookie

Joined: Mon Sep 24, 2012 8:32 am
Posts: 13
Post Re: Problem with data in variables
MHTS wrote:
I have not been writing compilers for a long time, but as I recall, initialization of variables is implementation specific that you should not count on one way or the other. Having said that, the array variable in your main is considered local variable. Local variables are allocated on the stack (at least on most platforms and I believe in most recent RobotC too). Stack variables are not initialized unless you did it explicitly in the code. So what you saw was expected.

No matter what I do agree with all the above.
I think you did not understand me, and if so, please disregard this message.
If you write the following code:
Code:
task main ()
{
int i;
int a = 100;
}

Compile and download the program to the NXT. After the program start, program stop. Okay?
After that restate the above code in the following code:
Code:
task main ()
{
int i;
nxtDisplayCenteredTextLine (2, "Var. 'and' = & d", i);
wait1Msec (10000);
}

And now again:
Compile and download the program to the NXT. After the program start, program stop after 10 seconds. (To see the screen).
I see on my screen next thing.
'I' = 100
My question is: WHY 100.
And this is not strange to you?

If I have the following C program:
Code:
# include <stdio.h>
int main (void) {
int i;
int a = 100;
}

Run the program. After that I rewrite:
Code:
# include <stdio.h>
int main (void) {
int i;
printf ("Var. 'and' = & d", i);
}

I see on my screen next thing.
'I' = -858993460

This is not strange for me, I do not know about you?
I hope I managed to describe what's in this story weird.

Greetings. :bigthumb:


Thu Oct 25, 2012 8:03 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Problem with data in variables
zerroTC wrote:
If you write the following code:
Code:
task main ()
{
int i;
int a = 100;
}

Compile and download the program to the NXT. After the program start, program stop. Okay?
After that restate the above code in the following code:
Code:
task main ()
{
int i;
nxtDisplayCenteredTextLine (2, "Var. 'and' = & d", i);
wait1Msec (10000);
}

And now again:
Compile and download the program to the NXT. After the program start, program stop after 10 seconds. (To see the screen).
I see on my screen next thing.
'I' = 100
My question is: WHY 100.
And this is not strange to you?

No, it is not strange to me at all. When you compile and download a new program to the NXT, it does not zero all the RAM memory in the NXT. So whatever the previous program left behind is still intact. Let's say when you loaded and ran the first program, RobotC assigned variable 'i' to memory location 1000 and variable 'a' to memory location 1004. Since you initialized variable 'a' to 100, memory location 1004 contained the value 100. Now when you compiled and downloaded the second program to the NXT, RobotC may assign the variable 'i' to location 1004. Since memory location 1004 did not get erased and it had the value 100 in it from before, now you printed the value 100. The point is, when a new program is loaded, you cannot count on where the location of a certain variable is at even though it has the same name as the previous program. So any uninitialized local variables will have "unpredictable values" in them.
zerroTC wrote:
If I have the following C program:
Code:
# include <stdio.h>
int main (void) {
int i;
int a = 100;
}

Run the program. After that I rewrite:
Code:
# include <stdio.h>
int main (void) {
int i;
printf ("Var. 'and' = & d", i);
}

I see on my screen next thing.
'I' = -858993460

This is not strange for me, I do not know about you?
I hope I managed to describe what's in this story weird.

Greetings. :bigthumb:

I assume you ran the above C program on a Windows PC. If so, Windows PC is quite different from NXT. I don't think RobotC supports virtual memory, but Windows does. So every time you run a new program, Windows allocates different physical memory pages for your program. So even if both variable 'a' in program 1 and variable 'i' in program 2 are assigned the same virtual address (e.g. 1004), they are still on different physical memory pages. So, you will truly see garbage. I am not going too deep into it because it involves how the memory manager in Windows manages physical memory and virtual memory space. There is no way for one Windows program to leave something behind in memory for the next Windows program to retrieve. If it had been possible, it would have been a huge security hole. Imagine a virus can scan through memory and find what the previous program has left behind. There may be a password in it. In fact, Windows runs programs in their own virtual memory space. So one program can never see the memory of the other program, period. But for RobotC in NXT, security is not a concern so there is no reason to zero memory between loading different programs. I hope it doesn't confuse you more :)


Last edited by MHTS on Thu Oct 25, 2012 5:49 pm, edited 1 time in total.



Thu Oct 25, 2012 5:16 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.