本文介绍了低级指针与数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不可思议,我对ASM和编译器构造几乎一无所知,

虽然我知道指针与

数组之间的语法差异,但我并不知道其中:



(是的,它是livejournal,但它是glibc维护者的期刊)


我不确定我真的理解指针之间的这种低级差异

和数组,我没有时间学习ASM和编译器

构造(我只是一个爱好编码器)。所以我想问你一些关于这个的问题:我在我的程序中经常使用的一个构造是一个

常量字符串的数组,在阅读之后我想知道是否:


const char foo_array [N_ENTRIES] [ENTRY_SIZE] = {

" foo",

" bar"

};


转换为比这更有效的装配:


const char * foo_array [N_ENTRIES] = {

" foo",

" bar"

};


有人可能争论这是一个特定于平台的问题,但是我会想知道这个问题的答案,以及最有可能知道的人们。

如何编写高效的C代码/ C编译器如何工作最有可能是这里找到的b $ b;)


copx

Unforuntately, I know next to nothing about ASM and compiler construction,
and while I was aware of the syntactic differences between pointers and
arrays, I was not aware of this:

http://udrepper.livejournal.com/13851.html
(Yes, it is livejournal, but it is the journal of the glibc maintainer)

I am not sure I really understand this low-level difference between pointers
and arrays, and I do not have the time to learn ASM and compiler
construction (I am just a hobby coder). So I would like to ask you something
about this: one construct I use very often in my programs is an array of
constant strings, and after reading this I wonder whether:

const char foo_array[N_ENTRIES][ENTRY_SIZE] = {
"foo",
"bar"
};

translates to more efficient assembly than this:

const char * foo_array[N_ENTRIES] = {
"foo",
"bar"
};

One could argue that this is a platform specific question, but I would
really like to know the answer to this, and the people most likely to know
how to write efficient C code / how C compilers work are most likely to be
found here ;)

copx

推荐答案



这里是指针:------>


这里是一个数组:+ --- + --- + --- + --- + --- + --- + --- + --- + --- +

| | | | | | | | | |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- +


它们看起来不完全不同吗?

Here''s a pointer: ------>

Here''s an array: +---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+

Do they not look completely different?



它们是具有不同含义的不同结构,所以问题

的效率并没有真正产生(快速 - 效率更高,一个

微波炉或电话?)。


第一个给你字符串存储,你可以修改的内容

闲暇时。第二个只给你一些指针,

目前指向你不能改变的字符串文字。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999


电子邮件:rjh在上述域名中, - www。

They are different constructs with different meanings, so the question
of efficiency doesn''t really arise (quick - which is more efficient, a
microwave oven or a telephone?).

The first gives you string storage the contents of which you can modify
at your leisure. The second merely gives you a handful of pointers,
which currently point at string literals that you must not change.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.




我现在没有时间调查这篇文章,

所以我不能对它的有效性发表评论。我可以推荐这个

一个:

Google也为Chris Torek撰写文章C for smarties。

IMO Torek先生当之无愧地被认为是comp.lang.c'

''常驻大师'之一。通过阅读他的帖子,我学到了很多关于

C.


我对你的问题的评论如下。

I don''t have time to investigate that article right now,
so I can''t comment on its validity. I can recommend this
one: http://pweb.netcom.com/~tjensen/ptr/pointers.htm
Also Google for Chris Torek''s article "C for smarties".
IMO Mr. Torek is deservedly considered one of comp.lang.c''s
''resident gurus''. I have learned a considerable amount about
C from reading his posts.

My remarks about your questions follow below.



任何数组,''高'',''低'或其间,数组

和指针是不同的实体。它们在语法和语义上都有不同的区别(尽管在某些情况下,它们可以表现相同 - 例如

通过自动转换数组的名称

作为参数传递给函数时指针。

At any level, ''high'', ''low'' or in between, arrays
and pointers are different entities. They differ
both syntactically and semantically (although in
some cases, they can behave the same -- e.g.
via the automatic conversion of an array''s name
to a pointer when passed as an argument to a function).



没有必要学习,以便学会有效地使用

C.

There is no need to learn that in order to learn to use
C effectively.



没有''参数'',它确实是一个平台(和编译器)

具体问题。 (理想情况下)编译器将写入

利用给定的

目标平台的优势(并克服弱点)。

There''s no ''argument'', it is indeed a platform (and compiler)
specific question. (Ideally) a compiler will be written to
exploit the strengths (and overcome the weaknesses) of a given
target platform.



这个建议已在这里多次给出,但我会再说一遍:

编写代码尽可能清晰,可读和可维护。

让编译器编写者做他们擅长的事:优化代码。

善于在源代码中清楚地表达解决方案。

只有在分析证明存在

a性能问题时才考虑优化。


-Mike

This advice has been given here many times, but I''ll say it again:
write your code to be as clear, readable, and maintainable as possible.
Let the compiler writers do what they''re good at: optimizing code.
Become good at expressing solutions clearly in the source code.
Only consider optimization if and when profiling proves there''s
a performance issue.

-Mike




他们这样做,但我已经知道了这一点。

They do, but I already knew about this.



它们是具有不同含义的不同结构,因此问题

的效率并不是真的出现(快速 - 效率更高) ,一台
微波炉或电话?)。


They are different constructs with different meanings, so the question
of efficiency doesn''t really arise (quick - which is more efficient, a
microwave oven or a telephone?).



当然它们是不同的结构,但我可以将它们用于相同的

目的。在我的情况下索引号-string / string -index number

转换。这些构造在更高级别完成相同的事情

给我一堆字符串,我可以使用索引号来解决 (我实际想要的是
),现在我想知道哪种结构更有效。

Of course they are different constructs, but I can use them for the same
purposes. In my case index number -string / string -index number
conversions. The constructs do exactly the same thing at a higher level
"give me a bunch of strings which I can address using index numbers" (what I
actually want), now I want to know which construct is more efficient.



谢谢,但我也知道。好吧,除了你可以随意修改一个const char

数组。我的编译器肯定会抱怨这一点。

当然,你可以抛弃const修饰符,但如果你问我,那将是相当难看的




copx

Thanks, but I knew that, too. Well, except that you can modify a const char
array "at your leisure". My compiler would certainly complain about that. Of
course, you can cast the const modifier away, but that would be rather ugly
if you ask me.

copx


这篇关于低级指针与数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:46