[cfe-dev] Negative Index of Pointer

phy coder via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 27 01:16:29 PST 2022


So, I was trying to do this  , what I was expecting is undefined behavior
by ptr[-4] and b[-4] but the undefined behavior is only shown by b[-4] . I
know that a pointer can have a negative index when there is some defined
value at that address . But in this case , sanitizer has to give undefined
behavior for ptr[-4]  but there is no such error . I'm not sure if this is
considered as Undefined Behavior or not . Or maybe some other tools exist
for this type of undefined behavior .


#include <iostream>

int main() {
int b[]= {7,5,4,3,2} ;

char a[] = "hello";
char *ptr = a;
std::cout<< ptr[0] << ptr[-4] << b[0] << b[-4];
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20220127/4a7bbebf/attachment.html>


More information about the cfe-dev mailing list