Skip to content

Commit ef65474

Browse files
authored
Remove other issues in off-by-one code (#187)
* Update snipMemory6.cpp * Fix double declaration for len * Update snipMemory6.cpp * Update snipMemory5.cpp
1 parent e21e043 commit ef65474

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

codereview101/snipMemory5.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11

2-
int len = 0, total = 0;
2+
int len = 0, total = 0, CHUNK_SIZE = 16, BUFFER_SIZE = 256, chunks = 0;
3+
char chunk[CHUNK_SIZE];
4+
char buffer[BUFFER_SIZE];
35
while(1){
4-
fgets(buff1, MAX_SIZE, stdin);
5-
int len = strnlen(buff1, MAX_SIZE);
6+
fgets(chunk, CHUNK_SIZE, stdin);
7+
len = strnlen(chunk, CHUNK_SIZE);
68
total += len;
7-
if(total <= MAX_SIZE) strncat(buff2, buff1, len);
9+
if(total <= MAX_SIZE){
10+
strncat(buffer, chunk, CHUNK_SIZE);
11+
chunks++;
12+
}
813
else break;
914
}
10-

codereview101/snipMemory6.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11

2-
int len = 0, total = 0;
2+
int len = 0, total = 0, CHUNK_SIZE = 16, BUFFER_SIZE = 256, chunks = 0;
3+
char chunk[CHUNK_SIZE];
4+
char buffer[BUFFER_SIZE];
35
while(1){
4-
fgets(buff1, MAX_SIZE, stdin);
5-
int len = strnlen(buff1, MAX_SIZE);
6+
fgets(chunk, CHUNK_SIZE, stdin);
7+
len = strnlen(chunk, CHUNK_SIZE);
68
total += len;
7-
if(total < MAX_SIZE) strncat(buff2, buff1, len);
9+
if(total < MAX_SIZE){
10+
strncat(buffer, chunk, CHUNK_SIZE);
11+
chunks++;
12+
}
813
else break;
914
}
1015

0 commit comments

Comments
 (0)