Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

shitty C pointer arithmetic

Name: Anonymous 2010-05-29 14:38

Why are pointers in C aligned to the size of their data? Why cant they just reference memory bytewise?

How can i manipulate a pointer so that it references a address not by its own size, without typecasting it?

Name: Anonymous 2010-05-30 4:25

>>1
You can reference memory bytewise, if you wish to do so, for example:


char *mem = (char*) 0x401000;
char *p;
char *mem_end = mem + 0x1000;

for ( p = mem; p < mem_end ; p++)
  printf("%02X", *p)


That first cast is implmentation dependent behaviour, but on common arch's like x86 (MSVC or GCC or ICC compilers), pointers are internally kept as integers, so casting an integer to a pointer will just set the pointer's memory address to it. You do however need at least one cast.

You can change the alignment of fields in structures and sometimes even of data on the stack by using compiler-specific pragamas, but you should only do this if you really need it. A legitimate example is for example reading entire structures from binary files and accessing the data directly. However, not letting your compiler align your data (for example align each field to sizeof(int) will lead to slower memory accesses).

Can you describe what exactly you want to achieve?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List