Do not do what you just posted in a real program. Ever.
An unsigned long is 4 bytes in length, and you've allocated 10 (not a multiple of 4) bytes for the pointer.
Probably what you want is this:
Code: Select all
unsigned char* p;
p = reinterpret_cast<unsigned char*>(malloc(11));
// or p = (unsigned char*) malloc(11); if you're not using C++
unsigned char* p2 = p + 10;
If you only allocate 10 bytes and p points to the address 10, then only the addresses 10, 11, 12, 13, 14, 15, 16, 17, 18, and 19 validly belong to that pointer.
Keep in mind that pointers and arrays are very closely related. p + 10 is essentially equivalent to p[10]. When you add to a pointer by saying p + 10 and p is of type unsigned long*, you're actually telling your program to advance p by 10
unsigned longs, i.e. 40 bytes.