小编Moh*_*ain的帖子

C ++“替换”为具有灵活数组成员的结构

考虑下面的C99结构,以灵活的数组成员结尾:

struct hdr
{
  size_t len;   
  size_t free;  
  char buf[];
};
Run Code Online (Sandbox Code Playgroud)

len例如,使用这样的内联函数(放入头文件中)进行访问,并将其buf作为参数:

static inline size_t slen(const char *s)
{
  struct hdr *h = (struct hdr*)(s - (int)offsetof(struct hdr, buf));
  return h->len;
}
Run Code Online (Sandbox Code Playgroud)

这是库的一部分,将使用C编译器进行编译。但是,我想从C ++访问该库。这实质上意味着相应的头文件(带有适当的extern "C" {...}防护)必须是有效的C ++代码。一种可能的解决方案是slen在源代码主体中定义功能,而完全避免使用内联代码,但这不是最佳选择。

我的想法是定义一个有效的虚拟C ++结构,并且可以通过某种方式将其映射到hdr,例如

struct cpp_hdr
{
  size_t len;
  size_t free;
  char buf[1];
}
Run Code Online (Sandbox Code Playgroud)

请注意,我只希望得到正确的(负)的偏移值lenfree; 没有访问buf的意图。

现在我的问题是:是否有任何保证

static inline size_t slen(const char *s)
{
  struct cpp_hdr *h = (struct cpp_hdr*)(s …
Run Code Online (Sandbox Code Playgroud)

c c++ arrays flexible-array-member

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

在std命名空间中添加模板特化

背景:
我试图回答这个问题为什么我的重载<运算符不能用于STL排序.我的一个建议(除了使用谓词)是在命名空间std中移动自定义operator <,std::string以便编译器可以优先于模板化版本.

以惊人的速度回答了一个高度知名的用户的评论:

这是未定义的行为,不允许向namespace std添加声明,因为它可以更改标准库componens的行为

我的问题:
是否可以为stl类型添加模板特化,即使此特化的声明不包含用户定义的数据类型?


ps我删除了我的答案,因为我担心这可能是错的

c++ stl language-lawyer

5
推荐指数
2
解决办法
635
查看次数

内存分配堆栈

在堆栈中,保留了内存main,我们称之为main函数的堆栈帧.

当我们调用该Add函数时,内存保留在堆栈顶部.在Add函数的堆栈帧,a并且b是本地指针和c是计算的整数,然后我们返回参考.cAdd函数的局部变量.

现在当Add函数执行完成时,堆栈中的内存空间也被释放,所以当我们尝试在mainwith指针中访问这个地址时p,我们试图访问的内容基本上是一个释放空间.编译器发出警告,但为什么它仍然正确打印值5?

答案可能是机器没有释放内存空间,因为它没有必要,因为没有更多的功能.但是如果我们编写另一个函数,Hello那么它肯定应该释放Add调用堆栈中的函数空间,但程序仍会打印

Yay     5
Run Code Online (Sandbox Code Playgroud)

是因为像在堆中我们需要null在释放它之后指定一个指针,否则我们仍然可以访问它?有类似的东西吗?

/* void Hello()
   {
     printf("Yay");
   } */

int* Add(int *a,int *b)
{
  int c=*a+*b;
  return &c;
}

int main()
{
  int a=1,b=4;
  int *p=Add(&a,&b);
  // Hello();
  printf("\t%d",*p);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c c++ pointers memory-management dangling-pointer

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

有没有办法打印对象的位表示?

我正在使用类似下面的内容.有没有更好的办法?

for (int i = 0; i < sizeof(Person) ; i++) {
    const char &cr = *((char*)personPtr + i);
    cout << bitset<CHAR_BIT>(cr);
}
Run Code Online (Sandbox Code Playgroud)

c++ memory

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

处理指针的以下代码有什么问题?

我是否以错误的方式处理指针?我想通过将变量的地址传递给函数来更新变量的值.

void func(int *p){
        int x = 3;
        p = &x;
}

int main(){
        int y = 0;
        func(&y);
        printf("\n Value = %d",y);
}
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

值= 0已退出:ExitFailure 11

c pointers

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

混合std :: move()和std :: thread将无法编译

代码如下:

#include <memory>
#include <thread>

class A
{
  void foo(int&& arg) const {}

  void boo() const 
  {
    int value(0);
    std::thread t(&A::foo, this, std::move(value));
    t.join();
  }

};

int main()
{
  A a;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

MS Visual Studio 2012(工具集v110)给出下一个错误:

错误C2664:'_ Rx std :: _ Pmf_wrap <_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,> :: operator()(const _Wrapper&,_ V0_t)const':无法从'转换参数2' int'到'int &&'

那是什么?我们不能通过线程使用移动语义吗?

c++ c++11

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

在 c 中初始化结构数组

我已经用三个项目初始化了一个结构数组,为我显示了 2 个!!!

#include <stdio.h>

typedef struct record {
    int value;
    char *name;
} record;

int main (void) {
    record list[] = { (1, "one"), (2, "two"), (3, "three") };
    int n = sizeof(list) / sizeof(record);

    printf("list's length: %i \n", n);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?我疯了吗?

c arrays struct

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

C是否有使用malloc初始化结构的简写方法并设置其字段?

我有一个混乱的代码块,如

result = (node*)malloc(sizeof(node));
result->fx = (char*)malloc(sizeof(char) * 2);
result->fx[0]='x'; result->fx[1]='\0';
result->gx = NULL; result->op = NULL; result->hx = NULL;
Run Code Online (Sandbox Code Playgroud)

我在哪里初始化一个类型的元素

typedef struct node
{
    char * fx; // function
    struct node * gx; // left-hand side
    char * op; // operator
    struct node * hx; // right-hand side
} node;
Run Code Online (Sandbox Code Playgroud)

这样做有简便的方法吗?换句话说,有没有办法像在C++中那样做?

result = new node { new char [] {'x','\0'}, NULL, NULL, NULL };
Run Code Online (Sandbox Code Playgroud)

c malloc struct initialization data-structures

5
推荐指数
2
解决办法
497
查看次数

如何在C++中初始化结构内部的union的结构成员?

我想移植一个参数查找表,它是C到C++中的结构数组.我读了几个问题,并且理解C++中不允许使用C风格的struct初始化器.如何将其移植到C++?

typedef struct {
  char const *property;
  int count;
} TYPE2;

typedef struct {
  int Address;             
  char const *Name;         
  union
  {
    TYPE1 foo;
    TYPE2 bar;
  }u;
} PARAMS;

//Initialize table:
const PARAMS paramTbl[] =
{
  {0x1000, "Param 1", {.bar = {"abc",0}}}, //So on ..
  .
  . 
}
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏.

c++ struct

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

为什么我们不应该在 C 中包含源文件

为什么将源文件包含到其他源文件中不是一个好习惯?更好的方法是包含头文件。这种方法有什么好处,反之亦然有什么缺点?请原谅我糟糕的英语。

c code-organization

5
推荐指数
2
解决办法
2636
查看次数