小编MOH*_*MED的帖子

获取内存泄漏导致的内存溢出,应用程序继续运行和分配

我为内存泄漏压力开发了一个小应用程序:

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

int main(int argc, char **argv)
{
    int period = 0;
    int size = 40000001;
    char *buf = NULL;
    if (argc > 1) period = atoi(argv[1]);
    if (period == 0) period = 21;

    for (;;) {
        buf = malloc(size);
        if (buf == NULL) printf ("malloc return NULL\n");
        sleep(period);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

应用程序每20秒进行一次~40M的内存分配(无空闲).我的系统内存是~256M.

我使用top命令检查了压力应用程序消耗的内存:

$top | grep "stress"
  873  5689 root     S    39892  16%   0% ./stress_test
  873  5689 root     S    39892  16%   0% ./stress_test
  873  5689 …
Run Code Online (Sandbox Code Playgroud)

c c++ linux memory memory-management

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

如何根据行字段为每行添加前缀

我有以下文件

aaa1.bbb1.ccc toto
aaa1.bbb1.ddd. titi
aaa2.bbb2.ccc
aaa2.bbb2.eee tza
aaa2.bbb2.ddd.
Run Code Online (Sandbox Code Playgroud)

我想在每行的开头添加一个字符串.如果该行的第一个字段完成,则该字符串将为"OK",如果该行的第一个字段未完成,则.该字符串将为"NOK".

田野的分界是空间

所以基于上面的例子,命令的输出将是:

NOK aaa1.bbb1.ccc toto
OK aaa1.bbb1.ddd. titi
NOK aaa2.bbb2.ccc
NOK aaa2.bbb2.eee aza
OK aaa2.bbb2.ddd.
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

linux bash shell awk sed

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

在2个c文件中包含头文件(包含变量定义)时没有构建错误

我有一个h1.h包含以下变量声明的头文件:

h1.h

struct namespaces
{
    char *soap_env;
    char *soap_enc;
    char *xsd;
    char *xsi;
} ns;
Run Code Online (Sandbox Code Playgroud)

我包括头文件h1.h在2个C文件c1.cc2.c.

c1.c

#include "h1.h"
Run Code Online (Sandbox Code Playgroud)

c2.c

#include "h1.h"
Run Code Online (Sandbox Code Playgroud)

我希望在构建中出错,但我没有.在构建中没有错误也没有警告.

这是正常的吗?

在程序运行时,此类问题是否会导致未定义的行为?

c c++ compilation

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

如何将一些行移动到文件的顶部?

我有这样一个文件:

aaa.bbb.1.ccc
xxx.bbb.21
mmm.ppp
xxx.eee
mmm.qqqq
xxx.hhh.12.ddd
Run Code Online (Sandbox Code Playgroud)

我想移动开始与所有线路xxx.在文件的顶部用一个简单的命令(使用sed,awk,grep...).

所以我的新文件将如下所示:

xxx.bbb.21
xxx.eee
xxx.hhh.12.ddd
aaa.bbb.1.ccc
mmm.ppp
mmm.qqqq
Run Code Online (Sandbox Code Playgroud)

这该怎么做?

linux shell awk sed ash

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

当我在类中重载 cout 运算符时出现编译错误

当我在类中重载 cout 运算符时出现编译错误

我缺少什么?

下面是源代码。当我在类外定义重载运算符时,问题就消失了

#include <iostream>
using namespace std;

class Box {
    public:
                int l, b, h;
        Box(int length, int breadth, int height) : l(length), b(breadth), h(height) {}
#if 1
        ostream& operator<<(ostream& os) {
            os << (l * b * h);
            return os;
        }
#endif
};

#if 0
ostream& operator<<(ostream& os, Box inb) {
    os << (inb.l * inb.b * inb.h);
    return os;
}
#endif


int main(void) {
    Box B(3,4,5);
        cout << B << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

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

如何在svn repo上应用由diff linux命令生成的补丁

我有一个用diff命令生成的补丁:

diff -ruN orig/ new/ > file.patch
Run Code Online (Sandbox Code Playgroud)

我想使用svn实用程序将此补丁应用于svn repo。怎么做?

linux svn patch

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

比较2个数字与汇编

我有以下代码.我想完成汇编代码,如下所示:

int main(void)
{
    int x = 10;

    int i=0;
    label1:


    asm (.....) // code to add here: if i>=x then jump to label2

    printf("%d\n",i);

    i++;
    asm (.....) // code to add here: jump to label 1
    label2:

    printf("out\n");
}
Run Code Online (Sandbox Code Playgroud)

我的机器是x86,操作系统是Ubuntu 12

c assembly

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

如何使"i + = x"作为"i ++"?

以下代码

int i=0;
while(i<10) {
   printf("%d\n", i++);
}
Run Code Online (Sandbox Code Playgroud)

相当于

int i=0;
while(i<10) {
   printf("%d\n", i); i++;
}
Run Code Online (Sandbox Code Playgroud)

但是下面的代码

int i=0;
while(i<10) {
   printf("%d\n", i+=2);
}
Run Code Online (Sandbox Code Playgroud)

相当于

int i=0;
while(i<10) {
   i+=2; printf("%d\n", i);
}
Run Code Online (Sandbox Code Playgroud)

如何使它等同于

int i=0;
while(i<10) {
   printf("%d\n", i); i+=2;
}
Run Code Online (Sandbox Code Playgroud)

同样的 i++

c

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

更改变量值(C程序)

int main(void)
{
  int a=5;
  fun();
  printf("%d",a);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我需要编写函数fun(),以便printf将打印15.我不能改变fun()的原型,即它不能传递任何东西,我不能修改main()函数中的任何东西

c

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

标签 统计

c ×5

linux ×4

c++ ×3

awk ×2

sed ×2

shell ×2

ash ×1

assembly ×1

bash ×1

compilation ×1

memory ×1

memory-management ×1

patch ×1

svn ×1