通过引用C++捕获所有异常
try {
int i = 0;
int j = 0/i; /* Division by 0 */
int *k = 0;
std::cout << *k << std::endl; /* De-reference invalid memory location. */
}
catch (...) {
std::cout << "Opps!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
无法检测到上述运行时错误.或者,我对C++异常处理功能有错误的期望吗?
通常,当我尝试初始化静态变量时
class Test2 {
public:
static vector<string> stringList;
private:
static bool __init;
static bool init() {
stringList.push_back("string1");
stringList.push_back("string2");
stringList.push_back("string3");
return true;
}
};
// Implement
vector<string> Test2::stringList;
bool Test2::__init = Test2::init();
Run Code Online (Sandbox Code Playgroud)
虽然初始化应该在main函数之前发生(因此,没有线程可以同时访问init),我关心的是:
我使用的是Windows XP,VC6和VC2008编译器.
我目前正在使用VC++ 2008 MFC.由于PostgreSQL不支持UTF-16(Windows用于Unicode的编码),我需要在存储之前将字符串从UTF-16转换为UTF-8.
这是我的代码片段.
// demo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "demo.h"
#include "Utils.h"
#include <iostream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: …Run Code Online (Sandbox Code Playgroud) 上一篇,我有以下代码.
double* a[100];
for (int i = 0; i < 100; i++) {
// Initialize.
a[i] = 0;
}
Run Code Online (Sandbox Code Playgroud)
初始化数组a为0 的目的是,当我迭代删除元素时a,一切都会正常工作,即使没有为元素分配的内存a.
for (int i = 0; i < 100; i++) {
// Fine.
delete a[i];
}
Run Code Online (Sandbox Code Playgroud)
现在,我想利用auto_ptr,以避免手动调用删除.
std::auto_ptr<double> a[100];
for (int i = 0; i < 100; i++) {
// Initialize. Is there any need for me to do so still?
a[i] = std::auto_ptr<double>(0);
}
Run Code Online (Sandbox Code Playgroud)
我想知道,是否需要初始化auto_ptr以保存空指针?我的感觉是否定的.我只是想确认这一点,以便没有任何陷阱.
考虑
#include <cstdio>
int main() {
char a[10];
char *begin = &a[0];
char *end = &a[9];
int i = end - begin;
printf("%i\n", i);
getchar();
}
Run Code Online (Sandbox Code Playgroud)
#include <cstdio>
int main() {
int a[10];
int *begin = &a[0];
int *end = &a[9];
int i = end - begin;
printf("%i\n", i);
getchar();
}
Run Code Online (Sandbox Code Playgroud)
#include <cstdio>
int main() {
double a[10];
double *begin = &a[0];
double *end = &a[9];
int i = end - begin;
printf("%i\n", i);
getchar();
}
Run Code Online (Sandbox Code Playgroud)
以上三个例子也打印9
我可以知道,我应该如何理解9.这是什么意思?
通过引用代码
http://initd.org/psycopg/docs/extras.html#dictionary-like-cursor
>>> rec['data']
"abc'def"
>>> rec[2]
"abc'def"
Run Code Online (Sandbox Code Playgroud)
我想知道他们如何设法创建一个具有元组和字典特征的数据结构?
上一篇,我有一个C++字符串处理代码,能够做到这一点.
input -> Hello 12
output-> Hello
input -> Hello 12 World
output-> Hello World
input -> Hello12 World
output-> Hello World
input -> Hello12World
output-> HelloWorld
Run Code Online (Sandbox Code Playgroud)
以下是C++代码.
std::string Utils::toStringWithoutNumerical(const std::string& str) {
std::string result;
bool alreadyAppendSpace = false;
for (int i = 0, length = str.length(); i < length; i++) {
const char c = str.at(i);
if (isdigit(c)) {
continue;
}
if (isspace(c)) {
if (false == alreadyAppendSpace) {
result.append(1, c);
alreadyAppendSpace = true;
}
continue;
}
result.append(1, c); …Run Code Online (Sandbox Code Playgroud) 最近,我遇到了一种volatile旗帜技术,我可以避免使用synchronized或lock.
http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html
这是使用volatileflag 的代码示例之一.
正确的工作实例
Thread 1
========
// re-odering will not happen. initialize_var...
// will not move below "volatile_initialization_ready = true"
// statement.
initialize_var1();
initialize_var2();
initialize_var3();
volatile_initialization_ready = true;
Thread 2
========
if (volatile_initialization_ready) {
use_var1();
use_var2();
use_var3();
}
Run Code Online (Sandbox Code Playgroud)
但是,我发现我在某些情况下不能使用这种技术,如下所示.
错误的工作示例
Thread 1
========
volatile_going_to_destroy = true;
destroy_var1();
destroy_var2();
destroy_var3();
Thread 2
========
if (volatile_going_to_destroy) {
return; // ignore.
}
// But Thread 1 still able to perform destroy
// when …Run Code Online (Sandbox Code Playgroud) 我有一个全局唯一值,我希望在exe和不同的DLL中使用它.
对于任何希望使用它的项目,它们可能只包括 defs.h
我想知道,我应该声明它
// defs.h
const UINT UNIQUE_MESSAGE =
RegisterWindowMessage(_T("UNIQUE_MESSAGE-{E5476FDB-3E7E-4113-8132-1D87709BC46C}"));
Run Code Online (Sandbox Code Playgroud)
要么
// defs.h
const static UINT UNIQUE_MESSAGE =
RegisterWindowMessage(_T("UNIQUE_MESSAGE-{E5476FDB-3E7E-4113-8132-1D87709BC46C}"));
Run Code Online (Sandbox Code Playgroud)
目前,我没有发现任何与代码消费者不同的观点.是否有任何陷阱?我应该使用static与否?
// OK!
volatile CString* a0;
CString* a1 = const_cast<CString *>(a0);
// error C2440: 'const_cast' : cannot convert from 'volatile CString' to 'CString'
volatile CString b0;
CString b1 = const_cast<CString>(b0);
Run Code Online (Sandbox Code Playgroud)
我在想,为什么const_cast只为指针工作?如何编译第二个案例?