我用什么编译器标志来完全摆脱异常处理?我根本不使用它们,但编译器仍然生成.pdata和.xdata部分,仅用于异常处理.
我在下面的代码中很困惑,这在VS2012 Update 5中有效但在g ++ 8.1中失败了.
int& func()
{
int i = 0;
return i;
}
int main()
{
int ri = func();
ri++;
std::cout << ri << std::endl; // output "1"
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解并参考类似的问题C++返回对局部变量的引用,它应该失败,因为i函数中的局部变量的生命周期func应该在函数调用之后结束.
但是,为什么它在VS2012中有效?
这让我无法入睡.
更新v1:
添加VS2012指定版本并更改代码以使用变量ri.
我是新手.喜欢理解为什么"p =&a"不起作用.谢谢.
class A{
int *p;
public:
A(int a){p=new int; p=&a;}
~A(){delete p;}
};
int main(void){
A B(11);
}
Run Code Online (Sandbox Code Playgroud) 我收到错误C2512:'derived':派生类的构造函数定义中没有适当的默认构造函数可用错误.我的代码如下.我该如何解决这个问题?
Class A
{
int a, int b;
A(int x, int y)
{
sme code....
}
}
Class B
{
int a, int b, int c;
B(int x, int y, int Z)
{
sme code....
}
}
Class derived : public A, public B
{
derived(int a, int b):A(a, b)
{
}
derived(int a, int b, int c):B(a, b, c)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我的角色指针指向一些记忆说"Hello world",我想将它与其他指针进行比较,后来想做strcpy.我认为可以做char*
char *A ="hello"
char *B ="";
strcmp(A,B); // this compares correctly because B points to diff string
strcpy(B,A); // will this statment copies the string alone or both will point to same memory
Run Code Online (Sandbox Code Playgroud) 我想编写一个打开二进制文件并使用DES对其加密的程序。
但是如何读取二进制文件?
我正在尝试初始化一个32位整数数组bu由于某种原因调试器(MSVC)在写入过程中途抛出异常.
该数组是1048576个元素,
在迭代263152上失败
#define ROM_MAX_SIZE (1024*1024*4)
int main(){
size_t rom_size = ROM_MAX_SIZE / sizeof(uint32_t);
uint32_t *rom = malloc(rom_size); //<-- Error here, must be ROM_MAX_SIZE. See edit
for (uint32_t i = 0; i < rom_size; i++){
rom[i] = i; //<--- Access violation here
}
free(rom);
return 1
}
Run Code Online (Sandbox Code Playgroud)
我可能会遗漏一些明显但我看不到的东西.
编辑:
Malloc必须是元素数量的4倍.
uint32_t *rom = malloc(rom_size); //<--- WRONG!
uint32_t *rom = malloc(ROM_MAX_SIZE); //<--- OK.
Run Code Online (Sandbox Code Playgroud) 我正在学习c ++语言,我试图找出vector和list之间的区别.我正在使用visual studio工具进行编码.有人能解释一下有什么区别吗?
我找到了这个例子float a = 35E5;并试图编译它并成功编译.我发现E告诉编译器将多少个十进制零添加到变量值.
然后我尝试为这样的变量赋值
float a = 5.0, b = 5.5;
float c;
c = (a + b)E5;
Run Code Online (Sandbox Code Playgroud)
和编译器报告错误:

现在,我的问题是:为什么不允许将值分配给变量,例如第2个例子,并且允许在第1个示例中使用?
我正在运行以下代码,其中我声明了一个动态2D数组,然后继续在列索引处分配值高于实际为动态数组分配的数字列.但是,当我这样做时,代码运行完美,我没有得到错误,我相信我应该得到.
void main(){
unsigned char **bitarray = NULL;
bitarray = new unsigned char*[96];
for (int j = 0; j < 96; j++)
{
bitarray[j] = new unsigned char[56];
if (bitarray[j] == NULL)
{
cout << "Memory could not be allocated for 2D Array.";
return;// return if memory not allocated
}
}
bitarray[0][64] = '1';
bitarray[10][64] = '1';
cout << bitarray[0][64] << " " << bitarray[10][64];
getch();
return;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出的链接在这里(值实际上是准确分配的,但不知道为什么).
c++ pointers undefined-behavior dynamic-memory-allocation visual-c++