小编sil*_*eth的帖子

使用未分配的内存没有错误?

为什么这样做?

#include <iostream>
using namespace std;

int main() {
    float* tab[3];

    int i = 0;
    while(i < 3) {
        tab[i] = new float[3-i];
        i++;
    }

    cout << tab[2][7] << endl;
    tab[2][7] = 6.87;
    cout << tab[2][7] << endl;

    i = 0;
    while(i < 3)
        delete[] tab[i];
}
Run Code Online (Sandbox Code Playgroud)

虽然这个没有?

#include <iostream>
using namespace std;

int main() {
    float* tab = new float[3];

    cout << tab[7] << endl;
    tab[7] = 6.87;
    cout << tab[7] << endl;

    delete[] tab;
}
Run Code Online (Sandbox Code Playgroud)

我在Win XP上使用MS VS …

c++ linux windows memory-management

8
推荐指数
3
解决办法
2371
查看次数

可以通过localhost连接到postgres而无需密码,但不能通过127.0.0.1.为什么?

我安装了PostgreSQL 9.4数据库的Kubuntu 14.10桌面.我postgres通过执行SQL 更改了数据库中用户的密码:

ALTER USER postgres PASSWORD 'password';
Run Code Online (Sandbox Code Playgroud)

我可以通过psql -h localhost -U postgres -W并提供密码连接到数据库服务器,我也可以简单地连接而无需密码psql -h localhost -U postgres.

另一方面,如果我运行psql -h 127.0.0.1 -U postgres它会提示我输入之前设置的密码.

localhost127.0.0.1主机及其登录方法有什么区别?它在哪里设置?我localhostpg_hba.conf文件中看到没有相关的条目.

postgresql ubuntu

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

强制Java流尽早执行管道的一部分,以将阻塞任务提交给线程池

我有一个我想要处理的对象列表,Java8流API看起来是最干净和可读的方式.

但是我需要对这些对象执行的一些操作包括阻塞IO(比如读取数据库) - 所以我想将这些操作提交给有几十个线程的线程池.

起初我想过做一些事情:

myObjectList
    .stream()
    .filter(wrapPredicate(obj -> threadPoolExecutor.submit(
            () -> longQuery(obj)          // returns boolean
    ).get())                              // wait for future & unwrap boolean
    .map(filtered -> threadPoolExecutor.submit(
            () -> anotherQuery(filtered)  // returns Optional
    ))
    .map(wrapFunction(Future::get))
    .filter(Optional::isPresent)
    .map(Optional::get)
    .collect(toList());
Run Code Online (Sandbox Code Playgroud)

wrapPredicatewrapFunction只是用于检查的异常重新抛出.

但是,显然,调用Future.get()将阻塞流的线程,直到查询完成给定对象,并且流将在此之前不会进展.因此,一次只处理一个对象,并且线程池没有意义.

我可以使用并行流,但是我需要希望默认ForkJoinPool值足够了.或者只是增加"java.util.concurrent.ForkJoinPool.common.parallelism",但我不想为了那个流而改变整个应用程序的设置.我可以在自定义中创建流ForkJoinPool,但我发现它并不能保证并行度.

所以我最终得到了类似的东西,只是为了保证在等待期货完成之前将所有需要的任务提交给threadPool:

myObjectList
    .stream()
    .map(obj -> Pair.of(obj, threadPoolExecutor.submit(
                    () -> longQuery(obj)             // returns boolean
        ))
    )
    .collect(toList()).stream()                      // terminate stream to actually submit tasks to the …
Run Code Online (Sandbox Code Playgroud)

java java-stream

5
推荐指数
0
解决办法
592
查看次数

使用mingw在Linux上交叉编译Direct3D

如何配置mingw32交叉编译Direct3D Apps for Windows?有可能吗?我实际上已经成功编译了本教程中的代码:http://www.directxtutorial.com/Tutorial9/B-Direct3DBasics/dx9B4.aspx - 使用Kubuntu上的Code :: Blocks和i586-mingw32msvc-g ++.我需要添加#define UNICODE和删除#pragma ...部件才能执行此操作,并且我使用了来自/ usr/i586-mingw32msvc/include的头文件以及来自mingw包的libs.

但是我无法编译本教程中的代码:http://www.directxtutorial.com/Tutorial9/B-Direct3DBasics/dx9B5.aspx

Mingw没有d3dx9.h档案.所以我已经安装了wine1.2-dev与Windows相关的头文件的葡萄酒版本的软件包,但现在我有错误:

#define UNICODE:

-------------- Build: Debug in d3d ---------------

i586-mingw32msvc-g++ -Wall  -g    -I/usr/include/wine/windows  -c /home/silmeth/programowanie/codeblocks/d3d/main.cpp -o obj/Debug/main.o
/home/silmeth/programowanie/codeblocks/d3d/main.cpp: In function ‘int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)’:
/home/silmeth/programowanie/codeblocks/d3d/main.cpp:50: error: invalid conversion from ‘const wchar_t*’ to ‘const WCHAR*’
/home/silmeth/programowanie/codeblocks/d3d/main.cpp:56: error: invalid conversion from ‘const wchar_t*’ to ‘const WCHAR*’
/home/silmeth/programowanie/codeblocks/d3d/main.cpp:56: error:   initializing argument 2 of …
Run Code Online (Sandbox Code Playgroud)

c++ directx mingw cross-compiling mingw32

4
推荐指数
1
解决办法
2960
查看次数

堆栈模板不编译push func

我写了类似堆栈的数据结构:

template<class T>
class Stos {
    class Element {
        public:
        T n;
        Element* prev;
        Element(const T& k = 0): n(k), prev(0) {}
    };
    Element* member;
    Stos(Stos&);
    public:
    Stos(const T&);
    ~Stos();
    unsigned int count;
    T get();
    Element* push(const T&);
    T pop();
    void mod(const T&);
};
Run Code Online (Sandbox Code Playgroud)

和实现(相同的文件):

template<class T>
Stos<T>::Stos(const T& n = 0): count(1) {
    member = new Element(n);
}

template<class T>
T Stos<T>::get() {
    return member->n;
}

template<class T>
Stos<T>::Element* Stos<T>::push(const T& n = 0) {
    Element* point = member; …
Run Code Online (Sandbox Code Playgroud)

c++ stack templates g++

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