小编jxh*_*jxh的帖子

使用Linux进行实时编程

我刚刚构建并设置了一个应用了RT补丁的vanilla Linux内核.一切都很顺利,我现在可以正确启动进入新内核.

令我疑惑的是:我有一个我在C中制作的模拟器程序,我希望它以硬实时模式执行,新内核应该允许它.可能整个模拟器不需要以实时优先级运行,但内部的一些任务可以.

我怎么能做到这一点?我认为只是运行该程序是行不通的.

c linux kernel real-time hard-real-time

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

我如何使用此调试宏?

using namespace std;
#ifdef DEBUG
     #define debug(args...)            {dbg,args; cerr<<endl;}
#else
     #define debug(args...)              // Just strip off all debug tokens
#endif

struct debugger
{
    template<typename T> debugger& operator , (const T& v)
    {    
        cerr<<v<<" ";    
        return *this;    
    }
} dbg;


int main(){
    int a=1,b=2,c=3;
    debugger(a,b,c);
}
Run Code Online (Sandbox Code Playgroud)

我找到了这个调试宏,我正在尝试使用它,但这不起作用.我收到以下错误:

ubuntu:~ g++ -DEBUG a.cpp -o a
a.cpp: In function ‘int main()’:
a.cpp:81:16: error: no matching function for call to ‘debugger::debugger(int&, int&, int&)’
a.cpp:81:16: note: candidates are:
a.cpp:62:8: note: debugger::debugger()
a.cpp:62:8: note:   candidate expects 0 …
Run Code Online (Sandbox Code Playgroud)

c++ macros

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

如何在C++中重载索引运算符?

我有以下代码声明一个重载的类operator[],如下所示:

#include <iostream>
#include <vector>

using namespace std;

