小编bas*_*hrc的帖子

如何在Linux上检查进程的堆大小

我正在写一些代码并且它一直在崩溃.在挖掘转储后,我意识到我超出了最大堆限制(如果我在malloc上添加了检查,生活会更容易).虽然我修复了,有没有办法增加我的堆大小?

PS:这是一个非常相似的问题,但回复对我来说并不清楚.

c linux memory crash heap

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

为什么我不能将this指针显式传递给成员函数?

C++标准(ISO C++ 11)提到在第9.3.1节

可以使用类成员访问语法(5.2.5,13.3.1.1)为其类类型的对象调用非静态成员函数,或者从其类类型调用派生类(第10条)的对象.

尝试使用g ++编译此代码(版本4.8.2)

 class foo{
    public:
         void bar(){
            cout<<"hey there"<<endl;
         }
};
int main(){
    foo obj;
    foo::bar(&obj);
}
Run Code Online (Sandbox Code Playgroud)

给出编译时错误,因为它无法匹配函数的签名.鉴于调用成员函数的标准陈述,我想这是预期的.由于该方法最终会在编译的某个阶段采用类似于bar(foo*)的形式,为什么标准要求成员访问语法调用成员函数?

c++ this c++11

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

如何防止进程产生更多的孩子

我正在为在我的大学局域网上进行ACM-ICPC比赛的在线评委工作.为此,我要求法官可以足够安全,以防止恶意程序在我的服务器上执行.(这种程序的一个例子是)

int main(){
           while(1) fork();
          }
Run Code Online (Sandbox Code Playgroud)

让我们调用这个程序testcode的可执行文件.

该程序将导致我的服务器运行判断冻结.显然我不希望这种情况发生.所以为了防止我尝试使用ptrace.I想出了以下代码:(让我们调用这个代码监视器的可执行文件)

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <sys/reg.h>
#include<stdio.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <sys/reg.h>
#include<stdio.h>
#include<signal.h>
#include<sys/prctl.h>
#include<stdlib.h>
#define NOBANNEDSYS 40
int bannedsys[50]={2,14,12,15,26,37,38,39,39,40,41,42,46,47,48,49,50,60,61,63,72,83,88,120,102,182,183,190};

