------- Comment #10 from mikulas at artax dot karlin dot mff dot cuni dot cz  
2009-07-07 18:07 -------
So you mean that that ->x operator is invalid and break the standard?

Anyway the standard means "if you write your code according to the standard =>
the code will run correctly", but the inverse implication doesn't apply. It is
sometimes required to break the C standard.

For example, you write "unsigned char *framebuffer = vga_getgraphmem();" and
now you want to access the framebuffer. According to the standard, you could
only do it by bytes. But that means one bus cycle to the videocard for every
byte transfered. So people understand that common processors allow aligned
accesses to 2, 4 or 8 bytes and that common videocards have their framebuffer
base address aligned on same larger boundary --- and they simply cast the
pointer to u_int32_t or u_int64_t and access the videoram faster. Each time you
watch some video, remember those undefined memory accesses that are hapenning
for you to get faster performance :)

Another example --- the C standard says how it's not allowed to even produce a
pointer that points before the allocated array or more than one entry after the
last entry of the array and how it's not allowed to subtract two pointers from
different arrays. It is perfectly rational --- unless you are writing the
memory allocator itself! Then you inevitably must do some operations that are
considered "undefined" by the standard.

Regarding that int * to short * cast --- obviously, the code may run on some
computer that has tagged memory and traps if access with invalid type is done.
But common computers don't have tagged memory and the programmer should be
allowed to do such casts if he understands the implications (for example, if
its done only in arch-specific part of an operating system, it is perfectly
legal). So there should be a method to silence the warning.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40665

Reply via email to