0

I am trying to write some tests in Catch2 (a C++ library) for a simple C library example and I am a little confused about how to initialize a C struct.

My C header looks like this:

struct node;

And my C implementation cannot be simpler:

struct node {
  int num;
  struct node* next;
}

Now, the problem is with the test (in C++):

#include <catch2/catch.hpp>
extern "C" {
  #include "node.h"
}

TEST_CASE("would it work?", "[foo]") {
  struct node* n = (struct node*) malloc(sizeof(struct node));
}

The code will not compile because "struct node is an incomplete type".

My question, in cases like this, how do you initialize C structs like that one in C++ code? what am I doing wrong?

9
  • 3
    You're taking the sizeof(struct node), but you haven't told the compiler what's in struct node. In other words, the structure definition in the C implementation needs to be visible in the C++ file. That's if you want to allocate memory for the struct in the C++ file. Commented Oct 22, 2021 at 8:24
  • 4
    In a case like that, you need to call whatever function the C API exposes to create the node structures. If there isn't any such function then perhaps the design or the implementation needs to be looked over? And in almost all cases you should never use malloc in C++, even for "C" structures. Generally, whenever you need to do a C-style cast then you should take that as a sign you're doing something wrong. Commented Oct 22, 2021 at 8:24
  • 3
    If this is your real code then you forgot a semicolon after the struct definition. Commented Oct 22, 2021 at 8:30
  • 1
    You probably should put struct node { int num; struct node* next;} in the C header "node.h" instead the C implementation. Commented Oct 22, 2021 at 8:45
  • 1
    Even in a using C application you will have the same problem when you try to allocate a structure. Please show us more of the header file, or correct. Commented Oct 22, 2021 at 8:46

1 Answer 1

1

Basically everything has already been said in the comments.

You are just forward declaring the struct in the header. While it is best practice to use forward declarations to reduce include dependencies in headers that just need to know what something is (e.g. a struct) but not what it contains, it usually doesn't make much sense to forward declare the struct in the header that is actually supposed to define the struct itself.

Using a struct (including malloc) beyond just needing to know that it is a pointer or a reference requires knowledge of the definition of that struct. Note that your C++ file only sees the contents of the included header; it doesn't see the contents of the C file. Therefore, the forward declaration in the header should replaced by the struct definition from the C file, making the C file obsolete in case it was really only intended to define the struct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.