Hi,
I think i just found a problem while using arduino in Mac OS X.
Here is the code:
void setup() {
Serial.begin(9600);
}
void loop() {
char *buff = (char *)malloc(3 * sizeof(char));
buff[0] = 'a';
buff[1] = 'd';
buff[2] = 0;
Serial.print(buff);
free(buff);
}
The problem with this is that when I read the from the serial line it doesn't always outputs "adadadadad"... continuously, instead sometimes some strange characters appear, like "adad? adadadàadad"...
The same happens using this code
void setup() {
Serial.begin(9600);
}
void loop() {
char *buff = "ad";
Serial.print(buff);
}
But when i write something without using a char pointer explicitly, it works well, like in the code below:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("ciao");
}
I can't figure out where the problem could be... I can just suppose it's a problem with handling pointers from the Serial but I just did those test until now. At first I thought there could be a problem with allocation using malloc but as you can see from the second code i posted (that is not using malloc) i doesn't work as well.
When i plug the arduino in a linux box all the codes above work well though, meaning that the output is always "adadadadadadadadad"... without any strange character.
Any thoughts?