3

I have the following local variable (that will get stored in the stack).

struct test1 {
  int a;
  int b;
  char c;
};

How do I align the starting address of integer a to a 16byte boundary in the stack?

I am running this C code on a custom written MIPS ISA processor.

masoud
  • 55,379
  • 16
  • 141
  • 208

3 Answers3

2

Here is some non-standard way of aligning your data.

struct test1 *pdata;

// here we assume data on stack is word aligned
pdata = alloca(sizeof(*pdata) + 14);
if (pdata & 0xF)
{
    pdata = (unsigned char*)pdata + 16 - (pdata & 0xF);
}
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
1

AFAIK On MIPS ISA C compilers are forced align to word boundaries in this case, just add an int (which is 32 bit) to the structure. so:

struct test1 {
   int a;       // 4 bytes
   int b;       // 4 bytes
   int foralign;// 4 bytes for 16 byte alignment
   char c;      // 1 byte but will be aligned 4 bytes

};
Volkan
  • 2,212
  • 1
  • 14
  • 14
  • Tecnhincally it just grows in size, but doesn't become 16-byte aligned. – Valeri Atamaniouk Mar 16 '13 at 19:57
  • @Valeri It will grow into 16 bytes if compiler word alignment is switched on... yep. If I am wrong inform me so I will erase my answer. I do not like unnecessary answer piles in SO too... Thanks in advance. – Volkan Mar 16 '13 at 20:42
1

Unfortunately there's no standard way to get objects aligned like that. There's almost always some compiler-specific trick, such as __attribute__ in GCC, but you'll have to check your compiler's documentation.

(Of course there's no standard use for that kind of alignment either, which is why there's no standard method for achieving it. So you're probably resorting to extensions already, so there's no real harm going further.)

A union that contains a large-enough elemental object often does the trick, but I believe the largest elemental C objects for MIPS CPUs are long long and double, which are only 8 bytes.

torek
  • 448,244
  • 59
  • 642
  • 775