473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Code to find the size of an object


I was thinking about how you can only use sizeof to find the size of an
unmanaged type. I started to wonder if there was a way to find the size of a
managed object and came up with the following. It's not guaranteed behaviour
but it seems to work. Doesn't lose verifiability either. Enjoy.
Cheers
Jon Jagger

namespace JSL
{
using System.Runtime.InteropServices;

public delegate object Creator();

public static class Size
{
public static int Of(Creator f)
{
Union a = new Union();
a.u2.o = f();
Union b = new Union();
b.u2.o = f();

return System.Math.Abs(a.u1.value - b.u1.value);
}

private struct U1
{
public int value;
}

private struct U2
{
public object o;
}

[StructLayout(LayoutKind.Explicit)]
private struct Union
{
[FieldOffset(0)] public U1 u1;
[FieldOffset(0)] public U2 u2;
}
}
}

namespace Example
{
using JSL;
using System;

class C
{
public C()
{
a = b = c = d = 42;
}

private int a,b,c,d;
}

class App
{
static void Main()
{
Creator c = delegate { return new C(); };
Console.WriteLine(Size.Of(c));
}
}
}
Nov 15 '05 #1
6 2450
Jon,
Your code only works if all members are primitive types, try adding a string
and check the result.
It also assumes that a and b are laid-out sequentially which is a wrong
assumption when multiple threads are creating objects, and is neither
guaranteed by the CLR.

Willy.

"Jon Jagger" <jo*@jaggersoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

I was thinking about how you can only use sizeof to find the size of an
unmanaged type. I started to wonder if there was a way to find the size of a managed object and came up with the following. It's not guaranteed behaviour but it seems to work. Doesn't lose verifiability either. Enjoy.
Cheers
Jon Jagger

namespace JSL
{
using System.Runtime.InteropServices;

public delegate object Creator();

public static class Size
{
public static int Of(Creator f)
{
Union a = new Union();
a.u2.o = f();
Union b = new Union();
b.u2.o = f();

return System.Math.Abs(a.u1.value - b.u1.value);
}

private struct U1
{
public int value;
}

private struct U2
{
public object o;
}

[StructLayout(LayoutKind.Explicit)]
private struct Union
{
[FieldOffset(0)] public U1 u1;
[FieldOffset(0)] public U2 u2;
}
}
}

namespace Example
{
using JSL;
using System;

class C
{
public C()
{
a = b = c = d = 42;
}

private int a,b,c,d;
}

class App
{
static void Main()
{
Creator c = delegate { return new C(); };
Console.WriteLine(Size.Of(c));
}
}
}

Nov 15 '05 #2
Still works for me. As for assumptions - I specifically said it was _not_
guaranteed behaviour but thanks for being explicit about what the reasons
are.
Cheers
Jon Jagger
that a,b would always be layed
a,b being layed out sequentially and threading...these are exactly the
reasons it's not guaranteed behaviour (which I mentioned).

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eF**************@tk2msftngp13.phx.gbl...
Jon,
Your code only works if all members are primitive types, try adding a string and check the result.
It also assumes that a and b are laid-out sequentially which is a wrong
assumption when multiple threads are creating objects, and is neither
guaranteed by the CLR.

Willy.

"Jon Jagger" <jo*@jaggersoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

I was thinking about how you can only use sizeof to find the size of an
unmanaged type. I started to wonder if there was a way to find the size
of a
managed object and came up with the following. It's not guaranteed

behaviour
but it seems to work. Doesn't lose verifiability either. Enjoy.
Cheers
Jon Jagger

namespace JSL
{
using System.Runtime.InteropServices;

public delegate object Creator();

public static class Size
{
public static int Of(Creator f)
{
Union a = new Union();
a.u2.o = f();
Union b = new Union();
b.u2.o = f();

return System.Math.Abs(a.u1.value - b.u1.value);
}

private struct U1
{
public int value;
}

private struct U2
{
public object o;
}

[StructLayout(LayoutKind.Explicit)]
private struct Union
{
[FieldOffset(0)] public U1 u1;
[FieldOffset(0)] public U2 u2;
}
}
}

