 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eugene Koudriavitski Guest
|
Posted: Fri Jul 30, 2004 9:48 am Post subject: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
Hi All,
I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
My code is a small part of some huge project, which takes an hour to
rebuild, and I want to be sure that I haven't changed the size of
MY_TYPE structure, by adding and deleting its data members.
As a temporary solution I created a small separate application
VerifySizeOfMyType that contains only MY_TYPE declaration and
std::cout<
However, every time I alter MY_TYPE, I have to rebuild this small
application and compare the new size with the old one.
I have feeling that since the compiler knows sizeof(MY_TYPE), then it
is probably possible to get it just after the compilation step without
building and running any additional code.
I think no preprocessor instructions can help, because sizeof is a
compiler operator which is calculated after preprocessing.
Can you advise how to solve the problem ?
Thanks,
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Frey Guest
|
Posted: Fri Jul 30, 2004 2:59 pm Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
Eugene Koudriavitski wrote:
| Quote: | I want to know sizeof(MY_TYPE) after the compilation stage.
Can you advise how to solve the problem ?
|
class A {
int i;
double d;
char c;
};
void check_size() {
char sizeof_A_is_[ sizeof( A ) ];
}
int main()
{
}
This prints a nice warning at the compilation stage for my compiler:
t.cc:8: warning: unused variable `char sizeof_A_is_[16]'
HTH,
Daniel
--
Daniel Frey
aixigo AG - financial solutions & technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: [email]daniel.frey (AT) aixigo (DOT) de[/email], web: http://www.aixigo.de
The hacks that we write today become the bugs of tomorrow.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Fri Jul 30, 2004 3:22 pm Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
Eugene Koudriavitski <evgenik1 (AT) yahoo (DOT) ca> wrote:
| Quote: | I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
My code is a small part of some huge project, which takes an hour to
rebuild, and I want to be sure that I haven't changed the size of
MY_TYPE structure, by adding and deleting its data members.
As a temporary solution I created a small separate application
VerifySizeOfMyType that contains only MY_TYPE declaration and
std::cout<
However, every time I alter MY_TYPE, I have to rebuild this small
application and compare the new size with the old one.
I have feeling that since the compiler knows sizeof(MY_TYPE), then it
is probably possible to get it just after the compilation step without
building and running any additional code.
I think no preprocessor instructions can help, because sizeof is a
compiler operator which is calculated after preprocessing.
Can you advise how to solve the problem ?
|
You can use static assertion like this one:
#define CONCAT(x, y) CONCAT1 (x, y)
#define CONCAT1(x, y) x##y
#define STATIC_ASSERT(expr) typedef char CONCAT(static_assert_failed_at_line_, __LINE__) [(expr) ? 1 : -1]
and then in your code
STATIC_ASSERT(sizeof(MY_TYPE) == N);
that will break the compilation if the expression is false.
You can find an advanced version of STATIC_ASSERT in boost/static_assert.hpp
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Jul 30, 2004 3:33 pm Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
In article
<007b01c475f2$ca100740$439d9c18 (AT) ym (DOT) phub.net.cable.rogers.com>, Eugene
Koudriavitski <evgenik1 (AT) yahoo (DOT) ca> writes
| Quote: | Hi All,
I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
My code is a small part of some huge project, which takes an hour to
rebuild, and I want to be sure that I haven't changed the size of
MY_TYPE structure, by adding and deleting its data members.
As a temporary solution I created a small separate application
VerifySizeOfMyType that contains only MY_TYPE declaration and
std::cout<
However, every time I alter MY_TYPE, I have to rebuild this small
application and compare the new size with the old one.
I have feeling that since the compiler knows sizeof(MY_TYPE), then it
is probably possible to get it just after the compilation step without
building and running any additional code.
I think no preprocessor instructions can help, because sizeof is a
compiler operator which is calculated after preprocessing.
Can you advise how to solve the problem ?
|
I suspect that any change you make to a type that results in a change to
its size (and many that do not) will force a rebuild of all code that
sees the definition of the type. Those parts that do not, cannot be
concerned with its size (and if you are playing games with malloc and an
external knowledge of the size of the type then your code is inherently
fragile and, IMO, lacks production quality).
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Fri Jul 30, 2004 3:37 pm Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
Eugene Koudriavitski wrote:
| Quote: | I want to know sizeof(MY_TYPE) after the compilation stage.
[...]
Can you advise how to solve the problem ?
|
Looks like you might use static assertions. Have a look here for an
implementation: http://www.boost.org/libs/static_assert/static_assert.htm
Regards,
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Karcher Guest
|
Posted: Fri Jul 30, 2004 4:53 pm Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
Eugene Koudriavitski <evgenik1 (AT) yahoo (DOT) ca> wrote:
| Quote: | I have feeling that since the compiler knows sizeof(MY_TYPE), then it
is probably possible to get it just after the compilation step without
building and running any additional code.
|
You are right. A sizeof expression ist a "integral constant expression",
which are usable for compile-time-arithmetic. You can do something like the
following example, which is a special case of compile time assertion. This
make the compiler emit an error if the size is wrong. The error message
should contain "checksize<TYPE,SIZE>" to tell you exactly which test fails.
You can look for "static assertions" in boost to find a more elaborate
version of this concept.
Michael Karcher
// Helper class
template <typename T, int S> checksize {
// Allowed array sizes in C++ start at 1.
// test1 is a error if sizeof(T) < S
char test1[sizeof(T) - S + 1];
// test2 is a error if sizeof(T) > S
char test2[S - sizeof(T) + 1];
};
// Your critical class
struct mytype {
int a,b,c,d;
};
// The checking code -> generates a 2 byte object (probably small enough
// to not be noticable in release code, perhaps do a #ifndef NDEBUG around
// it
static checksize<mytype,15> checker15; // should fail
static checksize<mytype,16> checker; // should compile (for 4 byte ints)
static checksize<mytype,17> checker17; // should fail
GCC output:
g++ newsgroup.cc -o newsgroup
newsgroup.cc: In instantiation of `checksize<mytype, 15>':
newsgroup.cc:20: instantiated from here
newsgroup.cc:8: error: creating array with size zero (`0')
newsgroup.cc: In instantiation of `checksize<mytype, 17>':
newsgroup.cc:22: instantiated from here
newsgroup.cc:6: error: creating array with size zero (`0')
Michael Karcher
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tom Tanner Guest
|
Posted: Sat Jul 31, 2004 3:06 am Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
"Eugene Koudriavitski" <evgenik1 (AT) yahoo (DOT) ca> wrote
| Quote: | Hi All,
I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
Well, no there isn't unless you compile something and examine the |
source code
| Quote: | My code is a small part of some huge project, which takes an hour to
rebuild, and I want to be sure that I haven't changed the size of
MY_TYPE structure, by adding and deleting its data members.
That's slightly different. There are a few macros around that allow |
you to check a value known at compile time. Here's one for C
#define CHECK(expr) { char unnamed[(expr) ? 1 : 0]; }
If you then include
CHECK(sizeof(MY_TYPE) == 16);
in your program, it just won't compile if the structure has been
changed.
If you want the check in the header, you'll have to use templates, and
define an enum or a class. The basic idea is
template<bool> struct Compile_Time_Check;
template<> struct Compile_Time_Check<true> {};
#define CHECK(name, expr) enum { name =
sizeof(Compile_Time_Check<(expr)>) }
You can then add
CHECK(sizecheck, sizeof(MY_TYPE) == 16);
in the header after the declaration of MY_TYPE. As soon as you compile
anything, if the size of MY_TYPE isn't 16 (or whatever), the compile
will fail.
There's probably a macro that does this even more nicely in Boost
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tom_usenet Guest
|
Posted: Sat Jul 31, 2004 3:08 am Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
On 30 Jul 2004 05:48:46 -0400, "Eugene Koudriavitski"
<evgenik1 (AT) yahoo (DOT) ca> wrote:
| Quote: | Hi All,
I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
My code is a small part of some huge project, which takes an hour to
rebuild, and I want to be sure that I haven't changed the size of
MY_TYPE structure, by adding and deleting its data members.
As a temporary solution I created a small separate application
VerifySizeOfMyType that contains only MY_TYPE declaration and
std::cout<
However, every time I alter MY_TYPE, I have to rebuild this small
application and compare the new size with the old one.
I have feeling that since the compiler knows sizeof(MY_TYPE), then it
is probably possible to get it just after the compilation step without
building and running any additional code.
I think no preprocessor instructions can help, because sizeof is a
compiler operator which is calculated after preprocessing.
Can you advise how to solve the problem ?
|
You can make size changes cause a compiler error. e.g.
typedef int checkMY_TYPE_size[sizeof(MY_TYPE) == 24];
//or however big it is
That works since it is illegal to declare an array of 0 size. Better
is to use a library facility:
http://www.boost.org/libs/static_assert/static_assert.htm (it's easy
to write your own).
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dag Viken Guest
|
Posted: Sat Jul 31, 2004 3:10 am Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
"Eugene Koudriavitski" <evgenik1 (AT) yahoo (DOT) ca> wrote
| Quote: | Hi All,
I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
My code is a small part of some huge project, which takes an hour to
rebuild, and I want to be sure that I haven't changed the size of
MY_TYPE structure, by adding and deleting its data members.
As a temporary solution I created a small separate application
VerifySizeOfMyType that contains only MY_TYPE declaration and
std::cout<
However, every time I alter MY_TYPE, I have to rebuild this small
application and compare the new size with the old one.
I have feeling that since the compiler knows sizeof(MY_TYPE), then it
is probably possible to get it just after the compilation step without
building and running any additional code.
I think no preprocessor instructions can help, because sizeof is a
compiler operator which is calculated after preprocessing.
Can you advise how to solve the problem ?
Thanks,
Eugene
|
If you are using Visual C with MFC and have runtime type information
enabled:
HMODULE hmod = GetModuleHandle("myapp.exe");
FARPROC procadd = GetProcAddress(hmod, "my mangled name");
CRuntimeClass* pClassInfo = reinterpret_cast
if (pClassInfo->m_nObjectSize != sizeof(MY_TYPE))
{
die();
}
You can find the mangled name for GetProcAddress using depends or dumpbin.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Florian Weimer Guest
|
Posted: Sat Jul 31, 2004 3:46 am Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
* Eugene Koudriavitski:
| Quote: | I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
|
When cross-compiling, autoconf uses a binary search algorithm and the
fact that an array of zero elements is ill-formed. It's rather
inefficient, but it doesn't require running any compiled code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alan Griffiths Guest
|
Posted: Sat Jul 31, 2004 1:50 pm Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
"Eugene Koudriavitski" <evgenik1 (AT) yahoo (DOT) ca> wrote
| Quote: | Hi All,
I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
|
Yes. But this is probably not a solution to your underlying problem.
| Quote: | My code is a small part of some huge project, which takes an hour to
rebuild, and I want to be sure that I haven't changed the size of
MY_TYPE structure, by adding and deleting its data members.
|
There are many other changes (re-orderings, changes of member names,
types or access) that also required a recompile of any translation
units that include the definition of MY_TYPE.
A better solution to detecting such changes is to use your version
control system to prevent edits to MY_TYPE.
| Quote: | Can you advise how to solve the problem ?
|
Guessing at what your real problem is, I suspect that this may point
the way:
http://www.octopull.demon.co.uk/c++/implementation_hiding.html
--
Alan Griffiths
http://www.octopull.demon.co.uk/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Sat Jul 31, 2004 6:49 pm Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
tom_usenet wrote:
[]
| Quote: | You can make size changes cause a compiler error. e.g.
typedef int checkMY_TYPE_size[sizeof(MY_TYPE) == 24];
//or however big it is
That works since it is illegal to declare an array of 0 size.
|
The reality is that Intel C++ 8 for Windows actually allows zero sized arrays issuing only a warning. The warning may be cast to an error but it's not on by default.
So, more reliable way is to use negative sizes instead of zero. That is:
typedef int checkMY_TYPE_size[sizeof(MY_TYPE) == 24 ? 1 : -1];
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Torjo Guest
|
Posted: Mon Aug 02, 2004 2:27 pm Post subject: Re: How to get sizeof(MY_TYPE) after the compilation ? |
|
|
"Eugene Koudriavitski" <evgenik1 (AT) yahoo (DOT) ca> wrote
| Quote: | Hi All,
I want to know sizeof(MY_TYPE) after the compilation stage.
Is there any way to get this information without building and running
the project?
My code is a small part of some huge project, which takes an hour to
rebuild, and I want to be sure that I haven't changed the size of
MY_TYPE structure, by adding and deleting its data members.
As a temporary solution I created a small separate application
VerifySizeOfMyType that contains only MY_TYPE declaration and
std::cout<
However, every time I alter MY_TYPE, I have to rebuild this small
application and compare the new size with the old one.
I have feeling that since the compiler knows sizeof(MY_TYPE), then it
is probably possible to get it just after the compilation step without
building and running any additional code.
I think no preprocessor instructions can help, because sizeof is a
compiler operator which is calculated after preprocessing.
Can you advise how to solve the problem ?
|
I'm not sure you're solving the right problem
As Francis pointed out, such advanced uses of sizeof() are really
dangerous - they require knowledge of the compiler internals, etc. -
and could easily lead to broken code (especially if your module
interacts with other modules).
So I think the real question is: why are you concerned with the size
of MY_TYPE (and its change)? Could you not redesign your code so that
this won't matter?
Best,
John
John Torjo
Freelancer
-- [email]john (AT) torjo (DOT) com[/email]
Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/
Professional Logging Solution for FREE
-- http://www.torjo.com/code/logging.zip (logging - C++)
-- http://www.torjo.com/logview/ (viewing/filtering - Win32)
-- http://www.torjo.com/logbreak/ (debugging - Win32)
(source code available)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|