真奇怪的对话 [英] Really Weird Conversation

查看:56
本文介绍了真奇怪的对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当朋友,Corbessero教授

和我想出这个时,我把东西寄到了邮件列表。也许你可以帮忙解决这个问题,或者加上

其他值得知道的事情?也许应该将它添加到常见问题解答中进行进一步的参考...... :)

I was posting stuff to a mailing list when a friend, Prof. Corbessero
and I came up with this one. Perhaps you can help resolve this, or add
anything else worth knowing?? Maybe it should be added to the FAQ for
further reference... :)


啊。这是我的区域... C以一种名为column
major的形式存储数组。顺序,这实际上意味着行首先出现(不要问!)。
所以,这就是3x4阵列在内存中的样子

-------- -------------------------------------------------- ---------------
| 0,0 | 0,1 | 0,2 | 0,3 | 1,0 | 1,1 | 1,2 | 1,3 | 2,0 | 2,1 | 2,2 | 2,3 |
------------------------------------------ -------------------------------

将数组传递给函数时,就是这样真正传递的是第一个元素的
地址。考虑到上面的布局,只要函数知道行的长度(即
列的数量)和数组的基本类型的大小,它总是可以
找出任何一个元素的地址。由于C没有对数组进行边界检查,因此不需要知道行数。
因为它知道每行的长度和每个元素的大小,所以它
可以跳过遍历行,直到找到您要求的元素。


边界检查缺乏整齐。似乎C是

编程语言的Barry Goldwater。就像我说的那样,传递数组

符号

(使用太多的emacs lisp)到''void foo(char * bar)''意味着我有



''手动''通过。我想我可以使用#define,所以如果你知道我的意思,程序

知道

这些部分被截断了。在

函数

foo中,如果我要说''printf("%s",(bar + 1));''我会得到的'' OO ''。开头

下来

到数组中的正确位置,超过结束''Foo''的空值,并且

我会

获得下一个字符序列,我打印并获得''Bar''。所以在其他

字样中,

使用''char * bar''样式意味着我必须通过,好像

数组

是一串香肠,捏是空的。

如果你想要位置2,1
编译器生成一个表达式来跳过两行,
将是
2(行)x 4(每行中的东西)x W(基数
类型的宽度字节)
然后编译器只跳过一个在该行中找到1列


在一般形式中,对于N维数组,您需要指定除第一个之外的所有维度。 />
例如,

双x [3] [4] ... [n]

应该通过

void f(double a [] [3] [4] ... [n]){}


看,当我使用它时,指针设置为我可以那种

对待它就像原始阵列一样对待b $ b,而不是上面的''香肠'时尚。试试

一些

printf'来搞定它。


我不确定你在问什么(b )。功能调用并不昂贵。


取决于!如果你一次又一次地这样做,那就加起来了。一个宏

可能会起作用,但是内存膨胀而不是CPU压力。

东西

你想要快速运行,或者因为你必须(老板

说),

你想要(你做的像FluxBox),或者你想要

看看

kewl在所有FreeBSD和GNU / Linux用户面前更公平的性别

(为什么?duh),可能想要这样的优化。有时guddy nuff aint

guddy nuff。

你没有通过整个阵列。


请参阅有关sizeof()使用的信息。我知道它是一个指针。

什么

我问的是:如何在函数中定义指向
$ b的指针$ b多维数组,我可以把它当作一个多维的

数组。

如果你还没有理解它,这里是''香肠''和''功能''方法:


香肠:


printf("%s",(foo + 1));


收益''oo''。


功能:


printf("%s" ;,(foo + 1));


收益''酒吧''。


printf("%s",(( foo + 1)+ 1));


收益率''ar''。 (实际上,我在最后一位的printf语法可能是错误的。$

可能会在这个Windoze框中无意识地将非法拷贝的Visual C弄清楚

但是我没有闯入人们的院子里去偷一些臭味,

吃掉的狗

sh * t所以我不会非法获取VC的副本,相当于

,我认为我不像M $那样盗窃。)