int main(int argc,char **argv) {
                int insyscall=0;
                if(argc!=2) {
                    fprintf(stderr,"Usage: %s <prog name> ",argv[0]);
                    exit(-1);
                    }

  int status = 0;
  int syscall_n …
Run Code Online (Sandbox Code Playgroud)

c linux ptrace

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

weak_ptr C++中的比较运算符

我仍然是新stl成员的新手.任何人都可以指出为什么这段代码会给出分段错误?

#include<memory>
#include<stdio.h>
#include<map>
#include<set>
#include<string>
using namespace std;
struct StubClass
{
    weak_ptr<string> b;
    int c;
    friend bool operator==(StubClass x,StubClass y);
    friend bool operator<(StubClass x,StubClass y);
    StubClass(weak_ptr<string> x):b(x){c=5;}    
};
bool operator==(StubClass d,StubClass c) { return d.b==c.b;}
bool operator<(StubClass d,StubClass c) { return d.b<c.b; }


int main()
{
    shared_ptr<string> spPtr(new string("Hello"));
    weak_ptr<string> wpPtr(spPtr);
    StubClass hello(wpPtr);
    set<StubClass> helloSet;
    helloSet.insert(hello);
    if(helloSet.find(StubClass(wpPtr))!=helloSet.end()) printf("YAYA");
    else puts("Bye");
}
Run Code Online (Sandbox Code Playgroud)

错误符合

if(helloSet.find(StubClass(wpPtr))!= helloSet.end())printf("YAYA");

更多的研究表明,调用StubClass的比较器函数时会出现问题.我在这里编译程序

编辑:

bool operator==(StubClass d,StubClass c) { return d.b.lock()==c.b.lock();}
bool operator<(StubClass d,StubClass c) …
Run Code Online (Sandbox Code Playgroud)

c++ stl weak-ptr c++11

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

前瞻性声明的缺点是什么?

我想知道在可能的情况下在所有地方使用前向声明是否有任何缺点.这是我的标题只包含声明.

据我所知,使用前向声明可加快编译时间,但我不知道有什么缺点.

例:

啊:

Class A
{
};
Run Code Online (Sandbox Code Playgroud)

BH:

// Should I use and include "a.h" in the cpp file (e.g., a.cpp)
Class A;
Class B
{
    doSomething(A *a);
    A *myA;
};
Run Code Online (Sandbox Code Playgroud)

或者它是否更好用

BH:

#include "a.h"

Class B
{
    doSomething(A *a);
    A *myA;
};
Run Code Online (Sandbox Code Playgroud)

c++ forward-declaration

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

在C#中将12小时格式转换为24小时格式

如何将12小时格式转换为格式的24小时DateTime格式.

我已经尝试将转换DateTime为字符串(24小时格式)并将其转换回Datetime格式但它无法正常工作.

.net c# datetime hour

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

O(n)比O(nlogn)花费更多时间

我在spoj上尝试这个问题.首先,我提出了一种简单的o(blogb)算法(参考问题是什么b).但由于问题的作者提到了b属于[0,10 ^ 7]的约束我不相信如果它会通过.没有出于剪切信念,我将其编码如下

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<string>
#include<cstring>


#define PR(x) cout<<#x"="<<x<<endl
#define READ2(x,y) scanf("%d %d",&x,&y)
#define REP(i,a) for(long long i=0;i<a;i++)
#define READ(x) scanf("%d",&x)
#define PRARR(x,n) for(long long i=0;i<n;i++)printf(#x"[%d]=\t%d\n",i,x[i])
using namespace std;
#include <stdio.h>
struct node {
          int val;
          int idx;
          };

bool operator<(node a,node b){ return a.val<b.val;}
node contain[10000001];
int main(){
          int mx=1,count=1,t,n;
          scanf("%d",&t);
          while(t--){
                count=1;mx=1;
                scanf("%d",&n);
                for(int i=0;i<n;i++){
                        scanf("%d",&contain[i].val);
                        contain[i].idx=i;
                        }
          sort(contain,contain+n);
          for(int j=1;j<n;j++){
                    if(contain[j].idx>contain[j-1].idx)
                            count++;
                            else count=1;
                            mx=max(count,mx);
                                }
                    printf("%d\n",n-mx);
                    } …
Run Code Online (Sandbox Code Playgroud)

c++ caching

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

是否可以在头文件中使用"using namespace"语句?

许多程序员告诉我,using namespace <x>在头文件中有一个语句是个坏主意.我一直盲目地遵循这个建议,直到现在却不明白为什么会如此.现在我正在开发一个包含大量命名空间的非常复杂的项目.有时我发现在开头告诉编译器关于命名空间而不是每次都要输入nested ::'是太诱人了.例:

ALongNameSpaceName::LongerNamespaceName::BasicUtilityFunctionUsedVeryCommonly
Run Code Online (Sandbox Code Playgroud)

这条规则背后的理由是什么?在哪些情况下我可以忽略此规则?

c++ coding-style namespaces

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

是否在由new []操作符未定义行为分配的内存上调用delete运算符?

我非常肯定它是,但如果我正确解释标准(第18.6.1.2节new.delete.array)提到:

void operator delete [](void*ptr)noexcept; 指针.

.13默认行为:调用operator delete(ptr)

因为在默认行为中delete []只调用它的delete(ptr)等价物,为什么调用哪个版本有关系?我尝试使用示例代码来验证这一点并且崩溃使得更明显的是不匹配的新[]和删除确实导致了不好的事情

#include <iostream>
#include <memory>
class foo{
    public:
        void bar(){
            std::cout << "foo's bar" << std::endl;
        }
        ~foo(){
            std::cout << "foo dies after this" << std::endl;
        }
};
int main() {
    std::shared_ptr<foo> x(new foo[10]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何解释标准中的上述引用线?

c++ standards delete-operator c++14

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