Re: Dynamic buffer library



Andrew Poelstra schrieb:
Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx> writes:

Andrew Poelstra schrieb:

"Spiros Bousbouras" <spibou@xxxxxxxxx> writes:


From the code it seems that your intention is to store
the "items" starting at buf[3]. Are the items of type
size_t ? If not how do you know that buf[3] is properly
aligned ?

The items may not be of size_t. I don't know that they're properly
aligned (this is an example of the UB I knew that I missed). What
would be the best way for me to guarantee alignment? Am I going to
need to use a union?

There is no portable way that does all the work; some time ago,
I wrote a "memsize" solution that does store the size in a standard
conforming albeit ugly and wasteful way. I asked for a code review
back then but did not receive any response; you are welcome to take
out the useful part of the ideas:
<3649apF4s752rU1@xxxxxxxxxxxxxx>
Download seems still possible.

Thanks! That'll be useful, and probably end up being my solution.

A quick question, though, is struct padding consistant?

That is, for every struct { int x, size_t y, float z }, will (&z - &x)
always be the same number? Because if so, a more efficient solution
could be found.

If you always use the same identifiers for members of structs
with this layout and this sequence of types in your source: Yes
(see "compatible type" in the standard).
If not: The standard does not guarantee it but in every
implementation worth its salt, yes.
Read the bits about compatible types w.r.t. structure types and
about the "common initial sequence" for unions in the standard
to get a feeling about what is guaranteed (C90 or C99).
The DS900x series probably uses a member name based struct layout
for struct types not used in a union...

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.



Relevant Pages

  • Re: WaitForSingleObject() will not deadlock
    ... But what is TSO? ... Technical Standard Order, a minimum performance standard issued by the FAA for certain ... have been more commonly referred to as the "cache coherency" ... A mutex is sufficient to guarantee visibility, ...
    (microsoft.public.vc.mfc)
  • Re: Segfault City
    ... Realizing ... What, if anything, can it be guaranteed will be> '9' in standard C? ... there /isn't/ any guarantee of a character greater than '9' in C. ...
    (comp.lang.c)
  • Re: Segfault City
    ... Andrew Poelstra wrote: ... What, if anything, can it be guaranteed will be> '9' in standard C? ... Perhaps I should have studied English more in high school at East ... there /isn't/ any guarantee of a character greater than '9' in C. ...
    (comp.lang.c)
  • Re: questions on ftell and fopen
    ... foo: ... actually guarantee that a failing fopensets errno. ... A standard is a contract between the implementer and the programmer. ...
    (comp.lang.c)
  • Re: questions on ftell and fopen
    ... actually guarantee that a failing fopensets errno. ... Since the standard doesn't guarantee that a failing fopendoes *NOT* ... A standard is a contract between the implementer and the programmer. ...
    (comp.lang.c)