Note that there are some explanatory texts on larger screens.

plurals
  1. POAlgorithm for determining Alignment of elements in C/C++ structs
    text
    copied!<p>Okay, Allow me to re-ask the question, as none of the answers got at what I was really interested in (apologies if whole-scale editing of the question like this is a faux-paus).</p> <p>A few points:</p> <ul> <li>This is offline analysis with a different compiler than the one I'm testing, so SIZEOF() or similar won't work for what I'm doing.</li> <li>I know it's implementation-defined, but I happen to know the implementation that is of interest to me, which is below.</li> </ul> <p>Let's make a function called pack, which takes as input an integer, called alignment, and a tuple of integers, called elements. It outputs another integer, called size.</p> <p>The function works as follows:</p> <pre><code>int pack (int alignment, int[] elements) { total_size = 0; foreach( element in elements ) { while( total_size % min(alignment, element) != 0 ) { ++total_size; } total_size += element; } while( total_size % packing != 0 ) { ++total_size; } return total_size; } </code></pre> <p>I think what I want to ask is "what is the inverse of this function?", but I'm not sure whether inversion is the correct term--I don't remember ever dealing with inversions of functions with multiple inputs, so I could just be using a term that doesn't apply.</p> <p>Something like what I want (sort of) exists; here I provide pseudo code for a function we'll call determine_align. The function is a little naive, though, as it just calls pack over and over again with different inputs until it gets an answer it expects (or fails).</p> <pre><code>int determine_align(int total_size, int[] elements) { for(packing = 1,2,4,...,64) // expected answers. { size_at_cur_packing = pack(packing, elements); if(actual_size == size_at_cur_packing) { return packing; } } return unknown; } </code></pre> <p>So the question is, is there a better implementation of determine_align?</p> <p>Thanks,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload