Skip to content

Commit ac069ae

Browse files
committed
Add project StringInterning
0 parents  commit ac069ae

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/tools
2+
/libraries
3+
/hardware

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# C++ for Arduino - cpp4arduino.com
2+
3+
This repository contains the code samples for the articles on [cpp4arduino.com](https://cpp4arduino.com/).
4+

StringInterning/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# String interning in action
2+
3+
This program displays the addresses of string so you can see the effect of string interning.
4+
5+
Read the article on [cpp4arduino.com](https://cpp4arduino.com/2018/10/23/what-is-string-interning.html)

StringInterning/StringInterning.ino

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// String interning in action
2+
//
3+
// This program displays the addresses of string so you can see the effect of
4+
// string interning
5+
//
6+
// cpp4arduino.com
7+
8+
const char *a1 = "hello";
9+
const char *a2 = "hello";
10+
11+
const char b1[] = "hello";
12+
const char b2[] = "hello";
13+
14+
const char c1[] PROGMEM = "hello";
15+
const char c2[] PROGMEM = "hello";
16+
17+
String d1 = "hello";
18+
String d2 = "hello";
19+
20+
void showAddresses(const char *title, const void *s1, const void *s2) {
21+
Serial.println(title);
22+
Serial.print(" s1 = ");
23+
Serial.println((intptr_t)s1);
24+
Serial.print(" s2 = ");
25+
Serial.println((intptr_t)s2);
26+
}
27+
28+
void setup() {
29+
Serial.begin(9600);
30+
while (!Serial)
31+
continue;
32+
33+
showAddresses("const char*", a1, a2);
34+
showAddresses("char[]", b1, b2);
35+
showAddresses("char[] PROGMEM", c1, c2);
36+
showAddresses("F()", F("hello"), F("hello"));
37+
showAddresses("String", d1.c_str(), d2.c_str());
38+
}
39+
40+
void loop() {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const char*
2+
s1 = 309
3+
s2 = 309
4+
char[]
5+
s1 = 268
6+
s2 = 262
7+
char[] PROGMEM
8+
s1 = 122
9+
s2 = 116
10+
F()
11+
s1 = 110
12+
s2 = 104
13+
String
14+
s1 = 544
15+
s2 = 552

StringInterning/results/ESP8266.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const char*
2+
s1 = 1073644664
3+
s2 = 1073644664
4+
char[]
5+
s1 = 1073644680
6+
s2 = 1073644672
7+
char[] PROGMEM
8+
s1 = 1076029988
9+
s2 = 1076029980
10+
F()
11+
s1 = 1076029972
12+
s2 = 1076029964
13+
String
14+
s1 = 1073676940
15+
s2 = 1073676964

0 commit comments

Comments
 (0)