小编Who*_*aig的帖子

VS2012编译器奇怪的内存释放问题

我有一个VS2012编译器的奇怪问题,似乎没有出现在GCC中.重新分配过程最终需要几分钟而不是几秒钟.有人对此有任何意见吗?步调试显示在调用RtlpCollectFreeBlocks()时显着挂起.我在调试和发布模式下都有这个问题.我正在运行Windows 7 32位,但我在64位7上遇到了同样的问题.

#include "stdafx.h"
#include <iostream>
#include <stdint.h>
#include <cstdlib>

#define SIZE 500000

using namespace std;

typedef struct
{
    uint32_t* thing1;
}collection;

/*
 * VS2012 compiler used.
 * Scenarios: 
 *  1) Don't allocate thing1. Program runs poorly.
 *  2) Allocate thing1 but don't delete it. Program runs awesome.
 *  3) Allocate thing1 and delete it. Program runs poorly.
 * 
 * Debug or Release mode does not affect outcome. GCC's compiler is fine.
 */
int _tmain(int argc, _TCHAR* argv[])
{ …
Run Code Online (Sandbox Code Playgroud)

c++ memory windows memory-management visual-studio-2012

6
推荐指数
1
解决办法
809
查看次数

如何从字符串中删除所有出现的特定字符?

您好我试图从C字符串中删除一个字符,但输出似乎不正确.例如,如果.输入字符串="Hello"要删除的指定字符="l"我的输出是"HeXXo".我似乎需要在删除char之后推送值?

代码如下:

#include <stdio.h>
#include <stdlib.h>

void squeeze(char str[], char c);

void main (){
  char input[100];
  char c;
  printf("Enter string \n");
  gets(input);
  printf("Enter char \n");
  scanf("%c", &c);
  printf("char is %c \n", c);
  squeeze(input , c );

  getchar();
  getchar();
  getchar();
}

void squeeze(char str[], char c){
    int count = 0, i = 0;   

    while (str[count] != '\0'){
      count++;
    }

    printf("Count = %d  \n", count);
    for ( i = 0 ; i != count; i++){
      if (str[i] == c){
            printf("Found at str[%d] …
Run Code Online (Sandbox Code Playgroud)

c string char

5
推荐指数
1
解决办法
4466
查看次数

如何使用struct作为条件的操作数?

我在C++ 11中有一个简单的结构

struct a {
    int a;
    int b;
    int c;
    ....
}
Run Code Online (Sandbox Code Playgroud)

我想使用这个结构,好像它本身就是一个标量类型,所以我重载了所有运算符.

我找不到如何定义的一个行为是在if语句中使用struct:

a v = {1,2,3};
if (v) { }
Run Code Online (Sandbox Code Playgroud)

是否有一个运算符,我可以重载以启用此行为?我想要标准行为:如果结构中的任何位为1则为真,否则为假.

c++ conditional struct operator-overloading c++11

5
推荐指数
1
解决办法
194
查看次数

如果类的成员是引用,为什么复制对象是非法的?

我遇到了一个测验,说下面第18行中的代码格式不正确,因为"当需要复制的其中一个成员是一个引用时,使用隐式定义的赋值运算符是不正确的."

我无法理解.为什么参考无法复制?为什么16号线合法?第16行与第18行非常相似,复制构造函数仍然需要复制,对吧?

1 #include <iostream>
2
3 struct A
4 {
5   A(int& var) : r(var) {}
6
7   int &r;
8 };
9
10 int main(int argc, char** argv)
11 {
12   int x = 23;
13
14   A a1(x);
15
16   A a2 = a1;
17
18   a2 = a1;
19
20   return 0;
21 }
Run Code Online (Sandbox Code Playgroud)

c++ copy copy-constructor

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

使用多个排序条件对数组进行排序(QuickSort)

我试图找出(使用快速排序算法)如何通过2个标准对结构数组进行排序.比如说我有一个结构:

struct employee{
   char gender[12];
   char name[12];
   int id;
};
Run Code Online (Sandbox Code Playgroud)

说我的输入是:

struct employee arr[3]=
{
    {"male","Matt",1234},
    {"female","Jessica",2345},
    {"male","Josh",1235}
};
Run Code Online (Sandbox Code Playgroud)

我想首先按性别对元素进行排序,然后按升序对ID进行排序.一个例子是,所有的男性首先打印出他们的ID,然后是所有的女性.我试图这样做而不使用qsort函数,但我没有丝毫想知道如何检查.这是我的排序功能:

void quicksort(struct employee *arr, int left, int right)
{
    int pivot, l, r, temp;
    if(left < right)
    {
        p = left;
        l = left;
        r = right;
        while(l < r)
        {

            while(arr[l].id <= arr[p].id && l <= right)
                l++;
            while(arr[r].id > arr[p].id && r >= left)
                r--;

            if(l < r)
            {
                temp = arr[l].id;
                arr[l].id = arr[r].id;
                arr[r].id = temp; …
Run Code Online (Sandbox Code Playgroud)

c arrays sorting structure quicksort

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

加密模板模板参数错误

我正在尝试创建一个从a std::map或an 获取键的函数std::unordered_map。我可以使用简单的重载,但首先我想知道这段代码有什么问题。

template<typename K, typename V, template<typename, typename> class TContainer>  
std::vector<K> getKeys(const TContainer<K, V>& mMap)
{
    std::vector<K> result;
    for(const auto& itr(std::begin(mMap)); itr != std::end(mMap); ++itr) result.push_back(itr->first);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

当使用调用它时std::unordered_map,即使手动指定所有模板类型名称,clang ++ 3.4 也会说:

template template参数的模板参数与其对应的模板template参数不同。

c++ templates types typename c++11

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

为什么将enum标记为导出/导入中断Doxygen生成?

使用Doxygen,我偶然发现了这个警告:

D:/<some/path>/Camera.h:20: warning: documented symbol `enum DLLPORT 
ct::CameraCapture::ct::CameraCapture::CamType' was not declared or defined.
Run Code Online (Sandbox Code Playgroud)

现在我知道为什么Doxygen没有找到那个类(命名空间显然是重复的),我不明白为什么它甚至在搜索它.这个枚举是在一个头文件中,直接在一个类定义之上,并且找到了很好的类,它也没有生成那些双重命名空间.源代码也编译,所以它可能不是导致Doxygen这些问题的语法错误.具体来说,源代码如下所示:

#ifdef CT_EXPORTS
#define DLLPORT __declspec(dllexport)
#else
#define DLLPORT __declspec(dllimport)
#endif

#include <somelibrary>

namespace ct {
namespace CameraCapture {

/**The type of camera used:
*[...]
**/
enum DLLPORT CamType {
    CT_ENUM1=0,
    CT_ENUM2,
    CT_ENUM3,
    CT_NONE
};

/**\brief A parent-class to cameras of all types.
*[...]
**/
class DLLPORT Camera
{
   //...some content...
};
}
}
Run Code Online (Sandbox Code Playgroud)

其他enum块也会出现同样的问题.希望你们中的一些人知道那里发生了什么.

干杯

c++ enums doxygen

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

并行mergesort C++中的性能问题

我试图使用线程和模板编写mergesort的并行实现.相关代码如下所示.

我已经将性能与C++ STL中的排序进行了比较.当没有线程产生时,我的代码比std :: sort慢6倍.使用变量maxthreads(和/或FACTOR)我只能将性能提高一倍,因此在最好的情况下,我比std :: sort慢3倍.我已经在16核多处理器机器上尝试了代码.

htop显示核心是按预期使用的,但为什么缺乏性能而我感觉不到整体运行时的并行性?

有错误吗?

谢谢你的回复.

#define FACTOR 1
static unsigned int maxthreads = FACTOR * std::thread::hardware_concurrency();

unsigned int workers=0;
std::mutex g_mutex;

template <typename T>
std::vector<T>* mergesort_inplace_multithreading(
    typename std::vector<T>::iterator* listbegin,
    typename std::vector<T>::iterator *listend,
    std::vector<T>* listarg)
{
    if (*listbegin == *listend)
    {
        return listarg;
    }
    else if (*listend == *listbegin + 1)
    {
        return listarg;
    }
    else
    {
        size_t offset = std::distance(*listbegin, *listend)/2;
        typename std::vector<T>::iterator listhalf = *listbegin + offset;
        g_mutex.lock();
        if (::workers <= maxthreads-2 and maxthreads …
Run Code Online (Sandbox Code Playgroud)

c++ recursion performance mergesort multithreading

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

C++自动从const char到string的隐式转换

好的,我只是第一次学习模板,所以我在玩弄创建我自己的模板类,模仿它的基础类型是一个向量.请记住,对push_back的调用只调用底层向量的push_back方法.

vector<string> sV;
sV.push_back("ha");       //ok: converts from const char[3] to string

Foo<string> fS;
fS.push_back("ha");      //error: const char[3] does not match the argument list
Run Code Online (Sandbox Code Playgroud)

有没有办法解决这个问题?我只是希望我的模板感觉自然就像我正在使用真实的东西一样.


编辑:这基本上是班级的主体

template <typename T> class FooPtr;
template <typename T>
class Foo{
    friend class FooPtr<T>;
public:
    Foo() {data->make_shared(vector<T>); }
#ifdef INITIALIZER_LIST
    Foo(initializer_list<T>);
#endif
    void push_back(T &t) { data->push_back(t); }
    void push_back(T &&t) { data->push_back(move(t)); }
    bool empty() { if (data->size() == 0) return true; }
    FooPtr<T> insert(size_t, T&);
    T& operator[](size_t);
    T& front();
    T& back();
    FooPtr<T> begin() { …
Run Code Online (Sandbox Code Playgroud)

c++ string type-conversion implicit-conversion

3
推荐指数
1
解决办法
3649
查看次数

错误“此声明没有存储类或类型说明符”

我正在使用 Visual Studio 编写我的第一个应用程序,但我不明白它向我显示的错误。

有两个文件,Session 和 Login。登录使用Session的set和get函数。如下所示,Login 调用“setCurrentLang”,这是 Visual Studio 在 Login.cpp 上显示的消息:“此声明没有存储类或类型说明符”。如果我编译,这就是错误:

“错误 26 错误 C2365:‘setCurrentLang’:重新定义;先前的定义是‘function’(....)\GUI\Login.cpp”。

这是 Session.cpp 文件:

#include "Session.h"
const char* CURRENT_LANG;
void setCurrentLang( char* lang){
    CURRENT_LANG = strdup(lang);
}
const char* getCurrentLang(){
    return CURRENT_LANG;
}
Run Code Online (Sandbox Code Playgroud)

会话.h

#ifndef __SESSION_H__
#define __SESSION_H__

#include <cstring>
#include <stdio.h>

void setCurrentLang( char* lang);
const char* getCurrentLang();

#endif
Run Code Online (Sandbox Code Playgroud)

登录.cpp

#include "Login.h"
#include "../data/Session.h"

setCurrentLang("English"); 
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助!

c c++ visual-studio-2013

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