小编Che*_*eng的帖子

如何在C++中捕获运行时错误

通过引用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++异常处理功能有错误的期望吗?

c++

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

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)
  1. 在静态变量初始化期间,以下代码线程是否安全?
  2. 有没有更好的方法来静态初始化stringlist,而不是使用单独的静态函数(init)?

虽然初始化应该在main函数之前发生(因此,没有线程可以同时访问init),我关心的是:

  1. 我有一个exe应用程序.
  2. 我的exe应用程序将加载a.dll,b.dll和c.dll
  3. a/b/c.dll反过来会加载common.dll.上面的代码在common.dll中
  4. 我已经验证了.由于3个dll在单个进程内,因此它们将引用相同的静态变量(向量).
  5. 在这种情况下,为了防止3个dll同时访问init(我可以将它们视为3个线程吗?虽然初看起来没有意义),对于init函数,我应该使用临界区来保护它吗?

我使用的是Windows XP,VC6和VC2008编译器.

c++

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

将UTF-16转换为UTF-8

我目前正在使用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)

c++ windows unicode winapi utf

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

是否需要为std :: auto_ptr分配空指针

上一篇,我有以下代码.

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以保存空指针?我的感觉是否定的.我只是想确认这一点,以便没有任何陷阱.

c++

2
推荐指数
2
解决办法
3924
查看次数

"内存地址差异"是什么意思?

考虑

#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.这是什么意思?

c c++

2
推荐指数
2
解决办法
539
查看次数

如何使用元组和字典特征的数据结构

通过引用代码

http://initd.org/psycopg/docs/extras.html#dictionary-like-cursor

>>> rec['data']
"abc'def"
>>> rec[2]
"abc'def"
Run Code Online (Sandbox Code Playgroud)

我想知道他们如何设法创建一个具有元组和字典特征的数据结构?

python psycopg2

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

Pythonic方式重写以下C++字符串处理代码

上一篇,我有一个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)

c++ python

2
推荐指数
2
解决办法
185
查看次数

用volatile标志代替锁定技术

最近,我遇到了一种volatile旗帜技术,我可以避免使用synchronizedlock.

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)

java multithreading

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

在这种情况下我应该使用静态吗?

我有一个全局唯一值,我希望在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与否?

c++ windows

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

为什么const_cast远离volatile只能用于指针

// 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只为指针工作?如何编译第二个案例?

c++ pointers volatile const-cast

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

标签 统计

c++ ×8

python ×2

windows ×2

c ×1

const-cast ×1

java ×1

multithreading ×1

pointers ×1

psycopg2 ×1

unicode ×1

utf ×1

volatile ×1

winapi ×1