C
Chris M. Thomasson
Chris M. Thomasson said:
Umm, I should probably use:
#include <new>
instead of:
#include <memory>
http://pastebin.org/96334
Sorry about that!
Chris M. Thomasson said:
I am missing something here. How do I use the macro above to align:
char temp[12287];
on a 4096 byte boundary? Here is what I am trying to do:
#define ALIGN(p, base, align) (base + (p - base + (align-1) & ~(align-1)))
char buffer[12287];
char* align = ALIGN(buffer, buffer, 4096);
if (((size_t)align) % 4096)
{
puts("NOT ALIGNED");
}
I am getting NOT ALIGNED output which means the align pointer to not
aligned. What am I doing wrong?
Thanks!
On the other hand, the core alignment calculation of "return (T*)
((((uintptr_type)ptr) + result - 1) & ((uintptr_type)(-result)));" is
not guaranteed to work on _every_ platform. Casting a pointer to an
integer gives strange results on some exotic hardware. If you care
about that (and most people don't), the calculation my ALIGN() macro
will continue to work even in these situations.
FWIW, check out this simple macro:
_________________________________________________
#define ALIGN_OF(mp_type) \
offsetof( \
struct \
{ \
char pad_ALIGN_OF; \
mp_type type_ALIGN_OF; \
}, \
type_ALIGN_OF \
)
Or, if you don't want a macro:
template<typename T>
size_t align_of()
{
struct aligner
{
char pad;
T aligned;
};
return offsetof(aligner, aligned);
}
FWIW, check out this simple macro:
_________________________________________________
#define ALIGN_OF(mp_type) \
offsetof( \
struct \
{ \
char pad_ALIGN_OF; \
mp_type type_ALIGN_OF; \
}, \
type_ALIGN_OF \
)
_________________________________________________
This is really helpful. My code already uses the same basic technique,
but I did not realize there was a way to pack it into a single-line
macro like this. That will really simplify some stuff, so thanks!
Alf P. Steinbach said:* s_tec:
I posted essentially the same solution but as a C++ function template and
with an additional alignment helper function as the very first response in
this thread.
Then instead of ALIGN_OF( Type ) you write alignmentOf<Type>()...
Reposting that:
<code>
typedef ptrdiff_t Size;
template< typename T >
inline Size alignmentOf()
{
struct X
{
char bah;
T t;
};
return offsetof( X, t );
}
Chris M. Thomasson said:I don't think `offsetof()' works with POD.
This is really helpful. My code already uses the same basic technique,
but I did not realize there was a way to pack it into a single-line
macro like this. That will really simplify some stuff, so thanks!
YIKES! I meant to say:
I don't think `offsetof()' works with non-POD types.
;^o
Alf P. Steinbach said:* Chris M. Thomasson:Chris M. Thomasson said:news:[email protected]... [...]
Reposting that:
<code>
typedef ptrdiff_t Size;
template< typename T >
inline Size alignmentOf()
{
struct X
{
char bah;
T t;
};
return offsetof( X, t );
}
I don't think `offsetof()' works with POD.
YIKES! I meant to say:
I don't think `offsetof()' works with non-POD types.
;^o
Formally it doesn't, in practice it may and probably will, depending on
the compiler.
After all there's not much magic underneath.
Of course that also applies to the macro that you posted.
Chris said:Well, I only use that macro in C where everything is a POD. For C++ I
use something like:
http://pastebin.org/96326
Which seems to be working fine for everything I throw at it so far,
including reference types.
For instance, I cannot get the following to compile and run correctly:
Then when I run the program, it crashes. What am I doing wrong here Alf?
Alf P. Steinbach said:* Chris M. Thomasson:[...]
Reposting that:
<code>
typedef ptrdiff_t Size;
template< typename T >
inline Size alignmentOf()
{
struct X
{
char bah;
T t;
};
return offsetof( X, t );
}
I don't think `offsetof()' works with POD.
YIKES! I meant to say:
I don't think `offsetof()' works with non-POD types.
;^o
Formally it doesn't, in practice it may and probably will, depending
on the compiler.
After all there's not much magic underneath.
Of course that also applies to the macro that you posted.
Well, I only use that macro in C where everything is a POD. For C++ I
use something like:
http://pastebin.org/96326
Which seems to be working fine for everything I throw at it so far,
including reference types.
For instance, I cannot get the following to compile and run correctly:
___________________________________________________________
#include <cstddef>
#include <iostream>
template< typename T >
inline std::size_t alignmentOf()
{
struct X
{
char bah;
T t;
};
return offsetof( X, t );
}
struct object
{
char m_1[2];
double m_d[6];
long m_2[2];
short m_3[14];
};
int
main()
{
std::cout << "alignmentOf<object&> == "
<< alignmentOf<object&>()
<< std::endl;
return 0;
}
___________________________________________________________
On Comeau I get:
___________________________________________________________
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 8: warning: class "X" defines no constructor to
initialize the
following:
reference member "X::t"
struct X
^
detected during instantiation of
"size_t alignmentOf<T>() [with T=object &]" at line 32
"ComeauTest.c", line 14: error: expression must have a constant value
return offsetof( X, t );
^
detected during instantiation of
"size_t alignmentOf<T>() [with T=object &]" at line 32
1 error detected in the compilation of "ComeauTest.c".
___________________________________________________________
For MingW 3.4.5 I get the following warnings:
___________________________________________________________
In function `size_t alignmentOf() [with T = object&]':
instantiated from here
[Warning] invalid access to non-static data member `alignmentOf() [with
T = object&]::X::t' of NULL object
[Warning] (perhaps the `offsetof' macro was used incorrectly)
___________________________________________________________
Then when I run the program, it crashes. What am I doing wrong here Alf?
Ian Collins said:C doesn't have reference types.
Remember you can't dynamically allocate reference types, so they aren't
applicable to the original question.
Alf's function (or your macro) won't work with reference types because the
dummy struct has a reference member but no constructor.
Alf P. Steinbach said:* Chris M. Thomasson: [...]
Well, I only use that macro in C where everything is a POD. For C++ I use
something like:
http://pastebin.org/96326
Which seems to be working fine for everything I throw at it so far,
including reference types.
Yah, it's better , because it's simpler & more well-defined.
I wonder whether the constructor you put in there is really necessary
though.
At first glance apparently it supports non-default-constructible type T by
assuming that T is copy constructible. But thinking about it, what it does
seems to be to limit T to copy constructible types. So how about, if the
compiler really insists on some constructor, just /declaring/ a default
constructor and not implementing, like
struct object
{
T m_object;
object();
};
After all, you'll never create any actual instance of this class?
I also believe that it has a problem with reference types.
Hm?
For instance, I cannot get the following to compile and run correctly:
___________________________________________________________ [...]
Then when I run the program, it crashes. What am I doing wrong here Alf?
Using a reference?
;^)
My hm was more like, why would you want to deal with that case?
Chris M. Thomasson said:Remember you can't dynamically allocate reference types, so they aren't
applicable to the original question.
You can dynamically allocate objects that contain references. [...]
__________________________________________________________________________
This is example of where I might need the alignment of a reference type...
Alf P. Steinbach said:* Chris M. Thomasson:Chris M. Thomasson said:
YIKES! I meant to say:
I don't think `offsetof()' works with non-POD types.
;^o
Formally it doesn't, in practice it may and probably will, depending on
the compiler.
Chris said:Chris M. Thomasson said:Remember you can't dynamically allocate reference types, so they
aren't applicable to the original question.
You can dynamically allocate objects that contain references. [...]
__________________________________________________________________________
This is example of where I might need the alignment of a reference
type...
Ahhh. I think I just made a false point here. I am not determining the
alignment of a "pure" reference type. Instead, I am calculating the
alignment of an object that happens to contain a reference type. Well,
you make a good point Ian. I am having trouble thinking of how I can use
the alignment of a pure reference type!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.