0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Arduino: malloc と free の使い方

Posted at

メモリーの確保と開放のサンプルです。

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
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?