小编P.C*_*ino的帖子

使用##运算符扩展宏

大家好,问题是我在这个宏中

 #define ADD_COMP(s1,s2,type)({\
  int _x=0;\
  for(int i=0;i<n_addrs;i++){\
    if(memcmp(s1,&(s2->##type),6)!=0){\
      _x=-1;\
    }else{\
      break;\
    }\
  }\
  _x;\
})
Run Code Online (Sandbox Code Playgroud)

s1是一个简单的数组,s2是一个结构,有4个向量作为这样的成员

typedef struct example{
 char[6] one,
 char[6] two,
 char[6] three
}example;
Run Code Online (Sandbox Code Playgroud)

现在由于自己的原因,我需要创建一个函数,将大小为6字节的s1数组与仅仅示例的成员进行比较,因此为此我使用##运算符编写ADD_CMP以使其更通用所以我定义了:

#define one
#define two
#define three
Run Code Online (Sandbox Code Playgroud)

我以这种方式使用函数不同时间希望宏扩展的成功

ADD_COMP(some_array,example1,one)
ADD_COMP(some_array,example1,two)
ADD_COMP(some_array,example1,three)
Run Code Online (Sandbox Code Playgroud)

但编译器返回错误:

error: pasting "->" and "one" does not give a valid preprocessing token
error: pasting "->" and "two" does not give a valid preprocessing token
error: pasting "->" and "three" does not give a valid preprocessing token
Run Code Online (Sandbox Code Playgroud)

如何在不为每个结构成员编写相同功能的情况下修复它?

c macros expansion

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

堆栈上的C可用内存

我创建了一些实用程序来帮助我处理DinamicList的管理.在我用来处理列表中元素删除的部分中,如果添加了一个存储在堆栈中的元素,那么当我调用free() 未定义的行为时,就会到达.

在网上冲浪我发现没有办法确定指针是指向堆栈内存还是堆内存.

所以我认为要解决这个问题我必须处理从中生成的错误free().我打电话时有办法处理这个异常free()吗?

c free undefined-behavior

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

尝试创建线程时malloc出错

大家好,我只发布代码的核心,创建problemam和哪些与线程一起使用.

#define HR_OFF h_r-1
pthread_t *threads = NULL;
int h_r = 1;
int foo(int handler)
{
    // if everything is empty alloc resources
    if (threads == NULL) {
        threads = (pthread_t*)malloc(sizeof(pthread_t));
        // stuff with other variables
        h_r++;
    }
    else {
        // stuff with other variables
        threads = (pthread_t*)realloc(threads, sizeof(pthread_t) * h_r);
        h_r++;
    }

    // stuff with other variables
    register unsigned int counter = 0;
    while (pthread_create(&threads[HR_OFF], NULL, (void*)&foo2, NULL) != 0) {
        if (counter == MAX_TRYING) {
            fprintf(stderr, "THREAD_ERROR_C occurs …
Run Code Online (Sandbox Code Playgroud)

c linux malloc multithreading

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