Search code examples
c#c++porting

Can someone tell me what this crazy c++ statement means in C#?


First off, no I am not a student...just a C# guy porting a C++ library.

What do these two crazy lines mean? What are they equivalent to in C#? I'm mostly concerned with the size_t and sizeof. Not concerned about static_cast or assert..I know how to deal with those.

size_t Index = static_cast<size_t>((y - 1620) / 2);
assert(Index < sizeof(DeltaTTable)/sizeof(double));

y is a double and DeltaTTable is a double[]. Thanks in advance!


Solution

  • size_t is a typedef for an unsigned integer type. It is used for sizes of things, and may be 32 or 64 bits in size. The particular size of a size_t is implementation defined, but it is unsigned.

    I suppose in C# you could use a 64-bit unsigned integer type.

    All sizeof does is return the size in bytes of a C++ type. Every type takes up a certain quantity of room, and sizeof returns that size.

    What your code is doing is computing the number of doubles (64-bit floats) that the DeltaTTable takes up. Essentially, it's ensuring that the table is larger than some size based on y, whatever that is.

    There is no equivalent of sizeof in C#, nor does it need it. There is no reason for you to port this code to C#.