Log in

View Full Version : strcat error


byteit101
13-03-2009, 15:27
testing out some code, our auto looked similar to this:
int loop=0;
while (IsAuto())
{
char * name = strcat((char*)(++loop), " iteration");
printf(name);
}
and I run kernel task, downloads, open console, see nothing, on ds, auto enable, and the console prints out nothing and the ds then says no comms. the RSL was solid, but we had to restart. the strcat seems to be crashing the robot, is this possible? and why?

Jared Russell
13-03-2009, 15:35
I think you think that strcat does something that it doesn't. You can't just cast an int to a char * and expect it to work.

How about:


int loop=0;
while (IsAuto())
{
printf("%d iteration\n", ++loop);
}


That is the most direct way to accomplish what you want. Alternatively, here's how to do it with strcat() properly:


int loop=0;
while (IsAuto())
{
char msg[100]; // We can't just declare this as a "char *" without first allocating memory - 100 characters is more than enough
strcat( msg, itoa(++loops) );
strcat( msg, " iteration\n");
printf("%s", msg);
}


The standard C itoa() function takes an integer and returns a C string of the printable characters.

byteit101
13-03-2009, 18:15
ok, but why is it crashing the crio?

Dave Flowerday
13-03-2009, 19:02
ok, but why is it crashing the crio?

You're taking the loop number and treating it like a pointer , so the rio is trying to access memory at the address of 'loop', which is probably 0 or very low. Basically you're causing a segmentation fault, which probably causes your task on the rio to exit.

byteit101
13-03-2009, 21:38
ahhh! that would explain that! doesn't one love the power and the unsafeness of C/C++? (there's an easy virus: ((char*)0) = "CORRUPT THIS DATA!!!!"; :D )

virtuald
13-03-2009, 21:47
ahhh! that would explain that! doesn't one love the power and the unsafeness of C/C++? (there's an easy virus: ((char*)0) = "CORRUPT THIS DATA!!!!"; :D )

Actually, if you're running as a normal process on a modern operating system, that will only crash the individual program that did the bad access. Like in Windows when you see "X has encountered a problem and needs to close... ", many times the reason it needs to close is because of bad memory accesses. On a *nix box you'll see something on the screen saying 'segmentation fault'.

Now of course, if you're running in the kernel and make a bad memory access, then the system will generally crash, since theres nothing to protect you there. Incidentally, our programs are running as a kernel module on the cRio...

Jared Russell
13-03-2009, 22:37
ahhh! that would explain that! doesn't one love the power and the unsafeness of C/C++? (there's an easy virus: ((char*)0) = "CORRUPT THIS DATA!!!!"; :D )

Pointers are a great and terrible thing. To the point that I've worked on many projects at work where the use of non-smart pointers is strictly prohibited. Simply put, 90% of us aren't smart enough to use them effectively without shooting ourselves in the foot (and I am definitely a member of that majority!)