K
Kenneth Brody
Given the following:
char *ptr1, *ptr2;
size_t n;
ptr2 = ptr1 + n;
Assuming ptr1 is a valid pointer, is the following guaranteed to be true?
(ptr2 - ptr1) == n
What if n is greater than the size of the buffer to which ptr1 points?
For example:
char buf[10];
char *pt = buf + 100;
size_t n = (pt - buf);
Is n guaranteed to be 100? Or, does the simple act of calculating an
address off the end of the buffer (beyond the address that immediately
follows the buffer) invoke UB?
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
char *ptr1, *ptr2;
size_t n;
ptr2 = ptr1 + n;
Assuming ptr1 is a valid pointer, is the following guaranteed to be true?
(ptr2 - ptr1) == n
What if n is greater than the size of the buffer to which ptr1 points?
For example:
char buf[10];
char *pt = buf + 100;
size_t n = (pt - buf);
Is n guaranteed to be 100? Or, does the simple act of calculating an
address off the end of the buffer (beyond the address that immediately
follows the buffer) invoke UB?
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>