Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the size of var?

Tags:

c#

I Have a variable declared as follows:

var a = 99494;

Then I used the following to determine the size of the variable in bytes:

Marshal.SizeOf(a)

Does it get the actual size of memory occupied by this value ?

like image 362
sajis997 Avatar asked Sep 11 '18 07:09

sajis997


People also ask

How do you determine the size of a variable?

The size of a variable depends on its type, and C++ has a very convenient operator called sizeof that tells you the size in bytes of a variable or a type. The usage of sizeof is simple. To determine the size of an integer, you invoke sizeof with parameter int (the type) as demonstrated by Listing 3.5.

How to check size of variable in c#?

To get the size of a variable, sizeof is used. // without using sizeof byte[] dataBytes = BitConverter. GetBytes(x); int d = dataBytes.

What is a VAR variable?

The var statement declares a variable. Variables are containers for storing information. Creating a variable in JavaScript is called "declaring" a variable: var carName; After the declaration, the variable is empty (it has no value).

How do you find the size of a variable?

Show activity on this post. The idea is to use pointer arithmetic ( (& (var)+1)) to determine the offset of the variable, and then subtract the original address of the variable, yielding its size. For example, if you have an int16_t i variable located at 0x0002, you would be subtracting 0x0002 from 0x0006, thereby obtaining 0x4 or 4 bytes.

How to find the size of the variable'i'without sizeof operator?

Now can I find the size of the variable 'i' without sizeof operator? The idea is to use pointer arithmetic ( (& (var)+1)) to determine the offset of the variable, and then subtract the original address of the variable, yielding its size.

How to find the size of an arbitrary variable at runtime?

I wonder what that means. To find the size of an arbitrary variable, x, at runtime you can use Marshal.SizeOf: As mentioned by dtb, this function returns the size of the variable after marshalling, but in my experience that is usually the size you want, as in a pure managed environment the size of a variable is of little interest.

What does the Var measure show?

The VaR measurement shows a normal distribution of past losses. The measure is often applied to an investment portfolio for which the calculation gives a confidence interval about the likelihood of exceeding a certain loss threshold.


2 Answers

Does it get the actual size of memory occupied by this value ?

Yes. In this case it is fairly simple, since the var is an int. It will always yield the same value (4). (var isn't a dynamic type, it is determined on compile time.)

like image 188
Patrick Hofman Avatar answered Nov 16 '22 00:11

Patrick Hofman


Yes, var is just a compile time trick so the compiler will decide it's type at compile time. I wouldn't recommend using it besides obvious and very long class names. e.g var x = new BigBigBigClass(). It makes your code less readable to others and makes it easier to make mistakes. C# is a statically typed language and while it takes a few more seconds to declare types, it pays off when you aren't accidentally mixing classes

Since the compiler will put x as an int, it should return 32 bits (4 bytes) as that's the size of a standard int

like image 45
Prodigle Avatar answered Nov 16 '22 01:11

Prodigle