方法调用(这是什么意思)? [英] Method call (what does it mean)?

查看:118
本文介绍了方法调用(这是什么意思)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码是什么意思?



Hi,
what does the following code mean?

static void fu(uint8_t param[static 64])
{
...
for (i = 0; i < 16; ++i)
z[i] = method(param + (4 * i));
...
}





这里是





where is

static uint32_t method(uint8_t *b)
{
return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
}





我理解除方法调用之外的所有内容,是什么意思



I understand everything except method call, what does mean

param+(4*i)

?程序是关于Salsa20。



谢谢

? Program it is about Salsa20.

Thanks

推荐答案

数组的名称是指向数组的第一个元素(并且自从考虑过C ++以来,它是原始C规范的一部分),这意味着当你写:

The name of an array is a pointer to the first element of the array (and has been since before C++ was considered, it was part of the original C specification) which means that when you write:
nameArray + offset

你正在为一个指针添加一个值并为另一个元素生成另一个指针值。

例如:

you are adding a value to a pointer and generating another pointer value to a different element.
For example:

int ar[10];
int *p0 = ar;
int *p1 = ar + 1;

p0指向数组的第一个元素,p1到第二个元素。

所有代码所做的就是将指针传递给数组的i-th * 4元素。

实际上它的作用是依次将指针传递给每个32位整数,以便它可以被转换。



这相当于说:

p0 points to the first element of the array, and p1 to the second.
All your code is doing is passing a pointer to the "i-th * 4" element of the array.
And what that actually does is pass a pointer to each 32 bit integer in turn so it can be converted.

It's the equivalent of saying:

z[i] = method(&(param[4 * i]));







for循环的第一轮是i = 0而z [0] =方法(param) for循环的第二轮中的[4 *])是i = 1且z [1] =方法(param [4 * 1]),依此类推......在for循环的最后一轮中,i = 15并且z [15] =方法(param [4 * 15])。在每一轮中,mathod()的参数只有uint [i],但是method()需要4个uint的数组?



我在谈论方法()的输入变量的大小。谢谢






OK - 每次输入method()时,它都会获得一个指向8位整数的指针,每次指针移动4个字节。

4字节== 4 * 8位== 32位==一个32位整数

所以方法是将四个8位字节重新组合成一个32位整数。

从理论上讲,只需投射指针就可以做到更简单:




"In first round of the for loop is i=0 and z[0]=method(param[4*]) in second round of the for loop is i=1 and z[1]=method(param[4*1]) and so on... At the last round of the for loop is i=15 and z[15]=method(param[4*15]). In each round is the parameter of the mathod() only uint[i], but method() requires array of the 4 uints?

I'm talking about the size of the input variables of method(). Thanks"



OK - each time you enter method(), it gets a pointer to an 8 bit integer, and each time that pointer has moved by four bytes.
4 bytes == 4 * 8 bits == 32 bits == one 32bit integer
So what method is doing is reassembling the four 8bit bytes into a single 32bit integer.
In theory, you can do this a lot simpler, just by casting the pointer:

static uint32_t method(uint8_t *b)
    {
    return *((uint32*)b);
    }

但是......有两个潜在的问题。

第一个是32位整数指针只有在8位指针中的地址才有效在32位边界上对齐:地址必须以00二进制结束。如果它不是(并且没有)那么你的遗嘱会得到奇怪的结果或者应用程序错误。



第二种是有两种转换方式32位数到4位8位值,具体取决于硬件如何组织它们:little endian和big endian,这取决于处理器。 小端系统将32位数的最低有效8位存储在最低编号的字节地址中,大端系统将最高有效位存储在最低编号的字节地址中。英特尔和AMD PC处理器是小端,但不是所有处理器都是,所以你不能总是假设来自另一个系统的数据将采用你期望的格式。



您使用的代码并不关心系统的字节序,它会手动将其从小端数据转换为32位整数,而不管它运行的系统如何。所以它得到一个至少有意义的字节的poiter,并且它在输出的低8位中。然后下一个字节进入下一个8位,依此类推。

因此,如果您的输入数据是十六进制字节0x12,0x34,0x56,0x78,则字节进入32位整数以产生十六进制值:0x78563412



看看我的意思?

BUT...there are two potential problems with that.
The first is that the 32bit integer pointer will only work if the address in the 8bit pointer is aligned on a 32bit boundary: the address must end with 00 binary. If it isn't (and doesn't) then your will either get odd results, or an application error.

The second is that there are two ways of converting a 32bit number to four 8bit values, depending on how your hardware organises them: "little endian" and "big endian" and that depends on the processor. "Little endian" systems store the least significant 8 bits of the 32 bit number in the lowest numbered byte address, "big endian" systems store the most significant digits in the lowest numbered byte address. Intel and AMD PC processors are "little endian", but not all processors are, so you can't always assume that data from another system will be in the format you expect.

The code you are using doesn't care about the "endianness" of your system, it manually converts it from "little endian" data to a 32bit integer regardless of the system it runs on. So it gets a poiter to teh leastsignificant byte, and buts it in the low 8 bits of the output. Then the next byte goes in the next 8 bits and so on.
So if your input data was in hex bytes 0x12, 0x34, 0x56, 0x78 then the bytes go into an 32 bit integer to produce a hex value: 0x78563412

See what I mean?


这篇关于方法调用(这是什么意思)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