实际上定义一个数组,你必须指定所有的维度,以便编译器可以生成代码来分配正确的内存量。

Ahh. This is my area... C stores arrays in a form called "column
major" order, which actually means the rows come first (don''t ask!).
So, here is what a 3x4 array looks like in memory

-------------------------------------------------------------------------
| 0,0 | 0,1 | 0,2 | 0,3 | 1,0 | 1,1 | 1,2 | 1,3 | 2,0 | 2,1 | 2,2 | 2,3 |
-------------------------------------------------------------------------

When you pass an array to a function, all that is really passed is the
address of the first element. With the above layout in mind, as long
as the function knows how long the rows are (ie, the number of
columns) and the size of the base type of the array, it can always
figure out the address of any one element. Since C does not have
bounds checking on arrays, it doesn''t need to know the number of rows.
Since it knows the length of each row and the size of each element, it
can "skip" over the rows until it finds the element you are
requesting.
The bounding checks lack is neat. It seems C is the Barry Goldwater of
programming languages. Like like I was saying, passing the array
symbol
(using too much emacs lisp) to ''void foo(char *bar)'' means that I have
to
''manually'' go thru. I suppose I could use a #define, so the program
knows
where the sections are cut off, if you know what I mean. In the
function
foo, if I were to say ''printf("%s", (bar + 1));'' I''d get ''oo''. Head
down
to the right spot in the array, past the null that ends ''Foo'', and
I''ll
get the next sequence of chars, I printf and get ''Bar''. So in other
words,
using the ''char *bar'' style means I have to go thru, as though the
array
was a a strand of sausages, with the pinches being nulls.

if you want position 2,1
the compiler generates an expression to skip over two rows, which
would be
2 (rows) x 4 (things in each row) x W (width in bytes of the base
type)
then the compiler just skips over one more spot to find the 1 column
in that row.
In the general form, for an N dimension array, you need to specify all
the dimensions except the first.

e.g,

double x[3][4]...[n]

should be passed as

void f(double a[][3][4]...[n]) { }
See, when I use that, the pointer is set up so that I can kind of
treat it
like the original array, and not in the ''sausage'' fashion above. Try
some
printf''s to grok it.


I am not sure what you are asking in part (b). Function calls are not
that expensive.
Depends! If you are doing them again and again, that adds up. A macro
might work, but that is bloat in memory as opposed to CPU strain.
Stuff
that you want to run real fast, either because you have to (boss
said),
you want to (you are doing something like FluxBox), or you want to
look
kewl in front of all the FreeBSD and GNU/Linux users of the fairer sex
(why? duh), might want such an optimisation. Sometimes guddy nuff aint
guddy nuff.
You are not passing the whole array.
See the bit about the sizeof() use. I was aware that it is a pointer.
What
I am asking is: how can I define, within a function, a pointer to a
multidimensional array, that I can treat like a multi-dimensional
array.
If ya don''t grok it yet, here are the ''sausage'' and ''function'' method:

Sausage:

printf("%s", (foo + 1));

Yields ''oo''.

Function:

printf("%s", (foo + 1));

Yields ''Bar''.

printf("%s", ((foo + 1) + 1));

Yields ''ar''. (Actually, my printf syntax in the last bit might be
wrong. I
could nonchalantly d''l an illegal copy of Visual C at this Windoze box
but I don''t trespass into people''s yards to steal pieces of stinking,
fly-eaten dog
sh*t, so I won''t illegally grab a copy of VC which is equivalent
thereto, and I think I''m above theft unlike M$.)

When actually defining an array, you must specify all the dimensions so the compiler
can generate code to allocate the correct amount of memory.




嗯,是的。我有点想到了''。 :)


-

对女孩不说的十大事情,#3:


" ;与大多数emacs模式相比,这个你为我定义的键映射

真的

咬了袋子。也许你可以做一些调整和

字节编译

又一次你的eisp来源?我的双手永远不会变得如此粘腻

