我正在Visual Studio中使用Win32 c ++应用程序.
在其中一个源文件中,我有如下的全局对象.
TestClass tObj;
int main() //Execution starts here
{
}
Run Code Online (Sandbox Code Playgroud)
TestClass在下面的其他DLL中定义.
struct Source
{
};
class TestClass
{
list<Source> sourceList;
public:
TestClass() {}
~TestClass() {}
};
Run Code Online (Sandbox Code Playgroud)
当我的应用程序运行时,如果我尝试通过关闭控制台窗口显式关闭应用程序,它将在TestClass析构函数中崩溃.Callstack显示CrtIsValidHeapPointer失败.
请帮我解决这个问题.
我从cplusplus.com获取以下代码:
// set_terminate example
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void myterminate () {
cout << "terminate handler called\n";
abort(); // forces abnormal termination
}
int main (void) {
set_terminate (myterminate);
throw 0; // unhandled exception: calls terminate handler
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于代码中存在未处理的异常,因此需要调用myterminate()函数,该函数设置为终止处理程序并且应该覆盖默认的终止处理程序.
程序崩溃但没有调用myterminate().我使用的是Visual C++ 2008 Express Edition.
代码有什么问题?
我想知道编译器优化策略,用于在Visual Studio中为我的c ++应用程序生成优化的目标代码.目前我正在使用默认设置.
我有以下代码,在32位Windows上运行,visual-studio.
template <class T>
class Test
{
public:
T &ref;
Test(T &x)
:ref(x)
{}
};
int main()
{
cout<<"sizeof Test<int> : "<<sizeof(Test<int>)<<endl;
cout<<"sizeof Test<double> : "<<sizeof(Test<double>)<<endl;
cout<<"sizeof Test<char> : "<<sizeof(Test<char>)<<endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
sizeof Test<int> : 4
sizeof Test<double> : 4
sizeof Test<char> : 4
Run Code Online (Sandbox Code Playgroud)
编译器为类中的引用变量提供4个字节,而不考虑引用类型.变量值不能存储在这4个字节中.
什么信息编译器将存储在这4个字节中?
它内部存储了所指的地址吗?这样引用和引用都可以写入相同的位置以便彼此同步.
或者是在符号表中存储指示对象的名称?
这类似于零大小的数组分配/意味着什么?
我有以下代码
int *p = new int[0];
delete []p;
Run Code Online (Sandbox Code Playgroud)
p获取一个地址并被正确删除.
我的问题是:为什么c ++ Standard首先允许分配零字节?为什么不抛出bad_alloc或一些特殊的异常?
我认为,这只是推迟了灾难性的失败,使程序员的生活变得困难.因为如果要在运行时计算要分配的大小,并且如果程序员正确地假定其分配并尝试向该内存写入内容,则最终会破坏内存!并且崩溃可能发生在代码中的其他位置.
编辑:它在零大小请求时分配了多少内存?
这是一个功课问题.对于以下代码,
#include <iostream>
using namespace std;
class A
{
public:
virtual void f(){}
};
class B
{
public:
virtual void f2(){}
};
class C: public A, public B
{
public:
virtual void f3(){}
};
class D: public C
{
public:
virtual void f4(){}
};
int main()
{
cout<<sizeof(D)<<endl;
}
Run Code Online (Sandbox Code Playgroud)
输出为:8
任何人都可以解释它是如何8字节?如果vtable实现依赖于编译器,那么在采访中我应该回答这类问题?虚拟基类怎么样?
编辑:我正在使用32位平台.
我使用下面的代码并尝试通过按F10在Visual Studio 2008中进行调试.
//test.cpp
#include<iostream>
using namespace std;
int main(void)
{
#line 100 "test.cpp"
cout<<"Inside main()"<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
下面是调试器屏幕截图.

#line 100告诉编译器转到第100行以获取其下一行.由于第100行不存在,它将超出主要功能,如屏幕截图所示.如果我尝试用F10调试代码,控制永远不会回到主函数.它继续显示主函数外的指针,即使它正在执行main().
如果我给其他文件名代替test.cpp,指针将转到该文件,但它不会返回test.cpp
知道为什么调试器的行为是这样的吗?
我在下面看到了关于c ++标准$ 6.4.2中switch语句的事情.
Switch语句可以采取一个条件.
条件应为整数类型,枚举类型或类型,其中存在单个转换函数为积分或枚举类型(12.3).如果条件是类类型,则通过调用该转换函数来转换条件,并使用转换结果代替本节其余部分的原始条件
我尝试下面的代码,工作正常.
class Test
{
public:
operator int() { return 1; }
};
int main()
{
Test obj;
switch(obj)
{
case 1: cout<<"Test class object";
break;
}
}
Run Code Online (Sandbox Code Playgroud)
与使用typeid运算符查找对象类型相比,这是一种更好的方法吗?
在switch case方式中,开销是每个类应该有一个唯一的整数id,它将由转换函数返回.
以typeid方式,如果我们使用类似typeid(obj)== typeid(Test),如果我们有很多类类型,if else链将会很长.代码可读性降低.与switch情况相比可能更慢,因为switch case可能像Compiler的跳转表一样实现
那么,哪种方式更适合在运行时找到对象类型?
编辑:考虑安德烈的评论纠正的问题.
给定一个整数数组,我需要找到最多次出现的数字.我编写了如下算法.
使用地图存储发生的次数和次数.
map<int, int>键:表示数字
值:表示键发生的次数.- 扫描输入数组并使用出现次数和次数更新地图.
- 从开始到结束迭代地图.找到存在最大值的键.该密钥成为发生次数最多的密钥.
我实现了如下算法.
#include <iostream>
#include <map>
using namespace std;
int main()
{
int a[10] = {1,2,3,2,1,3,2,4,1,1}; //Input array: hardcoded for testing
map<int, int> m;
for(int i=0;i<10;i++)
{
m[a[i]]++; //Increment the value of key for counting occurances
}
int mostNumTimes = 0;
int number = -999; //-999 represents invalid number
map<int,int>::iterator it = m.begin();
for( ;it != m.end(); it++) //Find the number which occurred
{ //most number of times
if(it->second > mostNumTimes)
{
mostNumTimes …Run Code Online (Sandbox Code Playgroud) 我们如何使用前导零数来反转数字?例如:如果输入为004,则输出应为400.
我写下面的程序,但只有在输入中没有前导零时它才有效.
int num;
cout<<"Enter number "<<endl;
cin>>num;
int rev = 0;
int reminder;
while(num != 0)
{
reminder = num % 10;
rev = rev * 10 + reminder;
num = num / 10;
}
cout<<"Reverse = "<<rev<<endl;
Run Code Online (Sandbox Code Playgroud)
有没有办法输入带前导零的数字?即便如此,Above逻辑对这些数字也不起作用.
有什么简单的解决 将输入作为字符串并处理它是可行的.但那看起来并不好看.
*编辑:如果已知数字长度,则可以使用前导零反转数字.(不使用字符串)*
我会尽快发布代码.
编辑2:我试图将字符放回到cin流,然后读取并计算反向.它适用于2位数字.
但如果已知长度,则更容易找到反向.我需要做的就是,按所需的次数乘以10.所以我想,我会采用字符串方法.希望面试官会很开心:)