namespace Example
{
using JSL;
using System;

class C
{
public C()
{
a = b = c = d = 42;
}

private int a,b,c,d;
}

class App
{
static void Main()
{
Creator c = delegate { return new C(); };
Console.WriteLine(Size.Of(c));
}
}
}


Nov 15 '05 #3
No sure how you are considering the result as valid?
Just modify your code as follows (and run in sequence):

1. Add a unitialized string member to the class, now Size.Of will return 28
(which is wrong).
2. Assign a value to the string in the constructor, lets say string s =
"Hello", the return value will be 56(which is correct, but this is not
guaranteed to be consistent).
3. Assign a value to s in your Of method ..
public static int Of(Creator f)
{
Union a = new Union();
a.u2.o = f();
(a.u2.o as Example.C).s = "Hello Hello";
.....
Size.Of will return 56 (which is wrong, and not guaranteed to be
consistent).

Hope you got the picture ;-)
Willy.
"Jon Jagger" <jo*@jaggersoft.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...
Still works for me. As for assumptions - I specifically said it was _not_
guaranteed behaviour but thanks for being explicit about what the reasons
are.
Cheers
Jon Jagger
that a,b would always be layed
a,b being layed out sequentially and threading...these are exactly the
reasons it's not guaranteed behaviour (which I mentioned).

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eF**************@tk2msftngp13.phx.gbl...
Jon,
Your code only works if all members are primitive types, try adding a string
and check the result.
It also assumes that a and b are laid-out sequentially which is a wrong
assumption when multiple threads are creating objects, and is neither
guaranteed by the CLR.

Willy.

"Jon Jagger" <jo*@jaggersoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

I was thinking about how you can only use sizeof to find the size of an unmanaged type. I started to wonder if there was a way to find the

size of
a
managed object and came up with the following. It's not guaranteed

behaviour
but it seems to work. Doesn't lose verifiability either. Enjoy.
Cheers
Jon Jagger

namespace JSL
{
using System.Runtime.InteropServices;

public delegate object Creator();

public static class Size
{
public static int Of(Creator f)
{
Union a = new Union();
a.u2.o = f();
Union b = new Union();
b.u2.o = f();

return System.Math.Abs(a.u1.value - b.u1.value);
}

private struct U1
{
public int value;
}

private struct U2
{
public object o;
}

[StructLayout(LayoutKind.Explicit)]
private struct Union
{
[FieldOffset(0)] public U1 u1;
[FieldOffset(0)] public U2 u2;
}
}
}

namespace Example
{
using JSL;
using System;

class C
{
public C()
{
a = b = c = d = 42;
}

private int a,b,c,d;
}

class App
{
static void Main()
{
Creator c = delegate { return new C(); };
Console.WriteLine(Size.Of(c));
}
}
}



Nov 15 '05 #4
Yup I get 28. I'm not saying this is valid or invalid. Only that I get 28. I
agree that logically I would except it to be 24 (because that is 4 more than
20). But I don't. I get 28. So clearly my logic is incomplete. If I add 2
reference fields the answer is 32. Perhaps there is a one-time hit of 4
bytes if you use any reference type fields? I don't know. I'm simply
exploring. As for adding...
(a.u2.o as Example.C).s = "Hello Hello"; to the code...Well you've changed the code. So don't change it ;-)
Cheers
Jon Jagger

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl... No sure how you are considering the result as valid?
Just modify your code as follows (and run in sequence):

1. Add a unitialized string member to the class, now Size.Of will return 28 (which is wrong).
2. Assign a value to the string in the constructor, lets say string s =
"Hello", the return value will be 56(which is correct, but this is not
guaranteed to be consistent).
3. Assign a value to s in your Of method ..
public static int Of(Creator f)
{
Union a = new Union();
a.u2.o = f();
(a.u2.o as Example.C).s = "Hello Hello";
....
Size.Of will return 56 (which is wrong, and not guaranteed to be
consistent).

Hope you got the picture ;-)
Willy.
"Jon Jagger" <jo*@jaggersoft.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...
Still works for me. As for assumptions - I specifically said it was _not_
guaranteed behaviour but thanks for being explicit about what the reasons are.
Cheers
Jon Jagger
that a,b would always be layed
a,b being layed out sequentially and threading...these are exactly the
reasons it's not guaranteed behaviour (which I mentioned).

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eF**************@tk2msftngp13.phx.gbl...
Jon,
Your code only works if all members are primitive types, try adding a

