Monday, April 16, 2012

C/C++ strcpy unhandled read violation

unsigned char* Data::getAddress(unsigned char* address)
{
strcpy((char*)address, (char*)this->_address);
return (unsigned char*)address;
}

int main()
{
Data d;
d.makealinkedlisthere();
while (d)
{
unsigned char address[256];
printf("0x%08x \r\n",d.getAddress(address));
d = d.getNext();
}
return 0;
}


It returns the first two (which is the same, and it should be different [can tell from the debugger]...) then crashes out.



It just makes a linked list. protected member Data* _next ... a chain of them.



The unsigned char* is from Windows function VirtualQueryEx part of the MEMORY_BASIC_INFORMATION data structure it returns.



this->_address = (unsigned char*)meminfo->BaseAddress; // casted from void*


It is void*, but I see it converted to unsigned char* in other's codes. In the debugger I can see it represented as a hex number.



D1: +    _address   0x7ffd5000 <Bad Ptr>    unsigned char * 
D1->_next:+ _address 0x7f6f0000 "áå•ú`©" unsigned char *
D1->_next->_next+ _address 0x7ffb0000 " " unsigned char *




No comments:

Post a Comment