0

I am working on a project which is using 1 byte alignment by default, including in places that use the CRITICAL_SECTION struct. I am investigating a certain deadlock and I can see that the data in the CRITICAL_SECTION struct which is causing the trouble is invalid .. can the alignment be the trouble ? (I can also see that for this specific CRITICAL_SECTION indeed the alignment is 1 (odd address) ..)

Thanks, Amit

Amit
  • 173
  • 1
  • 1
  • 9

1 Answers1

3

Windows header files require /Zp8 packing. You must respect the packing specified by the header file.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • David - both. In the case I described for example I can see that both the struct itself and its LockCount member have an alignment of 1 – Amit Nov 18 '12 at 14:31
  • Thanks Raymond, still I would be glad to also know if anyone experienced a problem due to a non-supported alignment of the CRITICAL_SECTION struct in specific, to be more assured that this is also my problem ... – Amit Nov 18 '12 at 14:41
  • @Amit Whether this is a likely cause will depend on which version of Windows and which processor architecture you are using. – Raymond Chen Nov 18 '12 at 15:04
  • About architecture it is basically a x64 processor but the application is 32 bit. – Amit Nov 18 '12 at 15:14
  • I didn't know the answer but I decided to search on SO. (You could've done that too.) [This answer](http://stackoverflow.com/a/5178914/902497) says that unaligned accesses that cross a cache line boundary are not atomic on certain CPU families. – Raymond Chen Nov 18 '12 at 16:12
  • Thanks Raymond.. this doesn't address Critical Sections specifically .. but I guess it's necessary that somewhere in its guts the functions like EnterCriticalSection etc use some atomic instruction like that .. – Amit Nov 18 '12 at 16:36