小编R. *_*des的帖子

分段故障

任何人都可以告诉我为什么我试图推动时会出现错误

   #include <stdio.h>


typedef struct Element
{
  struct Element *next;
  void *data;
}Element;

bool createStack(Element **stack)
{
  *stack = NULL;
  return true;
}

bool push (Element **stack, void *data)
{
  Element *new_element = new Element;

  if(!new_element)
  {
    printf("Memory allocation error in push");

    return false;
  }

  new_element->data = data;
  new_element->next = *stack;
  *stack            = new_element;
  return true;

}

bool pop (Element **stack, void *popped_data)
{
  if(!*stack)
  {
    printf("Stack empty");

    return false;
  }


  Element *new_head = new Element;

  popped_data   = (*stack)->data;
  new_head …
Run Code Online (Sandbox Code Playgroud)

c++ ubuntu

0
推荐指数
1
解决办法
162
查看次数

向量上的C++ operator()优化

我正在编写数字代码,其中定义向量操作很有用.例如,如果x和y是充满浮点数的n长向量,那么让x ^ y导致y的第i个元素中的a等于x的第i个元素的某个任意函数是很好的.一个简单的方法是:

#include <vector>
#include <stdio.h>
#include <ctime>

using namespace std;

template <typename T>
void operator^(vector<T> A, vector<T> B){
  typename vector<T>::iterator a = A.begin();
  typename vector<T>::iterator b = B.begin();
  while(a!=A.end()){
    *b = 2*(*a);
    a++; b++;
  }
//for (uint i=0; i<A.size(); i++)
  //B[i] = 2*A[i];
}

int main(int argc, char** argv){

  int n = 10000;
  int numRuns = 100000;
  vector<float> A;
  for (int i=0; i<n; i++)
    A.push_back((float) i);

  vector<float> B = vector<float>(n);
  clock_t t1 = clock();
  for (int i=0; i<numRuns; …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm optimization operators

0
推荐指数
1
解决办法
399
查看次数

隐藏复制构造函数C++

我想创建无法复制的类,所以我将复制构造函数放入私有部分:

class NotCopyable
{
public:
    NotCopyable(const double& attr1, const double& attr2) : _attr1(attr1), _attr2(attr2) {}
    ~NotCopyable(void) {}

private:
    NotCopyable& operator=(const NotCopyable&);
    NotCopyable(const NotCopyable&);
    double _attr1;
    double _attr2;
};
Run Code Online (Sandbox Code Playgroud)

一切都很好,除非我想分配数组:

NotCopyable arr[] =
{
    NotCopyable(1, 0),
    NotCopyable(2, 3)
};
Run Code Online (Sandbox Code Playgroud)

编译器说她无法访问复制构造函数,因为它在私有部分.当我把它放在公共部分时:

class NotCopyable
{
public:
    NotCopyable(const double& attr1, const double& attr2) : _attr1(attr1), _attr2(attr2) {}
    ~NotCopyable(void) {}
    NotCopyable(const NotCopyable&)
    {
        std::cout << "COPYING" << std:: endl;
    }
private:
    NotCopyable& operator=(const NotCopyable&);

    double _attr1;
    double _attr2;
};
Run Code Online (Sandbox Code Playgroud)

程序编译没有错误,但不调用复制构造函数.所以问题是:我如何禁止复制但仍有可能分配数组?

c++ copy-constructor

0
推荐指数
1
解决办法
497
查看次数

C++:声明指针

所以,在指南中,我读到了这句话

char * terry;
Run Code Online (Sandbox Code Playgroud)

不同于说

char* terry; //or
char *terry; // FYI: I understand what these two do.
Run Code Online (Sandbox Code Playgroud)

正如所述

"我想强调,*我们在声明指针时使用的星号()仅表示它是一个指针(它是其类型复合说明符的一部分),不应该与我们看到的取消引用运算符混淆早一点,但也用星号(*)写.它们只是两个不同的东西,用相同的符号表示."

但是我不明白为什么.也许我以错误的方式接受了引用,现在我再次阅读它,但我仍然感到困惑.任何人都可以告诉我这是错误的还是正确的,为什么?

谢谢.

c++ visual-studio

0
推荐指数
1
解决办法
239
查看次数

编译器如何工作,Makefile在前向声明的情况下发挥作用

我在堆栈溢出中遇到了一个建议,提到了何时使用以及何时不向用户转发声明.

我遇到了这个: -

struct X;              // Forward declaration of X

void f1(X* px) {}      // Legal: can always use a pointer/reference
X f2(int);             // Legal: return value in function prototype
void f3(X);            // Legal: parameter in function prototype
void f4(X) {}          // ILLEGAL: *definitions* require complete types`
Run Code Online (Sandbox Code Playgroud)

在编译时,最后一行说非法将失败.

void f3(X); // Works perfectly fine 
Run Code Online (Sandbox Code Playgroud)

因此,所有头文件(.hh)首先由编译器扫描,然后所有.cc文件寻找语法和语义,我们实际上可以定义
void f3(X);as扫描通过头文件编译器将有关于X的成员函数和成员的想法

c++ header-files

0
推荐指数
1
解决办法
143
查看次数

extern模板是c ++ 11扩展意味着什么?

当使用XCode 4.2编译crypto ++时,我收到多个警告,"extern模板的所有实例都是c ++ 11扩展".这意味着什么,这会影响输出可执行文件的兼容性?

c++ xcode warnings clang c++11

0
推荐指数
1
解决办法
702
查看次数

execv是如何做到的?

也许这是一个神奇的问题.我正在尝试创建一个具有以下签名的函数(或等效的const char*const*args)

void ExecuteJavaVM(char** args){

}
Run Code Online (Sandbox Code Playgroud)

我想模拟如何execv处理参数数组,因为您可能知道execv不要求我们放置数组的最大元素数.它只是神奇地计算我们传递的数组中的元素数量作为其第二个参数.

这是execv签名.

intptr_t _execv( 
   const char *cmdname,
   const char *const *argv 
);
Run Code Online (Sandbox Code Playgroud)

某种程度上,execv设法"计算"数组argv中的元素数量.这正是我想要做的.

sizeof几乎没用,因为数组的数据类型衰减为一个简单的指针,所以sizeof很可能总是返回4.

有任何想法吗?有谁知道在哪里可以找到Windows的这些功能的来源?

最诚挚的问候,Ariel

c windows exec

0
推荐指数
1
解决办法
323
查看次数

rvalue对原始整数的引用何时是短暂的或长寿的?

我用TDM-GCC 4.6.1编译器对rvalue引用做了一些实验,并做了一些我无法用理论解释的有趣观察.我想那里的专家帮我解释一下.

我有一个非常简单的程序,不处理对象,但int原语,并定义了2个函数:foo1(通过rvalue引用返回局部变量)和foo2(按值返回局部变量)

#include <iostream>

using namespace std;

int &&foo1();
int foo2();

int main()
{

int&& variable1 = foo1();
//cout << "My name is softwarelover." << endl;
cout << "variable1 is: " << variable1 << endl; // Prints 5.
cout << "variable1 is: " << variable1 << endl; // Prints 0.

int&& variable2 = foo2();
cout << "variable2 is: " << variable2 << endl; // Prints 5.
cout << "variable2 is still: " << variable2 << endl; // Still prints …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference c++11

0
推荐指数
1
解决办法
415
查看次数

C++逻辑问题

已经有一段时间了(去年的Java课程).自从我的学校不提供C++以来,我一直在努力学习C++.我写了一个简单的程序,只是为了测试我到目前为止所学到的东西 - 实际上只是语法 - 然后才进入中间件.无论如何我只想强调我从不寻找答案,我宁愿你质疑我的物流,所以我可以重新思考一些事情,并可能自己完成.我认为既然我可以用Java成功地编写这个,那么在C++中一切都会很好,但我遇到了各种各样的问题.我试图调试并逐步完成,但我仍然不明白为什么我的一些变量没有得到我指定的值.如果你能指出我正确的方向,我会非常感激.

// This program will create any number of teams the user chooses, 
// give each a score and calculate the average of all the teams.

#include <iostream>
using namespace std;

int main(){

    //number of teams
    int teamCount;
    //array to keep scores
    int team[0];
    //total of scores
    int total=0;
    //average of all scores
    int average=0;

    cout<<"How many teams do you want to keep scores of?"<<endl;

    cin>>teamCount;

    //cout<<teamCount;

    //ask the person for the score as many time …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
199
查看次数

如何在C++ 11中使用带有引用类型实例对象的向量?

是否有可能在C++ 11中使用没有可复制对象(有引用实例)的std :: vector?

struct CanNotCopy {
  int& intref_;
  CanNotCopy(int& i) noexcept : intref_(i) {}
  // How do I make move constructor and operator = ?
};

std::vector<CanNotCopy> hoge; // OK
hoge.resize(10); // default constructor required
int j = 123;
hoge[1] = CanNotCopy(j);  // copy constructor required
Run Code Online (Sandbox Code Playgroud)

c++ c++11

0
推荐指数
1
解决办法
541
查看次数