我看到了以下一些复杂的函数定义。
void foo(double A[static 10]) {
double B[10];
}
Run Code Online (Sandbox Code Playgroud)
它是有效的 C 和 C++ 代码吗?它是 C99 或 C++ 标准引入的新语法吗?它的目的是什么?我应该什么时候使用它?这有什么必要?
C11&C++14标准已经删除gets()了本质上不安全的功能并导致安全问题,因为它不会在缓冲区溢出中执行边界检查结果.那为什么C11标准不掉线strcat()和strcpy()功能?strcat()函数不检查第二个字符串是否适合第一个数组.strcpy()函数也不包含检查目标数组边界的规定.如果源数组有多于目标数组可以容纳的字符怎么办?程序很可能会在运行时崩溃.
那么,如果这两个不安全的函数完全从语言中删除,那不是很好吗?为什么他们仍然存在?是什么原因?只有像这样的功能不是很好strncat(),strncpy()吗?如果我没有错,Microsoft C&C++编译器提供了这些功能的安全版本strcpy_s(),strcat_s().那么为什么他们没有正式实施其他C编译器来提供安全性呢?
C++标准第3.6.1/3节说
main的链接是实现定义的
这是什么意思?为什么要定义实现?C也一样吗?
考虑以下程序:
#include <iostream>
struct __attribute__((__packed__)) mystruct_A
{
char a;
int b;
char c;
}x;
int main()
{
std::cout<<sizeof(x)<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
从这个我明白了以下几点:
我在32位环境中,正在使用Windows 7 OS。链接问题的第一个答案说,以上代码将在32位体系结构上生成6号大小的结构。
但是,当我使用g ++ 4.8.1对其进行编译时,它的输出为9。那么,结构打包在这里不会完全发生吗?为什么输出中要多出3个字节?sizeof char始终为1。在我的编译器中,sizeof int为4。因此,压缩结构时,以上结构的sizeof应该为1 + 4 + 1 = 6。
处理器有任何作用还是仅取决于编译器?
考虑以下程序(请参阅此处的实时演示)
import std.stdio;
void main()
{
int i=int();
writefln("i is %d",i);
}
Run Code Online (Sandbox Code Playgroud)
在像C++这样的语言中,该语句int i=int();称为值初始化.对于类型int,值初始化基本上最终为零初始化.如果我没有错,C++标准保证它总是给我零.但D语言是否包含像C++一样的值初始化功能?是否必须在所有D编译器和我在D程序上编译和运行的每个环境中给出零值?
考虑以下程序:
#include <cstdio>
int main()
{
int printf=9;
std::printf("%d",printf);
}
Run Code Online (Sandbox Code Playgroud)
在变量声明中使用内置函数名作为标识符可以吗?这是定义明确的程序吗?我的意思是上述程序的行为是否明确定义?我很想知道 C++ 标准是否允许使用标准函数名称作为变量的标识符
考虑以下计划:
#include <iostream>
#include <algorithm>
#include <array>
bool greater_than_seven(int i) {
return i > 5;
}
bool divisible_by_five(int x) {
return ((x%5)==0);
}
int main() {
int arr[]{3,6,9,12,15};
std::cout<<"Enter a number you want to search: ";
int num;
std::cin>>num;
auto result(std::find(std::begin(arr),std::end(arr),num));
if(result != std::end(arr))
std::cout<<"arr contains: "<<num<<'\n';
else
std::cout<<"arr doesn't contain: "<<num<<'\n';
for(result=std::find_if(std::begin(arr),std::end(arr),greater_than_seven);result!=std::end(arr);++result)
std::cout<<*result<<' ';
std::cout<<'\n';
std::array<int,4> x{33,66,99,55};
for(result=std::find_if_not(std::begin(x),std::end(x),divisible_by_five);result!=std::end(x);++result)
std::cout<<*result<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
这个程序在g ++和clang ++上编译得很好.
在这里查看现场演示(g ++ 5.4.0)
在这里查看现场演示(clang ++ 3.8.0)
但它在Microsoft Visual C++编译器上提供了可怕的编译器错误.
请参阅此处的实时演示(适用于x64的Microsoft(R)C/C++优化编译器版本19.00.23506)
Error(s):
source_file.cpp(27): …Run Code Online (Sandbox Code Playgroud) 我们知道重载对C++中的派生类不起作用.但为什么这种行为在java中有所不同?意味着为什么重载适用于java中的派生类?请考虑以下Stroustrup博士的常见问题解答中的示例
#include <iostream>
using namespace std;
class Base
{
public:
int f(int i)
{
cout << "f(int): ";
return i+3;
}
};
class Derived : public Base
{
public:
double f(double d)
{
cout << "f(double): ";
return d+3.3;
}
};
int main()
{
Derived* dp = new Derived;
cout << dp->f(3) << '\n';
cout << dp->f(3.3) << '\n';
delete dp;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:
f(双):6.3
f(双):6.6
而不是假定的输出:
f(int):6
f(双):6.6
但如果我们在java中执行此程序,则输出会有所不同.
class Base
{
public int f(int i)
{
System.out.print("f …Run Code Online (Sandbox Code Playgroud) 我知道C++也允许创建类和结构的全局对象.
#include <iostream>
using std::cout;
class Test
{
public:
void fun()
{
cout<<"a= "<<a;
}
private:
int a=9;
};
Test t; // global object
int main()
{
t.fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
何时何地我应该使用全局对象?全局对象是否有特定用途?请帮我.
我知道数组索引在C&C++中是可交换的,因此a [i]与i [a]相同,它与i [a]具有相同的效果,并且两者都是有效的.但是最近我写了一个下面的程序,当我使用i [intob]而不是intob [i]时无法编译.
#include <iostream>
#include <cstdlib>
using std::cout;
template<class T>class atype
{
T* a;
int size;
public:
atype(int n)
{
size=n;
a=new T[size];
for(auto i=0;i<size;i++)
a[i]=i;
}
~atype()
{
delete[] a;
}
T& operator[](int i);
};
template<class T> T& atype<T>::operator[](int i)
{
if(i<0 || i>size)
{
cout<<"\nIndex value of ";
cout<<i<<" is out of bounds.\n";
exit(1);
}
return a[i]; // i[a] also works fine here.
}
int main()
{
int i,n;
cout<<"Enter value of n: "; …Run Code Online (Sandbox Code Playgroud)