I've been told (specifically in an answer to C++ Standard Library on Arduino, and in Stack Overflow question C++ string and Arduino String. How to combine them?)) that the Arduino compiler does not implement the new
operator. However, I've written a program for the Arduino (in the Arduino IDE) which uses it, and it works perfectly.
void setup() {
Serial.begin(9600);
}
void loop() {
char* array;
char c;
unsigned arraySize;
Serial.write("Enter a 1 digit number.\n");
do {
c = Serial.read();
} while(c < '0' or c > '9');
arraySize = c-'0';
Serial.write("You wrote ");
Serial.write(c);
Serial.write(".\n");
Serial.write("Now enter ");
Serial.write(c);
Serial.write(" lower-case letters.\n");
array = new char[arraySize];
for (unsigned i = 0; i < arraySize;) {
array[i] = Serial.read();
if (array[i] >= 'a' and array[i] <= 'z')
i++;
}
Serial.write("You entered: ");
for (unsigned i = 0; i < arraySize; i++) {
Serial.write(array[i]);
Serial.write(" ");
}
Serial.write("\n");
}
Here is a sample output to demonstrate its functionality:
Enter a 1 digit number.
You wrote 5.
Now enter 5 lower-case letters.
You entered: h e l l o
Enter a 1 digit number.
You wrote 9.
Now enter 9 lower-case letters.
You entered: w a s s u p m a n
Enter a 1 digit number.
You wrote 9.
Now enter 9 lower-case letters.
You entered: h o w y a d o i n
Enter a 1 digit number.
You wrote 4.
Now enter 4 lower-case letters.
You entered: c o o l
Enter a 1 digit number.
You wrote 7.
Now enter 7 lower-case letters.
You entered: i t w o r k s
Enter a 1 digit number.
So why do I keep hearing this? Are these people wrong, or do I simply misunderstand their meaning?