3

In C, with MSVC and/or GCC, is it possible to control the alignment of function parameteres?

e.g.

int main()
{
  __declspec( align(64) ) unsigned long long int
    test = 5;
  my_function_call( test );
  return( EXIT_SUCCESS );
}

void my_function_call( unsigned long long int fc_test )
{
  return;
}

What I wish to achieve is that fc_test is 64 byte aligned.

AFAICT, this is not possible, but confirmation/denial is sought.

  • 3
    I think you are interested in this previous question: http://stackoverflow.com/questions/841433/gcc-attribute-alignedx-explanation – ziu Jul 15 '12 at 09:50
  • Yes - thankyou. I searched prior to posting, but that article escaped me. –  Jul 15 '12 at 09:56

2 Answers2

3

When my_function_call is called, it is passed a copy of test. It is not passed test itself, so the alignment of test is irrelevant. The alignment attribute is a property of the test object, not of its value.

Arguments are passed in a platform-specified way. Usually, small arguments are passed in registers. On most machines, registers are special hardware that is not part of memory, so they do not have addresses and so do not have alignment.

If an argument is large, the platform specification might specify that it is passed in memory. Then it is the job of the calling function to place a copy of test into memory and pass its address to the called function, and for the called function to use that address to retrieve the copy. It is possible a compiler could give you some influence over the alignment of that temporary copy, but I do not know of any compiler that does so.

If, for some reason, you want the called function to be passed an aligned address, then, instead of passing the value of test, pass its address or the address of a copy (which also as the alignment attribute). Additionally, if you are passing an aligned address and want the called function to know it is aligned, then declare the parameter of the function to be a pointer to a type which has the alignment attribute. (The syntax for doing this depends on the compiler, as it is not a part of standard C. I suspect a means for doing so is to define a type using typedef, and including the alignment attribute in the typedef, then declaring the function parameter to be a pointer to that type.)

You should not be concerning yourself with the alignment of objects without good reason.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • It appears GCC (recent versions, 4.5+) honours the variable definition alignment attribute when it comes to their position on the function call stack! –  Jul 15 '12 at 13:00
-1

On x64, alignment is 16 bytes.

On ARM, alignment in 8 bytes.

On x86, alignment is 4 bytes. If you pass a variable by value on the stack to a function which will perform contigous double-word compare and swap, you can be misaligned and segfault. I think you need to either pass the address of the variable, or define a local varible and make a copy of the argument.

Appears impossible with MSVC to control. GCC apparently does honour on the stack variable definition alignment qualifiers (!)