1828332 Members
4900 Online
109976 Solutions
New Discussion

using sizeof

 
SOLVED
Go to solution
Eric_369
Advisor

using sizeof

On the microsoft platform using Visual Studio it is valid to declare variables and use the sizeof() method on the variable to get the size. For instance I can declare the following variables (below) and use the
sizeof(variable name) and the correct size will return.

long x[10][10];
char y[20];

struct XXX
{
long x[10][10];
char y[20];
}strct;

sizeof(x) is 100;
sizeof(y) is 20;
sizeof(strct) is 120;

I have done some testing on the itanium and found it works! Is this just a fluke or is this a valid way to do this on the OpenVMS platform?

Thanks
6 REPLIES 6
Bojan Nemec
Honored Contributor
Solution

Re: using sizeof

Eric,

This works. I think that this is part of the ANSIC standard.

The documentation:

http://h71000.www7.hp.com/commercial/c/docs/6180profile_016.html#index_x_563

says that the parameter to sizeof can be an expression.

Bojan
David Jones_21
Trusted Contributor

Re: using sizeof

sizeof(x) should be 400 or 800, depending upon how big a 'long' is.
I'm looking for marbles all day long.
Eric_369
Advisor

Re: using sizeof

Bojan,
Thanks much!

Dave,
Thanks, I calculated wrong. It's 400
and 420 for 'strct.'
Eric
John Gillings
Honored Contributor

Re: using sizeof

Eric,

One thing to beware of... the sizes of objects might differ between your Windows and OpenVMS code. The OpenVMS compiler, by default, will align fields within a structure on their natural boundaries, inserting "padding" where necessary. This may result in a structure larger than the simple sum of the component fields. Usually this isn't a problem, but if you're expecting to share binary data files across platforms, you might need to check that the structures are compatible.
A crucible of informative mistakes
Eric_369
Advisor

Re: using sizeof

John,
Thanks! When we share data across platforms in the Client/Server environment we are using the #pragma pack(1) on our Windows clients and
#pragma nomember_alignment on the HP OpenVMS server aligning both structure members on byte boundries.
Eric
Joseph Huber_1
Honored Contributor

Re: using sizeof

If this is an existing software, o.k. let it be so, but for a new project starting now, DON'T do it, You will regret later, e.g. when You have to port to a another architecture or just new "clients" programming in a different langauage.
Better use a portable data format like XDR or XML.
http://www.mpp.mpg.de/~huber