class BitSet
{
private:
    int size;
public:
    vector<int> set;
    int &operator [] (int index) {
        return set[index];
    }
    BitSet(int nsize = 0)
    {
        cout << "BitSet creating..." << endl;
        size = nsize;
        initSet(size);
    }
    void initSet(int nsize)
    {
        for (int i = 0; i < nsize; i++)
        {
            set.push_back(0);
        }
    }
    void setValue(int key, int value)
    {
        set[key] = value;
    }
    int getValue(int key, int value)
    {
        return set[key];
    } …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

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

C ++中受朋友保护的方法

我有一个Foo类,必须在其他类Bar中“直接”访问。我想构建一个小框架,声明Bar方法(这是Foo的朋友方法)受保护的方法。这样,我可以为Bar培养几个班级的孩子。

Gcc抱怨这一点,并且只有在该方法是公开的情况下才起作用。

我能怎么做?我的代码示例:

class Foo;
class Bar {
    protected:
        float* internal(Foo& f);
};
class Foo {
    private:
        //some data
    public:
        //some methods
        friend float* Bar::internal(Foo& f);
};
Run Code Online (Sandbox Code Playgroud)

Gcc讯息:

prog.cpp:4:16: error: ‘float* Bar::internal(Foo&)’ is protected
         float* internal(Foo& f);
                ^
prog.cpp:11:43: error: within this context
         friend float* Bar::internal(Foo& f);
                                           ^
Run Code Online (Sandbox Code Playgroud)

c++ friend-function

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

转换c

我是C的新手,我需要在c中编写一个函数,它将整数转换为指定基数中的字符串并打印出来.

如果我的输入值为1234(基数为10),则应返回2322(基数为8).

这是我目前正在处理的代码结构:

void int2ascii(int value, int base){
    int a=0;
    if (value > base) {
         a = a + int2char(value);     //recursive case
         int2ascii(value/base, base); //base case
    }
    printf("%s\n",a);
 }
Run Code Online (Sandbox Code Playgroud)

程序不会运行,有人可以开导我吗?谢谢

c functional-programming

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

C++中的十进制到十六进制转换代码

我是C++编程的全新手.我已经被赋予了将十进制转换为十六进制转换的任务.这就是我到目前为止所做的.

#include <iostream>

int main()
{
    long dec;
    int rem;

    std::cout << "enter decimal number: ";
    std::cin >> dec;

    while (dec > 0) {
        rem = dec % 16;
        if (rem > 9) {
            switch (rem) {
               case 10: std::cout << "A"; break;
               case 11: std::cout << "B"; break;
               case 12: std::cout << "C"; break;
               case 13: std::cout << "D"; break;
               case 14: std::cout << "E"; break;
               case 15: std::cout << "F"; break;
            }
        }
        else {
            std::cout << rem; …
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么不重新分配工作

我在一个更大的项目中遇到了这个问题,其中由于某种原因,该realloc函数绝对没有任何作用.我错过了一些明显的东西吗

这是一个简化的例子:

#include <stdio.h>
#include <string.h>

int main()
{
    int x = 1, y = 2, z = 3;
    int* arr, *arr1;
    int** arra;

    arra = (int**)malloc(sizeof(int*));
    arr = (int*)malloc(sizeof(int));
    arr[0] = x;
    arra[0] = arr;
    arra = (int*)realloc(arra, sizeof(int*) + sizeof(int*));
    arr1 = (int*)malloc(sizeof(int));
    arr1[0] = y;
    arra[1] = arr1;

}
Run Code Online (Sandbox Code Playgroud)

当我调试时,最后的arra结果是{{1}},即使我的理解应该是{{1},{2}}.

c arrays realloc

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

这个删除是否会破坏每个数组对象?

假设我使用using的变体new来返回指向数组的指针:

class Type { /* ... */ };

typedef Type Type_42[1][42];
Type (*x)[42] = new Type_42;
Run Code Online (Sandbox Code Playgroud)

因为我new以前创建,我delete用来销毁:

delete x;
Run Code Online (Sandbox Code Playgroud)

是否应该调用42个对象的每个析构函数?

c++ language-lawyer

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

仅使用递归编写C函数

我想知道数组中的偶数之和是否等于奇数之和,只使用递归而没有任何附加函数,除了递归和没有任何静态变量.

如果奇数之和等于偶数之和,则函数返回1,否则返回0.数组中的所有数字均为非负数.

函数签名应如下所示:

function(unsigned int a[], int n)
Run Code Online (Sandbox Code Playgroud)

到现在为止我写了以下内容:

function(unsigned int a[], int n)
{ 
    if(n==0) return 0;
    return (a[0]%2)?((a[0]+function(a+1,n-1)):(-a[0]+function(a+1,n-1));
}
Run Code Online (Sandbox Code Playgroud)

此函数返回所需的总和,但不返回问题的答案(如果是,则为1,否则为0).

是的,这是作业的一部分,但如果没有不允许的附加功能,我无法解决它.

c algorithm recursion

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

用于替换嵌套for循环的宏

我发现这个宏#define TIMES(x) for(int i1=0;i1<x;i1++)非常实用,可以缩短代码文本.但是当我有嵌套循环时,我不知道如何编写这样的宏,甚至我不知道是否可能.这个想法如下.是否可以编写此代码

for(int i1=0;i1<5;i1++)
    for(int i2=0;i2<3;i2++)
        for (int i3=0;i3<7;i3++)
        /* many nested `for` loops */
{
    /* some code, for example to print an array printf("%d \n",a[i1][i2][i3]) */
}
Run Code Online (Sandbox Code Playgroud)

TIMES(5) TIMES(3) TIMES(7) ....
{
    /* some code, for example to print an array printf("%d \n",a[i1][i2][i3]) */
}
Run Code Online (Sandbox Code Playgroud)

用一种"递归"宏来检测所有TIMES并通过for循环替换它们与i1,i2,i3,... i'n'循环计数器?

c macros c-preprocessor variadic-macros

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