string
and check the result.
It also assumes that a and b are laid-out sequentially which is a wrong assumption when multiple threads are creating objects, and is neither
guaranteed by the CLR.

Willy.

"Jon Jagger" <jo*@jaggersoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
>
> I was thinking about how you can only use sizeof to find the size of

an > unmanaged type. I started to wonder if there was a way to find the

size
of
a
> managed object and came up with the following. It's not guaranteed
behaviour
> but it seems to work. Doesn't lose verifiability either. Enjoy.
> Cheers
> Jon Jagger
>
>
>
> namespace JSL
> {
> using System.Runtime.InteropServices;
>
> public delegate object Creator();
>
> public static class Size
> {
> public static int Of(Creator f)
> {
> Union a = new Union();
> a.u2.o = f();
> Union b = new Union();
> b.u2.o = f();
>
> return System.Math.Abs(a.u1.value - b.u1.value);
> }
>
> private struct U1
> {
> public int value;
> }
>
> private struct U2
> {
> public object o;
> }
>
> [StructLayout(LayoutKind.Explicit)]
> private struct Union
> {
> [FieldOffset(0)] public U1 u1;
> [FieldOffset(0)] public U2 u2;
> }
> }
> }
>
> namespace Example
> {
> using JSL;
> using System;
>
> class C
> {
> public C()
> {
> a = b = c = d = 42;
> }
>
> private int a,b,c,d;
> }
>
> class App
> {
> static void Main()
> {
> Creator c = delegate { return new C(); };
> Console.WriteLine(Size.Of(c));
> }
> }
> }
>
>



Nov 15 '05 #5
Jon,
Considering the title of this posting, I (wrongly ?) assumed your code was
trying to show us how to find the correct size of an object, which in fact
it does only for a particular case.
I'm trying to show you (and the NG readers) is that there is no managed way
get the size of an arbitrary object.

Note that 24 is the correct size of the object (CLR 1.0 and 1.1) containing
4 int's (4 bytes sync. block info, 4 bytes method/vtable pointer, 4 * 4
bytes (int data members)).
The value of 28 is wrong when adding a string is wrong, because it doesn't
account for the string object when not initialized in the constructor (only
4 bytes are added for the string reference).

Willy.
"Jon Jagger" <jo*@jaggersoft.com> wrote in message
news:uc**************@TK2MSFTNGP09.phx.gbl...
Yup I get 28. I'm not saying this is valid or invalid. Only that I get 28. I agree that logically I would except it to be 24 (because that is 4 more than 20). But I don't. I get 28. So clearly my logic is incomplete. If I add 2
reference fields the answer is 32. Perhaps there is a one-time hit of 4
bytes if you use any reference type fields? I don't know. I'm simply
exploring. As for adding...
(a.u2.o as Example.C).s = "Hello Hello";

to the code...Well you've changed the code. So don't change it ;-)
Cheers
Jon Jagger

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
No sure how you are considering the result as valid?
Just modify your code as follows (and run in sequence):

1. Add a unitialized string member to the class, now Size.Of will return

28
(which is wrong).
2. Assign a value to the string in the constructor, lets say string s =
"Hello", the return value will be 56(which is correct, but this is not
guaranteed to be consistent).
3. Assign a value to s in your Of method ..
public static int Of(Creator f)
{
Union a = new Union();
a.u2.o = f();
(a.u2.o as Example.C).s = "Hello Hello";
....
Size.Of will return 56 (which is wrong, and not guaranteed to be
consistent).

Hope you got the picture ;-)
Willy.
"Jon Jagger" <jo*@jaggersoft.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...
Still works for me. As for assumptions - I specifically said it was _not_ guaranteed behaviour but thanks for being explicit about what the reasons are.
Cheers
Jon Jagger
that a,b would always be layed
a,b being layed out sequentially and threading...these are exactly the
reasons it's not guaranteed behaviour (which I mentioned).

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eF**************@tk2msftngp13.phx.gbl...
> Jon,
> Your code only works if all members are primitive types, try adding a string
> and check the result.
> It also assumes that a and b are laid-out sequentially which is a wrong > assumption when multiple threads are creating objects, and is neither > guaranteed by the CLR.
>
> Willy.
>
> "Jon Jagger" <jo*@jaggersoft.com> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> >
> > I was thinking about how you can only use sizeof to find the size
of an
> > unmanaged type. I started to wonder if there was a way to find the

