1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
void _printf(const char* s) { // if we had no format codes
for(int i = 0; s[i] != '\0'; i++) { // A for loop is also a "conditional jump".
fputc(s[i], stdout); // Also: while, do, switch, if
}
}
// You need the null terminator for strings in memory as a convention. It is expected
// by printf(…), strlen(…), and other standard string library functions.
//
// In a double-quoted string literal in your code (e.g., "…"), the '\0' is implicit.
// Otherwise, you are responsible for putting it there.
int main(int argc, char* argv[]) {
// BAD CODE will follow.
char s[4];
s[0] = 'a';
s[1] = 'c';
s[2] = 'e';
// OOPS... forgot the '\0'
// printf("%s\n", s);
_printf(s);
/* $ gcc -o conditional_jump_2 conditional_jump_2.c
*
* $ valgrind ./conditional_jump_2
*
* Conditional jump or move depends on uninitialised value(s)
* at 0x4C2D128: __GI_strlen (vg_replace_strmem.c:462)
* by 0x4EA7751: puts (ioputs.c:35)
* by 0x40056B: main (conditional_jump_2.c:14)
*/
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2021 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.