I was just experiminting with the following C code and i noticed something interesting.. The first loop runs fine, but from the second loop the variable r1 occupies the kernel address space(ffffffffe) and instead of randomly changing as it is supposed to, all the consecutive loops show r1 as occupying the same address space. Further, when i tried to access the content of that address space my antivirus caught this program as a piece of malicious code.
#include<stdio.h>
#include<conio.h>
void funct1(){
char *r1;
char r2;
int count=0;
printf("r1= %x r2= %x\n",r1,&r2);
//printf("*r1=",*r1);-- unable to open, permission denied, takes as a virus
printf("\nin hex: %x\n",r1-(&r2));//print difference between address spaces as hex
printf("\nin char: %c\n",r1-(&r2));//print difference between address spaces as char
printf("\nin decimal: %d\n",r1-(&r2));//print difference between address spaces as decimal
r1=&r2;
printf("After: r1= %x r2= %x\n\n",r1,&r2);
getch();
funct1();
}
int main()
{
funct1();
getch();
return 0;
}
Can someone help me understand as to why this occurs?
- Warning : Running the code multiple times caused my C-free compiler to crash on my Windows Machine.