小编Suj*_*ane的帖子

fork命令是否适用于多线程应用程序?

我试图分叉多线程应用程序.似乎fork没有复制我的第二个帖子.

这是我的代码:

#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <linux/unistd.h>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>

using namespace std;

void Loop(const char* zThread)
{
    while (true)
    {
        sleep(2);
        cout << "LOOP : " << zThread << " : " << getpid() << endl;
    }
}

void *MyFunction(void *pData)
{
    Loop("Second");
};

int main()
{
    pthread_t thread1;

    pthread_create(&thread1, NULL, MyFunction, NULL);

    int iPID = fork();

    if (iPID …
Run Code Online (Sandbox Code Playgroud)

c c++ multithreading fork

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

如何合并多个版本的gcda文件?

我正在使用 gcov 来获取我的应用程序的覆盖范围信息。但是,我的应用程序有 3 个实例同时运行,创建了 3 个版本的“gcda”文件。有没有办法在我的覆盖信息文件中合并相同“gcda”文件的不同版本。

我只想将覆盖范围信息作为一个实例。

c++ gcov lcov

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

什么是Python 3中的PyClass_New等价物?

以前我使用C API创建了一些Python类.当我要使用Python 3+构建旧项目时,它会产生以下编译错误

PyClass_New was not declared in this scope
Py_InitModule was not declared in this scope

等价物是什么?

PyObject *pClassDic = PyDict_New();
PyObject *pClassName = PyBytes_FromString("MyClass");
PyObject *pClass = PyClass_New(NULL, pClassDic, pClassName);
Run Code Online (Sandbox Code Playgroud)

c python python-c-api

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

当在堆栈中创建对象时,即使具有100%的代码覆盖率,功能覆盖也较少

我正在用gcov分析我的代码.它说在堆栈中创建对象时,我的代码是2个函数.但是,当我做新删除100%功能覆盖是实现的.

码:

class Animal
{
public:
    Animal()
    {
    }
    virtual ~Animal()
    {
    }
};

int main()
{
    Animal animal;
}
Run Code Online (Sandbox Code Playgroud)

我执行的命令用于生成gcov报告.

rm -rf Main.g* out.txt a.out coverage;
g++ -fprofile-arcs -ftest-coverage -lgcov -coverage Main.cpp;
./a.out;
lcov --capture --directory . --output-file out.txt;
genhtml out.txt --output-directory coverage;
Run Code Online (Sandbox Code Playgroud)

生成的htmls显示我的功能覆盖率为3/4 - 75%.

但是一旦我将堆栈对象更改为堆,

码:

class Animal
{
public:
    Animal()
    {
    }
    virtual ~Animal()
    {
    }
};

int main()
{
    auto animal = new Animal;
    delete animal;
}
Run Code Online (Sandbox Code Playgroud)

我的功能覆盖率是100%.

只有在调用"new"和"delete"时才会调用哪些隐藏函数?

c++ gcov lcov

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

有没有办法覆盖C语言中的malloc / free函数?

有没有办法从自身的C应用程序挂接malloc / free函数调用?

c malloc free

5
推荐指数
3
解决办法
5414
查看次数

Linux内存管理开销

我试图解释我在Linux中应用程序的内存.我做了一个基本的测试,并发现如果我们新建一些内存,它为一个新的分配了至少32个字节.

这是我的代码.

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, const char** argv)
{
        int iBlockSize = atoi(argv[1]);
        int iBlockCount = atoi(argv[2]);

        for (int i = 0 ; i < iBlockCount ; i++)
        {
                cout << (int*)(new char[iBlockSize]) << endl;
        }
        return 0;
};
Run Code Online (Sandbox Code Playgroud)

当我执行./a.out 8 100它时给出了以下结果.

....
....
....
0xf6db10
0xf6db30
0xf6db50
0xf6db70
0xf6db90
0xf6dbb0
0xf6dbd0
0xf6dbf0
0xf6dc10
0xf6dc30
0xf6dc50
0xf6dc70
Run Code Online (Sandbox Code Playgroud)

我得到的所有内存都有32个字节的间隙.

直到24(BlockSize)它是一样的.如果超过24,则为48字节.

./a.out 25 100

....
....
....
0x18b30c0
0x18b30f0
0x18b3120
0x18b3150
0x18b3180
0x18b31b0 …
Run Code Online (Sandbox Code Playgroud)

c++ linux memory

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

调用 JNI_CreateJavaVM 函数两次

我正在使用一个调用JNI_CreateJavaVM库代码内的函数的库。但是,我还需要一些 JNI 包装,并且需要调用相同的函数JNI_CreateJavaVM来获取JNIEnv*我的应用程序。

但第二次调用失败。

有什么办法可以做到这一点吗?

该库不支持获取或设置JNIEnv*库内创建的内容。

c c++ java java-native-interface

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

使用 std::bitset 进行双重表示

在我的应用程序中,我试图显示双变量的位表示。它适用于较小的双变量。不适用于 10^30 级别。

代码:

#include <iostream>
#include <bitset>
#include <limits>
#include <string.h>

using namespace std;

void Display(double doubleValue)
{
    bitset<sizeof(double) * 8> b(doubleValue);
    cout << "Value  : " << doubleValue << endl;
    cout << "BitSet : " << b.to_string() << endl;
}

int main()
{
    Display(1000000000.0);
    Display(2000000000.0);
    Display(3000000000.0);

    Display(1000000000000000000000000000000.0);
    Display(2000000000000000000000000000000.0);
    Display(3000000000000000000000000000000.0);

    return 0;   
}
Run Code Online (Sandbox Code Playgroud)

输出:

/home/sujith% ./a.out
Value  : 1e+09
BitSet : 0000000000000000000000000000000000111011100110101100101000000000
Value  : 2e+09
BitSet : 0000000000000000000000000000000001110111001101011001010000000000
Value  : 3e+09
BitSet : 0000000000000000000000000000000010110010110100000101111000000000
Value  : 1e+30
BitSet : …
Run Code Online (Sandbox Code Playgroud)

c++ double bitset

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

const std::shared_ptr 引用向上转换如何工作?

根据我的理解,当 const shared_ptr& 向上转换时,它会创建指向同一(转换)对象的基类的新共享指针。

#include <iostream>
#include <memory>

class Animal
{
};

class Cat : public Animal
{
};

void display(const std::shared_ptr<Animal>& animal)
{
    std::cout << animal.use_count() << std::endl;
}

int main()
{
    auto cat = std::make_shared<Cat>();

    std::cout << cat.use_count() << std::endl;
    display(cat);
    std::cout << cat.use_count() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

上述代码的输出如下。

sujith@AKLJoincggDLEd:~/sujith$ g++  -std=c++17 main.cpp
sujith@AKLJoincggDLEd:~/sujith$ ./a.out
1
2
1
sujith@AKLJoincggDLEd:~/sujith$
Run Code Online (Sandbox Code Playgroud)

我的问题是哪个运营商执行此操作?对于其他引用向上转换,我们是否也可以获得相同的行为?或者是专门针对shared_ptr引用进行处理的?

感谢您对此进行调查。

c++ casting shared-ptr

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

对于原始数据类型,Equal运算符Vs memcmp的性能

我一直在使用memcmp函数来比较我的性能关键应用程序中的2个整数.除了使用相等的运算符之外我不得不使用它,因为我必须一般地处理其他数据类型.但是,我怀疑原始数据类型的memcpy性能,并将其更改为等于运算符.但是,性能的提高.

我刚做了一些简单的测试,如下所示.

使用memcmp

#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char **argv)
{
        int iValue1 = atoi(argv[1]);
        int iValue2 = atoi(argv[2]);

        struct timeval start;
        gettimeofday(&start, NULL);

        for (int i = 0; i < 2000000000; i++)
        {
//              if (iValue1 == iValue2)
                if (memcmp(&iValue1, &iValue2, sizeof(int)) == 0)
                {
                        cout << "Hello" << endl;
                };
        };

        struct timeval end;
        gettimeofday(&end, NULL);

        cout << "Time taken : " << ((end.tv_sec * …
Run Code Online (Sandbox Code Playgroud)

c c++ memcmp

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