ReadLnString not working (SAMBA mode)

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

ReadLnString not working (SAMBA mode)

Post 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
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: ReadLnString not working (SAMBA mode)

Post 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.
Thanks, and have a nice day,
nxtboy III

programnxt.com
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: ReadLnString not working (SAMBA mode)

Post 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?
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: ReadLnString not working (SAMBA mode)

Post 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."
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: ReadLnString not working (SAMBA mode)

Post 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. ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: ReadLnString not working (SAMBA mode)

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: ReadLnString not working (SAMBA mode)

Post 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");
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests