标题说明了一切.我很好奇为什么restrict关键字不是C++的一部分?我对C++知之甚少,而且我仍然无法在网上找到任何阻止这种情况的理由.有没有人知道会发生什么可怕的事情,如果C++标准会像C一样使用这个关键字?它根本不需要吗?
更多解释:这不是关于使用它,也许在我的一生中我不会从这个关键字中获益.这个问题只是关于好奇心,因为自C99以来,限制是C的一部分,即15年.
阅读本文:我对技术原因感兴趣,而不是像"他们只是不喜欢,它不够酷"这样的意见
我在C标准文本中遇到过这个问题,我可以在§6.8.5找到C99或C11的任何版本:
iteration-statement:
while ( expression ) statement
do statement while ( expression ) ;
for ( expression ; expression ; expression ) statement
for ( declaration expression ; expression ) statement
Run Code Online (Sandbox Code Playgroud)
这里的第四项似乎for只有一个分号,我在其他地方看不到任何对这种语法的引用.谁能在这里解释我错过了什么?
对不起,如果标题令人困惑,我找不到一个简单的方法来写一个简单的句子.无论如何,我面临的问题是:
// header:
class SomeThing
{
private:
SomeThing() {} // <- so users of this class can't come up
// with non-initialized instances, but
// but the implementation can.
int some_data; // <- a few bytes of memory, the default
// constructor SomeThing() doesn't initialize it
public:
SomeThing(blablabla ctor arguments);
static SomeThing getThatThing(blablabla arguments);
static void generateLookupTables();
private:
// declarations of lookup tables
static std::array<SomeThing, 64> lookup_table_0;
static SomeThing lookup_table_1[64];
};
Run Code Online (Sandbox Code Playgroud)
该getThatThing函数用于从查找表返回实例.
// in the implementation file - …Run Code Online (Sandbox Code Playgroud) 我想使用 longjmp 返回错误代码,并从调用 setjmp 的函数传递它。简化代码:
int do_things(stuff ........)
{
int error_code;
jmp_buf jb;
if ((error_code = setjmp(jb)) == 0) {
/* do stuff */
return 0;
}
else {
return error_code;
}
}
Run Code Online (Sandbox Code Playgroud)
但我读到:“setjmp 宏的调用只能出现在以下上下文之一中:”
the entire controlling expression of a selection or iteration statement
if (setjmp(jb)) {
switch (setjmp(jb)) {
while (setjmp(jb)) {
Run Code Online (Sandbox Code Playgroud)
或者
one operand of a relational or equality operator with the other operand
an integer constant expression, with the resulting expression being
the entire controlling expression …Run Code Online (Sandbox Code Playgroud) 标题说明了一切!clang是否有相当于GCC的-mno-vzeroupper标志?我使用3.5,也许它会在3.6?
让我们假设我想读/写一个tar文件头.考虑到标准C(C89,C99或C11),char数组在结构上是否有任何特殊处理,关于填充?编译器可以为这样的结构添加填充:
struct header {
char name[100];
char mode[8];
char uid[8];
char gid[8];
char size[12];
char mtime[12];
char chksum[8];
char typeflag;
char linkname[100];
char tail[255];
};
Run Code Online (Sandbox Code Playgroud)
我已经看到它也用在网络上的代码中.只是自由,将这个结构写入一个块中的文件,假设不会有任何填充.当然也有假设CHAR_BITS == 8.我认为这样的C代码是如此常见,标准会处理这种情况,但我只是找不到它,也许我不会是一个好律师.
编辑
接受的答案将根据C标准之一给出严格或最严格的可移植实现,这使我可以使用标准库字符串函数处理这些字段.考虑CHAR_BITS到所有.我认为需要为此读取512的数组uint8_t,然后可能将它们逐个转换为字符.有更简单的方法吗
在这个示例代码中,while循环后指针真的可以无效,并且在编写代码时是否应该考虑到这一点?或者C标准是否被误解和/或有缺陷?
#include <stdio.h>
int main(int argc, char **argv) {
(void)argc;
(void)argv;
int *pointer;
int object[1];
pointer = object;
printf("pointer -before: %p\n", (void*)pointer);
do {
int other_object[1];
printf("a pointer \"just past\" other_object can look like: %p\n", (void*)(&other_object+1));
printf("address of other_object: %p\n", (void*)&other_object);
} while (0);
puts("the lifetime of other_object has ended");
printf("pointer -after: %p\n", (void*)pointer);
}
Run Code Online (Sandbox Code Playgroud)
可能的输出(在我的机器上运行):
pointer -before: 0x7fff5f744ae4
a pointer "just past" other_object can look like: 0x7fff5f744ae4
address of other_object: 0x7fff5f744ae0
the lifetime of other_object has ended
pointer -after: …Run Code Online (Sandbox Code Playgroud) 与此问题相关的C代码:
#include <stdio.h>
int main(int argc, char **argv)
{
int k = 0;
double b = k++ + ++k + k--;
printf("%d", k);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
存储的值b是未定义的,但是怎么样k?
我发现这个的地方:
http://www.sanfoundry.com/online-c-test-precedence-order-evaluation/ Question#10
- 编辑 -
到目前为止我发现:存储的值b没有在任何地方使用,所以如果存储的东西b是唯一的UB,这个程序将不依赖于UB.
但我也在C99 6.5.2中找到了这一部分:
"在前一个和下一个序列点之间,一个对象的存储值最多只能通过表达式的评估来修改一次."
并列在J.2下.未定义的行为:
"行为未定义......'''应'或''不得''出现在约束之外的要求被违反"
但实际问题尚未得到解答.
- 编辑#2 -
并不是说我想写一个,而是根据标准的'严格符合程序':
"不得产生依赖于任何未指定,未定义或实现定义的行为的输出"
因此,原始示例是错误的,因为它确实依赖于未定义的行为,即使有人会替换该行,它也将是未定义的
double b = k++ + ++k + k--;
Run Code Online (Sandbox Code Playgroud)
与线
k++ + ++k + k--;
Run Code Online (Sandbox Code Playgroud)
所以现在我正在寻找关于什么问题的更好的介绍.