简体   繁体   中英

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!

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#.

The bad news first you can't do that in C#. There's no static cast only dynamic casts. However the good news is it doesn't matter.

The two lines of code is asserting that the index is in bounds of the table so that the code won't accidentally read some arbitrary memory location. The CLR takes care of that for you. So when porting just ignore those lines they are automatically there for you any ways.

Of course this is based on an assumption based on the pattern of the code. There's no information on what Y represents and how Index is used

sizeOf calculates how much memory in bytes the DeltaTable type takes. There is not equivalent to calculate the size like this in c# AFAIK.

I guess size_t much be a struct type in C++ code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM