Page 1 of 1

ReadLnString not working (SAMBA mode)

Posted: 13 Apr 2011, 22:05
by ricardocrl
Hey all,

Can anyone confirm if ReadLnString() NXC function is working under my conditions: 1.031 firmware, brcxcc 3.3.8.10 and compiler 1.2.1.r4 ?

In my case, it enters in SAMBA mode (the clicking mode, right?)

Cheers,
Ricardo

Re: ReadLnString not working (SAMBA mode)

Posted: 13 Apr 2011, 22:39
by nxtboyiii
If you don't have the enhanced 1.31 firmware, download it. It will make your life easier. That might be the reason why it's not working.

Re: ReadLnString not working (SAMBA mode)

Posted: 14 Apr 2011, 02:20
by ricardocrl
OK, I got the problem! It's nothing about ReadLnString(). Sorry...

I was using strcat() with a destination string with no content. I wonder if this shouldn't be possible. In C it works well, as far as the first char of a string is a '\0'. I know NXC works a bit differently, but I think this shouldn't crash:

Code: Select all

string msg;
strcat(msg, "Hello"); //nothing in "msg" in the first use of strcat()
Is this failing by purpose?

Re: ReadLnString not working (SAMBA mode)

Posted: 14 Apr 2011, 02:28
by muntoo
ricardocrl wrote:

Code: Select all

string msg;
strcat(msg, "Hello"); //nothing in "msg" in the first use of strcat()
Is this failing by purpose?
You should initialize msg to "".
In C, if you do:

Code: Select all

typedef char *string;
string msg;
msg = "Hello";
That's a bad idea, because the memory the pointer points to was never allocated (and definitely didn't have the size to hold "Hello"). It may work for a while, if you're unlucky. (You're lucky if it crashes, so you can find the bug.)

How does this relate to NXC? Well, my guess is, it tries to access the memory that msg points to (within the VM), and then waits until the value of the string is returned. It isn't since it can't access itself, and I'm obviously not making any sense. :)

-----

Short answer:
Patient: "Doctor, it hurts when I whack myself on the head with a very large value of pie."
Doctor: "Don't do that. Unless it's a scientific experiment."

Re: ReadLnString not working (SAMBA mode)

Posted: 14 Apr 2011, 16:42
by ricardocrl
Well, maybe I don't make much sense either, but, once we don't specify the size of a string in NXC, every time we use strcat(), it will reallocate the space for a bigger string. If by default, a declared string is empty, I think it should reallocate space in the first use of the function.

Of course, when I mentioned the situation in C, I'm referring to an array of chars with the first char being null '\0', not just a pointer. ;)

Re: ReadLnString not working (SAMBA mode)

Posted: 14 Apr 2011, 17:23
by afanofosc
It is not in SAMBA mode, even though it is clicking. The firmware VM crashes and the brick clicks but all you need to do is pull a battery and put it back in and the brick reboots without requiring a firmware re-flash. If it was in SAMBA mode then the only way to recover would be to download a new firmware image to the brick.

It looks like a firmware bug since the code should not crash the VM. Can you try this with the standard firmware to check whether I broke something in the enhanced firmware?

I just tried the standard firmware myself and it also crashes when this code executes. With string msg = ""; rather than string msg; neither firmwares crash. The difference is that the initialized string actually contains one element (the null terminator) while the uninitialized string is completely empty. This appears to kill the strcat opcode. I will add some code to the compiler so that all string variables are actually initialized with a null even if no explicit initializer is provided.

John

Re: ReadLnString not working (SAMBA mode)

Posted: 14 Apr 2011, 17:40
by ricardocrl
afanofosc wrote:It is not in SAMBA mode, even though it is clicking. The firmware VM crashes and the brick clicks but all you need to do is pull a battery and put it back in and the brick reboots without requiring a firmware re-flash. If it was in SAMBA mode then the only way to recover would be to download a new firmware image to the brick.
John
Ah, thanks for the clarification. I had this doubt for a long time. ;-)

I tried now with the standard firmware 1.29 and it crashes as well.

Code: Select all

task main(){
  string msg;
  
  strcat(msg, "Hello");
  
  TextOut(0, LCD_LINE1, msg);
  Wait(2000);
}
Expectedly, StrCat() doesn't work as well, like this:

Code: Select all

msg = StrCat(msg, "Hello");