问题标签 [indirection]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c - void 指针伪装成 void 双指针
我一直在思考。我还没有找到任何直接回答这个问题的东西,但我想我知道答案;我只是想从一些更有经验的人那里得到一些意见。
已知:
一个 void 指针只指向一个内存地址。它不包括类型信息。
int 指针指向包含 int 的内存地址。它将读取指向整数的内存地址中的任何内容,而不管最初填充到地址中的是什么。
问题:
如果 void 双指针指向void ** foo
动态分配的 void 指针数组
是不是真的,正如我所假设的那样,由于 void 指针的独特性质,实际上缺乏任何类型的类型信息,而不是void ** foo
等效的语句
并且当我通过分配特定类型来使用间接访问时,例如
(有人指出我不能取消引用 void 指针。为了清楚问题的目的,下一行更改为适合该信息)
我会从单个void 指针中得到一个适当的指针,它就像一个双精度的指针吗?
还是这一切都在我的脑海中?
c - Program crash when trying to retrieve an U32 from a struct
I have been asked to finish some code someone else started, and I am completely confused on how to copy a U32 value inside an struct. These are the relevant parts of the various structs; note that I am trimming a lot because those are some seriously huge structs:
Now, in the function I am trying to modify:
That last line is the one that is causing me all sorts of grief. It does work, I can see in the debug output how many critters are attacking the player; but it causes a crash whenever the AI loses interest in the target. I know this has something to do with pointers, but sprinkling asterisks on the code results in either "invalid indirection" or "differs in levels of indirection". I am pretty much stumped on how to retrieve just the value of aiTarget->attackerList.count without any weird pointer stuff.
c++ - Does the standard mandate an lvalue-to-rvalue conversion of the pointer variable when applying indirection?
TL;DR
Given the following code:
does *ptr
require an lvalue-to-rvalue conversion of ptr
before applying indirection?
The standard covers the topic of lvalue-to-rvalue in many places but does not seem to specify enough information to determine whether the * operator require such a conversion.
Details
The lvalue-to-rvalue conversion is covered in N3485 in section 4.1
Lvalue-to-rvalue conversion paragraph 1 and says (emphasis mine going forward):
A glvalue (3.10) of a non-function, non-array type T can be converted to a prvalue.53 If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the glvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior.[...]
So does *ptr = 0;
necessitate this conversion?
If we go to section 4
paragraph 1 it says:
[...]A standard conversion sequence will be applied to an expression if necessary to convert it to a required destination type.
So when is it necessary? If we look at section 5
Expressions the lvalue-to-rvalue conversion is mentioned in paragraph 9 which says:
Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue (4.1), array-to-pointer (4.2), or function-to-pointer (4.3) standard conversions are applied to convert the expression to a prvalue. [...]
and paragraph 11 which says:
In some contexts, an expression only appears for its side effects. Such an expression is called a discarded-value expression.[...] The lvalue-to-rvalue conversion (4.1) is applied if and only if the expression is an lvalue of volatile-qualified type and it is one of the following [...]
neither paragraph seems to apply to this code sample and 5.3.1
Unary operators paragraph 1 it says:
The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.” [ Note: indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue, see 4.1. —end note ]
it does not seem to require the value of the pointer and I don't see any requirements for a conversion of the pointer here am I missing something?
Why do we care?
I have seen an answer and comments in other questions that claim the use of an uninitialized pointer is undefined behavior due the need for an lvalue-to-rvalue conversion of ptr
before applying indirection. For example: Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior? makes this argument and I can not reconcile the argument with what is laid out in any of the recent draft versions of the standard. Since I have seen this several times I wanted to get clarification.
The actual proof of undefined behavior is not as important since as I noted in the linked question above we have others way to get to undefined behavior.
c - C 预处理器是否删除“&*”的实例?
我正在玩gcc
并尝试了以下代码:
并且C == &A
,正如预期的那样。但是当我尝试时:
结果是C == NULL
,并且没有段错误。所以在获取地址之前&*B
实际上并没有取消引用。B
我的猜测是,预处理器正在剥离实例,&*
甚至*&
在它们到达编译器之前,因为它们相互否定,但我找不到任何文档来验证这是标准C还是特定于编译器的。
预处理器是否剥离了&*
and *&
,我可以从任何给定的编译器中得到这种行为吗?
c - 声明 C 指针 - 为什么我们使用 * 而不是 &
我的问题是关于指针声明的 c 语法的一个简单问题(我希望如此)。我完全知道如何声明一个指针,它是如何使用的,效果是什么,如下所示。
然而,令我困惑的是,为什么当我们在 C 中声明一个指针时,我们使用 * 间接(值)运算符?而不是运算符的 & 地址。如果我们声明一个指针,用 & 这样做是不是没有意义,因为我们声明了一个地址,对吗?例子:
我知道这是不正确的,但在我看来这似乎更直观。我对 * 运算符的概念中缺少什么。我还没有找到一本可以解释原因的教科书,它们只是说明了如何。我知道怎么做,我想知道为什么。
谢谢阅读。
c++ - 动态内存分配、堆、间接、NULL
我试图保证我动态分配的内存没有指向任何地方。
我尝试了以下
就像一个人可以写
但这不起作用,因为在上面的示例mem
中不是“左值”?
为什么只有在我在堆上动态分配更多一种特定于类型的内存时才会出现这种情况?
另外如果我使用模板函数会发生一件奇怪的事情,因为它似乎是合法的
怎么可能将NULL
(基本上是int
值 0)分配给基本上可以是任何东西的变量或对象?它可能是一个int
变量,很好,它会起作用。但是如果我用std::string
. 它根本不应该工作,对吧?那么为什么写这样的东西似乎是合法的呢?
是否可以自由编程泛型,但也有责任注意,而不是调用错误类型的泛型函数?
c - C4047 - 间接级别 - 结构初始化
我实际上正在使用我公司的一些旧软件,我试图弄清楚如何使东西正常工作。
经过一些数据库编辑后,我得到了一个软件,它给了我一些 *.C 文件,以便稍后编译以获得环境模拟工具。
我的主要问题是我没有设法让任何编译工作。我面临 2 个电源错误:
错误:C2097:非法初始化
和
警告 C4047 '正在初始化':不同级别的间接。
我开始研究以了解这些间接级别。我想我理解试图用数据指针等保存指针指针的事情。
代码不是我的。所有这些都在计算机中使用应用程序生成。关键是开发所有这些东西的人都退休了。
以下是代码的主要部分:
ST_FAMI.H
我的 M1L0PS1.C 文件:
错误日志:
我的问题:我没有得到间接错误级别。我试着给我的初始化差异数据,如: &(E_L0PS1Pq1[0]) (元素 0 的地址),但它没有按预期工作。
这段代码有什么问题?我注意到一个合适的 C 开发人员。我曾经做过一些 C++ 或 Java,但由于那些不太严格的语言,我对指针的了解有点偏颇。我正在使用“Microsoft (R) C Optimizing Copiler Version 5.10”进行编译。
对不起我的英语,如果有人通读全文,谢谢!:D
c++ - 通过指向对象的指针访问成员
在堆栈上,编译器可以自由地进行很多优化,因为上下文是静态的并且在编译时是已知的,但是在处理对动态分配的对象的访问并且通常访问“通过引用”时,上下文是未知的,所以在逻辑上在这种情况下,成员访问归结为取消引用通过添加对象基地址和该成员的偏移量得出的内存地址。或者是吗?我对此很陌生,所以在这一点上,我只是在猜测,可能会遗漏很多细节。
例如,我注意到如果我在使用堆栈成员(并使用运算符)时实现+
运算符,则汇编代码与常规运算符创建的相同,因此似乎已知指向编译时已知值的指针are 正在被优化以排除额外的取消引用(和复制),并且函数内部的间接被内联为对堆栈对象的直接访问。这是否意味着编译器“足够好”能够优化通过从堆栈上的对象基地址的常量值偏移间接实现的访问器,就好像使用结构成员访问来获取该值一样?void add(int * a, int * b, int * r);
&
+
add()
python - 我应该如何引用任何以固定字符串开头并因结束数字不同而不同的任意类属性?
我正在编写一个 Python 程序,它使用 ConfigParser 来读取一个配置文件,该文件旨在控制程序配置、执行和定位到其环境和景观的各个方面。我在 RHEL 6.4 上使用 Python 2.6.6。
其配置的一个方面是它需要与多少个 rsyslog 守护进程交互,以及每个实例的详细信息。我选择了 instance<#>_ 格式,使用户能够指定任意数量的实例,并使用一组一致的属性进行配置。配置文件的摘录出现在此处:
我构建了一个名为“rsyslog”的对象,使其属性如下所示:
当我将 rsyslog 对象和实例编号传递给函数以让该函数对 instance#_rules 执行操作时,我的问题就出现了。例如,我以这种方式调用该函数:
该函数应该返回一个规则列表,它从规则文件中解析出来,例如 instance2
当我对实例进行硬编码时,解析规则没有问题:
但是我如何允许类似下面的内容,其中 '' 表示我传递给函数的实例号:
我已经使用 locals() 和 globals() 函数在其他上下文中进行变量间接寻址,但我不确定如何在此处应用它们。
或者,如果您能看到一种更好、更优雅或 Pythonic 的方法来解决允许任意数量的连续编号实例的问题,这些实例可以在迭代中通过数字轻松地和更 Pythonic 地引用;请解释如何以及为什么它是完成任务的更好或更Pythonic的方式。
如果有另一种方法,我不会使用实例。ConfigParser 不允许 '.' 在配置中
shell - 动态为unix中的变量赋值 - 变量间接
我收到错误替换错误。但是如果传递的参数是 stg,我希望打印 stg.com:8080。我该怎么做