Skip to content

Commit 4b035e6

Browse files
author
Owen L - SFE
committed
use 'sizeof()' consistently
1 parent b13b4fc commit 4b035e6

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

libraries/EEPROM/examples/Example2_AllFunctions/Example2_AllFunctions.ino

+26-26
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ void setup()
5050
Serial.println("8 bit tests");
5151
byte myValue1 = 200;
5252
byte myValue2 = 23;
53-
randomLocation = random(0, EEPROM.length());
53+
randomLocation = random(0, EEPROM.length() - sizeof(myValue1));
5454

5555
startTime = millis();
5656
EEPROM.write(randomLocation, myValue1); //(location, data)
5757
endTime = millis();
58-
EEPROM.put(randomLocation + 1, myValue2);
58+
EEPROM.put(randomLocation + sizeof(myValue1), myValue2);
5959

6060
Serial.printf("Write byte time: %dms\n", endTime - startTime);
6161

6262
startTime = millis();
6363
EEPROM.write(randomLocation, myValue1); //(location, data)
6464
endTime = millis();
6565

66-
Serial.printf("Write identical byte to same location (should be ~1): %dms\n", endTime - startTime);
66+
Serial.printf("Write identical byte to same location: %dms\n", endTime - startTime);
6767

6868
byte response1 = EEPROM.read(randomLocation);
69-
byte response2 = EEPROM.read(randomLocation + 1);
69+
byte response2 = EEPROM.read(randomLocation + sizeof(myValue1));
7070
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue1, response1);
71-
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + 1, myValue2, response2);
71+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue1), myValue2, response2);
7272
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
7373

7474
Serial.println("");
@@ -78,17 +78,17 @@ void setup()
7878
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
7979
uint16_t myValue3 = 3411;
8080
int16_t myValue4 = -366;
81-
randomLocation = random(0, EEPROM.length());
81+
randomLocation = random(0, EEPROM.length() - sizeof(myValue3));
8282

8383
EEPROM.put(randomLocation, myValue3);
84-
EEPROM.put(randomLocation + 2, myValue4);
84+
EEPROM.put(randomLocation + sizeof(myValue3), myValue4);
8585

8686
uint16_t response3;
8787
int16_t response4;
8888
EEPROM.get(randomLocation, response3);
89-
EEPROM.get(randomLocation + 2, response4);
89+
EEPROM.get(randomLocation + sizeof(myValue3), response4);
9090
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue3, response3);
91-
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + 2, myValue4, response4);
91+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue3), myValue4, response4);
9292
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
9393

9494
Serial.println("");
@@ -99,52 +99,52 @@ void setup()
9999
Serial.printf("Size of int: %d\n", sizeof(int));
100100
int myValue5 = -245000;
101101
unsigned int myValue6 = 400123;
102-
randomLocation = random(0, EEPROM.length());
102+
randomLocation = random(0, EEPROM.length() - sizeof(myValue5));
103103

104104
EEPROM.put(randomLocation, myValue5);
105-
EEPROM.put(randomLocation + 4, myValue6);
105+
EEPROM.put(randomLocation + sizeof(myValue5), myValue6);
106106

107107
int response5;
108108
unsigned int response6;
109109
EEPROM.get(randomLocation, response5);
110-
EEPROM.get(randomLocation + 4, response6);
110+
EEPROM.get(randomLocation + sizeof(myValue5), response6);
111111
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue5, response5);
112-
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + 4, myValue6, response6);
112+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue5), myValue6, response6);
113113
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
114114

115115
//int32_t and uint32_t sequential test
116116
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
117117
int32_t myValue7 = -341002;
118118
uint32_t myValue8 = 241544;
119-
randomLocation = random(0, EEPROM.length());
119+
randomLocation = random(0, EEPROM.length() - sizeof(myValue7));
120120

121121
EEPROM.put(randomLocation, myValue7);
122-
EEPROM.put(randomLocation + 4, myValue8);
122+
EEPROM.put(randomLocation + sizeof(myValue7), myValue8);
123123

124124
int32_t response7;
125125
uint32_t response8;
126126
EEPROM.get(randomLocation, response7);
127-
EEPROM.get(randomLocation + 4, response8);
127+
EEPROM.get(randomLocation + sizeof(myValue7), response8);
128128
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue7, response7);
129-
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + 4, myValue8, response8);
129+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue7), myValue8, response8);
130130
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
131131

132132
//float (32) sequential test
133133
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
134134
Serial.printf("Size of float: %d\n", sizeof(float));
135135
float myValue9 = -7.35;
136136
float myValue10 = 5.22;
137-
randomLocation = random(0, EEPROM.length());
137+
randomLocation = random(0, EEPROM.length() - sizeof(myValue9));
138138

139139
EEPROM.put(randomLocation, myValue9);
140-
EEPROM.put(randomLocation + 4, myValue10);
140+
EEPROM.put(randomLocation + sizeof(myValue9), myValue10);
141141

142142
float response9;
143143
float response10;
144144
EEPROM.get(randomLocation, response9);
145-
EEPROM.get(randomLocation + 4, response10);
145+
EEPROM.get(randomLocation + sizeof(myValue9), response10);
146146
Serial.printf("Location %d should be %f: %f\n\r", randomLocation, myValue9, response9);
147-
Serial.printf("Location %d should be %f: %f\n\r", randomLocation + 4, myValue10, response10);
147+
Serial.printf("Location %d should be %f: %f\n\r", randomLocation + sizeof(myValue9), myValue10, response10);
148148
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
149149

150150
Serial.println("");
@@ -157,24 +157,24 @@ void setup()
157157
double myValue12 = 384.95734987;
158158
double myValue13 = 917.14159;
159159
double myValue14 = 254.8877;
160-
randomLocation = random(0, EEPROM.length());
160+
randomLocation = random(0, EEPROM.length() - sizeof(myValue11));
161161

162162
startTime = millis();
163163
EEPROM.put(randomLocation, myValue11);
164164
endTime = millis();
165165
Serial.printf("Time to record 64-bits: %dms\n", endTime - startTime);
166166

167-
EEPROM.put(randomLocation + 8, myValue12);
167+
EEPROM.put(randomLocation + sizeof(myValue11), myValue12);
168168
EEPROM.put(EEPROM.length() - sizeof(myValue13), myValue13); //Test end of EEPROM space
169169

170170
double response11;
171171
double response12;
172172
double response13;
173173
EEPROM.get(randomLocation, response11);
174-
EEPROM.get(randomLocation + 8, response12);
174+
EEPROM.get(randomLocation + sizeof(myValue11), response12);
175175
EEPROM.get(EEPROM.length() - sizeof(myValue13), response13);
176176
Serial.printf("Location %d should be %lf: %lf\n", randomLocation, myValue11, response11);
177-
Serial.printf("Location %d should be %lf: %lf\n", randomLocation + 8, myValue12, response12);
177+
Serial.printf("Location %d should be %lf: %lf\n", randomLocation + sizeof(myValue11), myValue12, response12);
178178
Serial.printf("Edge of EEPROM %d should be %lf: %lf\n", EEPROM.length() - sizeof(myValue13), myValue13, response13);
179179

180180
double response14;
@@ -189,7 +189,7 @@ void setup()
189189
//String write test
190190
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
191191
char myString[19] = "How are you today?";
192-
randomLocation = random(0, EEPROM.length());
192+
randomLocation = random(0, EEPROM.length() - sizeof(myString));
193193
EEPROM.put(randomLocation, myString);
194194

195195
char readMy[19];

0 commit comments

Comments
 (0)