|
From: Lars C. <la...@cs...> - 2001-06-06 00:39:56
|
On Tue, 5 Jun 2001, Bill Soudan wrote: > > On Tue, 5 Jun 2001, Lars Christensen wrote: > > > Description: This patch fixes cast alignment bugs detected by using the > > -Wcast-align option for gcc. Although > > > > *((DWORD*)&buffer[5]) = value; > > > > may work on an intel processor, it is not portable because some > > architechtures (e.g. sparc) cannot copy DWORDS if they are not aligned on > > a multiple of 4 bytes. The attached patch fixes all occurences of such > > statements to: > > > > memcpy(buffer + 5, &value, sizeof(DWORD)); > > > > The patches is tested and icqlib works on Sparc Solaris with is (UDP > > communication at least. TCP code is also changed, but untested - it does > > compile though). icqlib would about with a "bus error" or "segmentation > > fault" without this patch. > > Interesting - I never realized you have to worry about alignment. > > Why doesn't the compiler generate assembly code to just do the copy byte > by byte? Any ideas? :) For efficieny. The dword copy is many times faster than byte-by-byte copy. If the compiler needed to generate safe code it would have to do this in any case, even when direct assignment to the adresses of the casted pointer is indeed possible, e.g. n = 8 *(DWORD*)&buffer[n] = 0 -- Lars Christensen, la...@cs... |