奇怪的代码。 [英] weird code.
问题描述
1:float(* data)[16384];
2:data = malloc(31 * sizeof(data [0]));
3:数据[i] [j] = VARIABLE;
问题1:变量数据是浮点指针吗?
问题2:当[使用16384]。这是否意味着它是一个
浮点指针的数组?这是什么意思?
问题3:或者是否意味着有16384个花车分配了
指针数据指向那些16384的头部浮点数?
问题4:它将malloc 31 * 16384浮点数...我认为这是真的
基于程序如何使用它。但不应该是mallocing
31 * sizeof(浮动*)?
问题5:然后代码开始使用数据作为多维
数组。什么时候它成为一个多维数组?!怎么回事?
变成多维?!
1: float (*data)[16384];
2: data = malloc(31 * sizeof(data[0]));
3: data[i][j] = VARIABLE;
Question 1: The variable data is a float pointer?
Question 2: When the [16384] is used. Does that mean it is an array of
float pointers? What does it mean?
Question 3: Or does it mean that there are 16384 floats allocated with
the pointer data that points to the head of those 16384 floats?
Question 4: It will malloc 31*16384 floats... I assume this is true
based on how the program uses it. But shouldn''t it be mallocing
31*sizeof(float*)?
Question 5: Then the code starts using data as a multidimensional
array. When did it become a multidimensional array?! and how did it
become multidimensional?!
推荐答案
syn1kk写道:
syn1kk wrote:
1:float(* data)[16384];
2:data = malloc(31 * sizeof(data [0]));
3:data [i] [j] = VARIABLE;
问题1:变量数据是浮点指针吗?
问题2:使用[16384]时。这是否意味着它是一个
浮点指针的数组?这是什么意思?
问题3:或者是否意味着有16384个花车分配了
指针数据指向那些16384的头部浮动?
1: float (*data)[16384];
2: data = malloc(31 * sizeof(data[0]));
3: data[i][j] = VARIABLE;
Question 1: The variable data is a float pointer?
Question 2: When the [16384] is used. Does that mean it is an array of
float pointers? What does it mean?
Question 3: Or does it mean that there are 16384 floats allocated with
the pointer data that points to the head of those 16384 floats?
data是指向16384个浮点数组的指针。
data is a pointer to an array of 16384 floats.
问题4:它将是malloc 31 * 16384浮动...我认为这是真的
基于程序如何使用它。但是不应该是mallocing
31 * sizeof(浮动*)?
Question 4: It will malloc 31*16384 floats... I assume this is true
based on how the program uses it. But shouldn''t it be mallocing
31*sizeof(float*)?
鉴于它是一个指向数组的指针,你不需要31个浮动*。
31个16384浮点数组,确定。
Given it''s a pointer to an array, you don''t want 31 float*.
31 arrays of 16384 floats, sure.
问题5:然后代码开始使用数据作为多维
数组。什么时候它成为一个多维数组?!它是如何变成多维的?
变成多维的?!
Question 5: Then the code starts using data as a multidimensional
array. When did it become a multidimensional array?! and how did it
become multidimensional?!
''data''是指向数组的指针。你已经分配了31个这样的阵列空间,所以data [0]是16384个浮点数的数组,
数据[1]下一个数组,以及等等。
''data'' is a pointer to an array. You''ve allocated room for 31
such arrays, so data[0] is the 1. array of 16384 floats,
data[1] the next array, and so on.
syn1kk写道:
syn1kk wrote:
1:float(* data)[16384];
2:data = malloc(31 * sizeof(data [0]));
3:data [i] [j] = VARIABLE;
问题1:变量数据是浮点指针吗?
1: float (*data)[16384];
2: data = malloc(31 * sizeof(data[0]));
3: data[i][j] = VARIABLE;
Question 1: The variable data is a float pointer?
" data"是指向浮点的16384个元素的数组的指针。 parens
是不必要的。
"data" is a pointer to an array of 16384 elements of float. The parens
are unnecessary.
问题2:当使用[16384]时。这是否意味着它是一个
浮点指针的数组?这是什么意思?
Question 2: When the [16384] is used. Does that mean it is an array of
float pointers? What does it mean?
" data"是一个指向浮点数为16384个元素的数组的指针。
"data" is a pointer to an array of 16384 elements of float.
问题3:或者是否意味着有16384个浮点数分配了
指向那些16384漂浮物的头部的指针数据?
Question 3: Or does it mean that there are 16384 floats allocated with
the pointer data that points to the head of those 16384 floats?
" data"是一个指向浮动的16384个元素数组的指针。
"data" is a pointer to an array of 16384 elements of float.
问题4:它将malloc 31 * 16384浮点数...我认为这是真的
基于程序如何使用它。但是不应该是mallocing
31 * sizeof(浮动*)?
Question 4: It will malloc 31*16384 floats... I assume this is true
based on how the program uses it. But shouldn''t it be mallocing
31*sizeof(float*)?
有很多东西应该在这段代码中,以
开头几百个字的评论,程序员可以自己解释一下
。他确实有一些解释要做。
There are a lot of things that "should be" in this code, beginning with
a few hundred words of comments where the programmer can explain
himself. He does have some explaining to do.
>
问题5:然后代码开始使用数据作为多维
数组。什么时候它成为一个多维数组?!它是如何变成多维的?$
>
Question 5: Then the code starts using data as a multidimensional
array. When did it become a multidimensional array?! and how did it
become multidimensional?!
数组表示法没有什么神奇之处。它只是另一种方式
表示你指针的偏移量。
There''s nothing magical about the array notation. It''s just another way
of expressing an offset from your pointer.
syn1kk写于08/23/06 13:55,:
syn1kk wrote On 08/23/06 13:55,:
1:float(* data)[16384];
2: data = malloc(31 * sizeof(data [0]));
3:data [i] [j] = VARIABLE;
问题1:变量数据是浮点指针?
1: float (*data)[16384];
2: data = malloc(31 * sizeof(data[0]));
3: data[i][j] = VARIABLE;
Question 1: The variable data is a float pointer?
不,它是一个浮动[16384]指针。 事物 'data''
指向(当它被指向某个东西时)将是一个由16384个浮点对象组成的数组。 (见下文。)
No, it is a float[16384] pointer. The "thing" that `data''
points to (when it''s made to point to something) will be an
array of 16384 float objects. (See below.)
问题2:使用[16384]时。这是否意味着它是一个
浮点指针的数组?这是什么意思?
Question 2: When the [16384] is used. Does that mean it is an array of
float pointers? What does it mean?
一个浮点指针数组是`float * data [16384];'',
没有括号。请记住声明模仿使用
规则(每个人最终都会对此表示不满)。在
无括号的情况下,声明表示
表达式`* data [i]''是一个浮点数。看着你方便的C运算符
优先表你会发现[]绑定比绑定更紧密 *,
所以`* data [i]''与`*(data [i])''的含义相同。这说
来获取数组的第i个元素,然后使用那个
元素作为指针。那个过程必须产生一个浮点数,所以通过向后推理我们发现data [i]必须是指向
float的指针,并且该数据本身必须是一个数组这样的指示。
......但这不是你的令人费解的代码所说的;它有一些括号改变了含义。
让我们以前面的
段落中的方式执行
实际代码 - 我们已经有了一些练习,所以现在我们应该好吗?b $ b相当不错,对吧?
浮动(*数据)[16384];''表示
形式`(* data)[i]''是一个浮点数。这次我们不需要为我们的优先表看一下
,因为括号已经显示了我们用什么比特来表示什么。表达式说使用
`data''作为指针 - 好吧,让我们暂时说某事
。一旦我们得到了某事, `[i]''
位意味着将它视为数组并获得第i个元素。
该元素必须是浮点数,所以我们推理像以前一样倒退:
如果某事[i]是浮点数,那么某事物必须是一个
浮点数组。如果* data是某事物,则数据必须是b指向某事物的指针。也就是说,一个指向数组的指针
浮点数。
指向数组的指针!=指针数组。
An array of float pointers would be `float *data[16384];'',
without the parentheses. Remember the "declaration mimics use"
rule (everybody scratches his head over it eventually). In the
no-parentheses case, the declaration says that an expression of
the form `*data[i]'' is a float. Looking at your handy C operator
precedence table you''ll find that [] "binds more tightly than" *,
so `*data[i]'' means the same thing as `*(data[i])''. This says
to fetch the i''th element of the array, and then to use that
element as a pointer. That process must produce a float, so by
reasoning backwards we find that data[i] must be a pointer to
float, and that data itself must be an array of such pointers.
... but that''s not what your puzzling code says; it''s got
some parentheses that change the meaning. Let''s walk through
the actual code in the same way we just did in the previous
paragraph -- we''ve had some practice, so by now we ought to
be pretty good at this, right?
`float (*data)[16384];'' says that an expression of the
form `(*data)[i]'' is a float. This time we don''t need to look
for our precedence table, because the parentheses have already
shown us what bits go with what. The expression says to use
`data'' as a pointer to -- well, let''s just say "something"
for the moment. Once we''ve got the "something," the `[i]''
bit means to treat it as an array and get the i''th element.
That element must be a float, so we reason backwards as before:
if "something"[i] is a float, then "something" must be an
array of float. If *data is a "something", then `data'' must
be a pointer to "something," that is, a pointer to an array
of float.
Pointer to array != array of pointers.
问题3:或者是否意味着有16384个花车分配了
指针数据指向那些16384花车的头部?
Question 3: Or does it mean that there are 16384 floats allocated with
the pointer data that points to the head of those 16384 floats?
我想*答案是是,但我不完全确定你的问题是什么。 (见上文。)
I *think* the answer is "Yes," but I''m not entirely sure
of what your question is. (See above.)
问题4:它将malloc 31 * 16384浮点数...我认为这是真的
基于如何该程序使用它。但是不应该是mallocing
31 * sizeof(浮动*)?
Question 4: It will malloc 31*16384 floats... I assume this is true
based on how the program uses it. But shouldn''t it be mallocing
31*sizeof(float*)?
第一部分是正确的,但它可能更有启发性
用一点点不同来说:malloc()被要求
足够的记忆力,适用于31岁的某事。其中每个东西是
一系列16384浮点数。总共有31 * 16384浮动,因为
你说,并且malloc()只知道总大小而且没有任何关于内存如何被细分的
,所以真的没有
的区别。但如果*你*注意细分,
你可以更容易地整理出这样的代码。
规则:如果`p''是指向任何对象类型的指针,任何类型
无论如何,那么`sizeof * p''是需要的内存量
来存储该类型的一个实例。永远。
让'我们应用规则:我们知道`data''指向一个
类的对象,即一个16384的数组彩车。这样一个数组需要多少内存?b $ b内存?我们可以把它计算为'16384 * sizeof(float)'的
,但是我们可以更方便地计算它作为`sizeof * data''(或`sizeof data' [0] '')。为什么
这个更方便?因为当我们应该写'sizeof(float)'',
或`sizeof时,我们永远不会滑倒并写下
`sizeof(float *)'' (struct msghead)''而不是`sizeof(struct msgbody)''。
我们只写`sizeof * data''和编译器(知道什么是
`数据''指向)为我们填写正确的数字,减少
我们制造愚蠢错误的机会。
如果`sizeof data [0]' '是一个所需的房间数量b / b $ b&b;什么,我们需要为其中的三十一个多少钱?
`31 * sizeof data [0]'',显然。再一次,我们可以写出
`31 * 16384 * sizeof(float)'',但是再次它更方便
并且更不容易出错编译器完成工作。
The first part is right, but it might be more instructive
to phrase it a little differently: malloc() is being asked for
enough memory for 31 "somethings," where each "something" is
an array of 16384 floats. That''s 31*16384 floats in all, as
you say, and malloc() knows only the total size and nothing
about how the memory will be subdivided, so there''s really no
difference. But if *you* pay attention to the subdivisions,
you may have an easier time sorting out code like this.
Rule: If `p'' is a pointer to any object type, any type
whatever, then `sizeof *p'' is the amount of memory required
to store one instance of that type. Always.
Let''s apply the rule: We know that `data'' points to a
kind of object, namely, an array of 16384 floats. How much
memory would such an array require? We could calculate it
as `16384 * sizeof(float)'', but we could calculate it more
conveniently as `sizeof *data'' (or `sizeof data[0]''). Why is
this more convenient? Because we''ll never slip up and write
`sizeof(float*)'' when we should have written `sizeof(float)'',
or `sizeof(struct msghead)'' instead of `sizeof(struct msgbody)''.
We just write `sizeof *data'' and the compiler (knowing what
`data'' points to) fills in the correct number for us, reducing
our chance of making a silly error.
If `sizeof data[0]'' is the amount of room needed for one
"something," how much do we need for thirty-one of them?
`31 * sizeof data[0]'', obviously. Again, we could have written
`31 * 16384 * sizeof(float)'', but again it''s more convenient
and less error-prone to let the compiler do the work.
问题5:然后代码开始使用数据作为多维
数组。什么时候它成为一个多维数组?!它是如何变成多维的?
变成多维的?!
Question 5: Then the code starts using data as a multidimensional
array. When did it become a multidimensional array?! and how did it
become multidimensional?!
当你有一个指向某个东西的指针时,你可以把那个
指针视为那些东西的数组的开头,对吗?
给定`char * p =" Hello";''你可以写p [0],p [1],等等
on to refer to H,e等等。清楚?
好的:数据是指向某个东西的指针,我们已经为这些事情中的三十一个请求了
空间。所以每个数据[0],
数据[1],...,数据[30]都是其中一个,对吧?
和每个人都是16384个花车阵列。
你能用阵列做什么?您可以使用下标在其中一个元素上获得
。因此,如果数据[3]是一个16384
浮点数组,那么数据[3] [42]就是其中一个浮点数。
希望这有助于。你不是一个人被这个混淆了吗?至少有一个常规的在这个新闻组谁是'b
$ b无法理解它。
-
Er ********* @ sun.com
When you''ve got a pointer to something, you can treat that
pointer as the start of an array of those somethings, right?
Given `char *p = "Hello";'' you can write p[0], p[1], and so
on to refer to the ''H'', the ''e'', and so on. Clear?
Okay: `data'' is a pointer to something, and we''ve requested
space for thirty-one of those somethings. So each of data[0],
data[1], ..., data[30] is one of the somethings, right?
And each of those somethings is an array of 16384 floats.
What can you do with an array? You can use a subscript to get
at one of its elements. So if data[3] is an array of 16384
floats, then data[3][42] is one of those floats.
Hope this helps. You''re not alone in being confused by
this; there''s at least one "regular" on this newsgroup who''s
failed to understand it.
--
Er*********@sun.com
这篇关于奇怪的代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!