I need to dynamically declare AvahiClient*. So for that I used
AvahiClient *c = (AvahiClient*)malloc(sizeof(AvahiClient))
but this and all other methods to get size of AvahiClient gives error of the form incomplete type ‘AvahiClient’ {aka ‘struct AvahiClient’}
. So is there a way to get size of AvahiClient?
Asked
Active
Viewed 50 times
1

mm12
- 11
- 2
-
1I'm not a "avahi" user so I'm not 100% sure but it seems that `AvahiClient` is an opaque struct that you are not supposed to create using `malloc`. Seems it created using the function call: `avahi_client_new` See: https://www.avahi.org/doxygen/html/client_8h.html#a07b2a33a3e7cbb18a0eb9d00eade6ae6 Notice that there is `avahi_client_free` to free the object when done. – Support Ukraine Jul 20 '20 at 07:21
-
From what I can tell, `AvahiClient` is just a `typedef` forward declaration, e.g. `typedef struct AvahiClient AvahiClient;` It is the up to you to assign the return of `avahi_client_new (...)` to the pointer you declare. The call requires the following parameters `( const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *error)` (explained in `/usr/include/avahi-client/client.h)` With this type setup, I'm not sure that `sizeof yourAvahiClient;` would have much meaning other than allowing you to allocate an array of them.. – David C. Rankin Jul 20 '20 at 07:21
-
@DavidC.Rankin How we should do if we want to pass the client pointer from main thread to a child thread ? Since we can not malloc it, we will have to create a new client in the child thread or will have to make the client global. Is there any other way I can pass the pointer from parent to child ? – Deepak Patankar Jul 20 '20 at 07:25
-
1@DeepakPatankar Seems the `avahi_client_new` function returns the pointer to you. After that you can pass the pointer as you like. It is just as good as any "malloced" pointer. – Support Ukraine Jul 20 '20 at 07:32
-
It is `malloc'ed` behind the scenes in `/usr/include/avahi-common/malloc.h` There is a fixed `size_t size` that corresponds to the macro `AVAHI_GCC_ALLOC_SIZE(1)` so I suspect that is where you get your actual allocated size from. I have never liked this "I want to be C++" approach in C -- for these reasons. So you will get an allocated block back, but if and how you get the size will have to come from the documentation. – David C. Rankin Jul 20 '20 at 07:32
-
Thanks a lot @DavidC.Rankin for the help! – Deepak Patankar Jul 20 '20 at 07:57