小编msc*_*msc的帖子

在循环内部或外部声明变量有很大区别吗?

例如

int i, a, c, d; // these vars will only be used in while
while(bigNumber--) {
    i = doSomething();
    // **

}
Run Code Online (Sandbox Code Playgroud)

while(bigNumber--) {
   int i, a, b, c; // i'd like to put it here for readability
   i = doSomething();
   // **
}
Run Code Online (Sandbox Code Playgroud)

它在性能方面有很大区别吗?

c performance variable-declaration

1
推荐指数
1
解决办法
2468
查看次数

构造函数的名称和模板的析构函数

n4459:

635.模板的构造函数和析构函数的名称

允许定义构造函数的语法和类模板的析构函数之间存在差异.例如:

template <class> struct S { S(); ~S (); };
template <class T> S<T>::S<T>() { }        // error
template <class T> S<T>::~S<T>() { }       // okay
Run Code Online (Sandbox Code Playgroud)

原因是3.4.3.1 [class.qual]第2段说 S :: S被认为是"命名构造函数",它不是模板,因此不能接受模板参数列表.另一方面,第二个S in S::~S找到inject-name-name,"可以使用或不使用template-argument-list"(14.6.1 [temp.local]第1段),从而满足要求命名析构函数的类(12.4 [class.dtor]第1段).

我不明白,是什么说S::S是"考虑到命名构造函数",这是不是一个模板

c++ templates constructor c++11

1
推荐指数
1
解决办法
84
查看次数

关于限制限定符的未定义行为的混淆

我在cppreference上看到了以下示例.

void f(int n, int * restrict p, int * restrict q)
{
    while(n-- > 0)
        *p++ = *q++; // none of the objects modified through *p is the same
                     // as any of the objects read through *q
                     // compiler free to optimize, vectorize, page map, etc.
}
void g(void)
{
    extern int d[100];
    f(50, d + 50, d); // OK
    f(50, d + 1, d); // Undefined behavior: d[1] is accessed through both p and q …
Run Code Online (Sandbox Code Playgroud)

c restrict undefined-behavior

1
推荐指数
1
解决办法
81
查看次数

是"printf("%d \n",(int)a);" unsigned int的未定义行为?

如果我使用错误的格式说明符,如下所示:

unsigned int i = -1;
printf("%d\n", i);
Run Code Online (Sandbox Code Playgroud)

它被调用未定义的行为,因为%u格式说明符unsigned.

C11标准§7.21.6.1(P9):

如果转换规范无效,则行为未定义.282)如果任何参数不是相应转换规范的正确类型,则行为未定义.

但是,如果我这样写:

unsigned int i = -1;
printf("%d\n", (int)i); // unsigned to signed 
Run Code Online (Sandbox Code Playgroud)

它也是未定义的行为吗?

c format-specifiers language-lawyer

1
推荐指数
1
解决办法
115
查看次数

访问以null结尾的字符

灵感来自这个问题.

码:

#include <stdio.h>

int main()
{
   char arr[] = "Hello";
   char *ptr = arr + 5;
   printf("%s\n",ptr);
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我访问了以null结尾的字符.

那么,在文字字符串中访问空终止字符时实际发生了什么?它是未定义的行为吗?

c arrays null-terminated

1
推荐指数
1
解决办法
66
查看次数

关于"while(*s1 ++ =*s2 ++);"的困惑 在C.

码:

#include <stdio.h>

int main()
{
    char str1[]="bombay";
    char str2[]="pune";

    char *s1 = str1;
    char *s2 = str2;

    while(*s1++ = *s2++);
    printf("%s\n",str1);
}
Run Code Online (Sandbox Code Playgroud)

输出:( GCC编译器)

pune
Run Code Online (Sandbox Code Playgroud)

但根据我的输出应该是puneay.pune应该复制代替,bomb休息应该是原样.

那么,为什么编译器会输出这段代码"pune"而不是"puneay"

c pointers

1
推荐指数
1
解决办法
127
查看次数

关于C++中复制构造函数的困惑

码:

class A 
{
public:
    A()
    {
        cout<<"Defualt Constructor"<<endl;
    }

    A(A &t) 
    { 
        cout<<"Copy Constructor"<<endl;
    }
};

A func()
{
    cout<<"In func"<<endl;
}

int main()
{
    A a1;
    A a2;
    a2 = func();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序工作正常.另外,如果我调用这样的函数:

A a2 = func();
Run Code Online (Sandbox Code Playgroud)

const复制 构造函数参数中添加限定符,如:

A(const A &t) 
{ 
    cout<<"Copy Constructor"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

还有,工作正常.

但是,如果const复制构造函数参数中删除如:

A(A &t) 
{ 
   cout<<"Copy Constructor"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

并调用函数 func()

A a2 = func();
Run Code Online (Sandbox Code Playgroud)

编译器给出错误:

error: invalid initialization of non-const reference of …
Run Code Online (Sandbox Code Playgroud)

c++ reference copy-constructor c++11 c++14

1
推荐指数
1
解决办法
94
查看次数

为什么转换规范%lf对于printf中的Double不起作用

我只是写一个非常小的代码scanfprintf。我正在读取一个双精度值并将其打印出来。转换规范%lf可以正常工作以读取双精度值。但是,它不适用于printf。

当我尝试打印该值时,我得到的输出像 0.000000

double fag;
scanf("%lf", &fag);
printf("%lf", fag);
Run Code Online (Sandbox Code Playgroud)

但是,如果我在printf中使用%f,它可以正常工作。

c format-specifiers

0
推荐指数
1
解决办法
1205
查看次数

自动变量问题

我知道C中的存储类.我想知道auto关键字.

在以下表达式中,

auto int i = 0;
Run Code Online (Sandbox Code Playgroud)

auto是存储类说明符,int是数据类型.

但如果我们写:

int auto i = 0;
Run Code Online (Sandbox Code Playgroud)

怎么了?被auto表现为数据类型?

c

0
推荐指数
1
解决办法
96
查看次数

为什么局部变量在for循环结束后可见且可访问?

我想知道在python中,为什么d 在for 循环结束后可见且可访问  ?C,这是不可能的.它在python中有效吗?

for i in range(4):
    d = i * 2
print(d)
Run Code Online (Sandbox Code Playgroud)

在线编译链接在这里.

python

0
推荐指数
1
解决办法
90
查看次数