メモリーの確保と開放のサンプルです。
sketch_aug21a.ino
int count = 0;
void setup()
{
Serial.begin(19200);
delay(500);
Serial.println(F("setup *** end"));
}
void loop()
{
char *chx = (char *)malloc(256);
char aa[17] = "0123456789abcdef";
strcpy(chx,"Good Morning aaaa ");
for (int it=0; it<6; it++)
{
strcat(chx, aa);
}
Serial.println(chx);
Serial.println("strlen(chx) = " +String(strlen(chx)));
free(chx);
Serial.println ("Hello " + String(count));
delay(2000);
count++;
}
実行結果
setup *** end
Good Morning aaaa 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
strlen(chx) = 114
Hello 0
Good Morning aaaa 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
strlen(chx) = 114
Hello 1
Good Morning aaaa 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
strlen(chx) = 114
Hello 2
Good Morning aaaa 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
strlen(chx) = 114
Hello 3
Good Morning aaaa 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
strlen(chx) = 114
Hello 4