size
of
> a
> > managed object and came up with the following. It's not guaranteed
> behaviour
> > but it seems to work. Doesn't lose verifiability either. Enjoy.
> > Cheers
> > Jon Jagger
> >
> >
> >
> > namespace JSL
> > {
> > using System.Runtime.InteropServices;
> >
> > public delegate object Creator();
> >
> > public static class Size
> > {
> > public static int Of(Creator f)
> > {
> > Union a = new Union();
> > a.u2.o = f();
> > Union b = new Union();
> > b.u2.o = f();
> >
> > return System.Math.Abs(a.u1.value - b.u1.value);
> > }
> >
> > private struct U1
> > {
> > public int value;
> > }
> >
> > private struct U2
> > {
> > public object o;
> > }
> >
> > [StructLayout(LayoutKind.Explicit)]
> > private struct Union
> > {
> > [FieldOffset(0)] public U1 u1;
> > [FieldOffset(0)] public U2 u2;
> > }
> > }
> > }
> >
> > namespace Example
> > {
> > using JSL;
> > using System;
> >
> > class C
> > {
> > public C()
> > {
> > a = b = c = d = 42;
> > }
> >
> > private int a,b,c,d;
> > }
> >
> > class App
> > {
> > static void Main()
> > {
> > Creator c = delegate { return new C(); };
> > Console.WriteLine(Size.Of(c));
> > }
> > }
> > }
> >
> >
>
>



Nov 15 '05 #6

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
Jon,
Considering the title of this posting, I (wrongly ?) assumed your code was
trying to show us how to find the correct size of an object,
Yes. I should have added a ? to the end of the subject line to match the
"not guaranteed behaviour" content. Apologies.
I'm trying to show you (and the NG readers) is that there is no managed way get the size of an arbitrary object.


Agreed.
Cheers
Jon Jagger
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Poplar Reader | last post by:
Following a crash while using one of out Microsoft Access databases, the following message appears everytime we try to open it. "The Microsoft Jet Database engine could not find the object...
8
by: chippy | last post by:
Hi, I've a VB script that creates a Access object in a word doc. Here is the full script. It works for all but the Export. Which always fails with a 3011 error. If I do the same in Access as a...
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
0
by: Orgil | last post by:
Hi all, I have just writing a program named "DayBook". I am using MS Access 2003 database and Microsoft .NET C# 2005 (with framework 2.0). MS Access 2003, MS .NET C# 2005 and dotNetFramework2.0...
10
by: Sami Ward | last post by:
I have an an MSAccess data project front end linked to an SQL Server Database. I keep getting Error number 7874: database cannot find the object tblname when I look however the table is there. I...
2
by: Chetanhl | last post by:
Hey guyz if i wanna know how to find size of unsigned char*? void func1(unsigned char* str) { int a; a=?? ////////////////now how to store size of str in a???? }
3
by: Salad | last post by:
I'm asking this for curiosity's sake. Is there a code module size limit? I looked at Access Specifications in help and didn't notice a code limit size. For some odd reason I was under the...
2
by: DeelynBaNi | last post by:
HI all..i have this excel file created during export data from sql server. When i try to import this error occured "external data is not in the correct format' something like that. After i did some...
7
by: Tapas Bose | last post by:
Hello. I want to find size of a dynamically allocated array in my following code : #include <iostream> #include <cstdlib> using namespace std; int main() {
5
by: teresedp | last post by:
Hi--I am trying to get an Access database to print labels NOT starting on the first line of labels (to use up partially-used sheets). I was directed to this MS Knowledge Base article:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.