emacs,

为什么会这样?你应该尝试自己冷启动,修复所有lisp

问题......"


....和#8:


在第二十四世纪的斯特拉特经历!!!!"



Well, yeah. I kinda figured that''n. :)

--
Top Ten Things Not to Say to a Girl, #3:

"compared to most emacs modes, this keymap you have defined for me
really
bites the bag. perhaps you could make some adjustments and
byte-compile
your e lisp source again? and my hands never got so sticky using
emacs,
why is that? you should try cold booting yourself, that fixes all lisp
problems..."

....and #8:

"STRAITER EMPY IN THE TWENTY-FOURTH-AND-A-HALF CENTURY!!!!"

推荐答案

。)

实际定义数组时,必须指定所有维度,以便编译器可以生成代码来分配正确的内存量。
.)

When actually defining an array, you must specify all the dimensions so the compiler
can generate code to allocate the correct amount of memory.




嗯,是的。我有点想到了''。 :)


-

对女孩不说的十大事情,#3:


" ;与大多数emacs模式相比,这个你为我定义的键映射

真的

咬了袋子。也许你可以做一些调整和

字节编译

又一次你的eisp来源?我的双手永远不会变得如此粘腻

emacs,

为什么会这样?你应该尝试自己冷启动,修复所有lisp

问题......"


....和#8:


在第二十四世纪的斯特拉特经历!!!!"



Well, yeah. I kinda figured that''n. :)

--
Top Ten Things Not to Say to a Girl, #3:

"compared to most emacs modes, this keymap you have defined for me
really
bites the bag. perhaps you could make some adjustments and
byte-compile
your e lisp source again? and my hands never got so sticky using
emacs,
why is that? you should try cold booting yourself, that fixes all lisp
problems..."

....and #8:

"STRAITER EMPY IN THE TWENTY-FOURTH-AND-A-HALF CENTURY!!!!"


" Mr。狂暴"写道:
"Mr. Berserker" wrote:

当朋友,Corbessero教授和我提出这个问题时,我正在将信息发布到邮件列表中。也许你可以帮忙解决这个问题,或者添加其他值得了解的东西?也许它应该被添加到常见问题解答中进一步参考...... :)
[...]

I was posting stuff to a mailing list when a friend, Prof. Corbessero
and I came up with this one. Perhaps you can help resolve this, or add
anything else worth knowing?? Maybe it should be added to the FAQ for
further reference... :)
[...]




我看了看,在你的帖子中的任何地方都找不到问题

。一些技术细节,是的。

有些幼稚的事,是的。一些片段来自

一定是一个更大的解释性背景,是的 -

但毫无疑问。


唯一合适的我能想到的答案是:


-
Er * ********@sun.com


先生。 Berserker< El *********** @ yahoo.com>写道:

[大片...我认为*这是问题..]
Mr. Berserker <El***********@yahoo.com> wrote:
[big snip... I *think* this is the question..]
我要问的是:我如何在一个函数中定义,一个指向多维数组的指针,我可以像对待多维数组一样对待。
What I am asking is: how can I define, within a function, a pointer
to a multidimensional array, that I can treat like a
multi-dimensional array.




鉴于:


int(* foo)[2] [3] [10];


和:


int bar [ 15] [2] [3] [10];

int baz [300] [2] [3] [10];


你可以设置:


foo = bar;


和访问栏[a] [b] [c] [d]如foo [a] [ b] [c] [d],然后设置


foo = baz;


并访问baz [a] [b] [c ] [d]如foo [a] [b] [c] [d]。


这就是你追求的目标吗?


- 凯文。



Given:

int (*foo)[2][3][10];

and:

int bar[15][2][3][10];
int baz[300][2][3][10];

You can set:

foo = bar;

and access bar[a][b][c][d] as foo[a][b][c][d], then set

foo = baz;

and access baz[a][b][c][d] as foo[a][b][c][d].

Is that what you''re after?

- Kevin.


这篇关于真奇怪的对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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