Macro that knows void * allocation type - c

Let have this code as example:
typedef struct {
int value;
char another;
} foo;
typedef struct {
float yet_another;
long really_another;
} bar;
typedef struct {
char* name;
void* data;
} gen;
int main(int argc, char** argv) {
gen foo_gen, bar_gen;
foo_gen.name = "Foo";
foo_gen.data = (foo*) malloc(sizeof(foo));
bar_gen.name = "Bar";
bar_gen.data = (bar*) malloc(sizeof(bar));
((foo*) foo_gen->data)->value = 0;
((bar*) bar_gen->data)->yet_another = 1.0f;
return 0;
}
This code would work ok, so I defined 2 macro to facilitate my work:
#define FOO_DATA(N, D) ((foo*) N->data)->D
#define BAR_DATA(N, D) ((bar*) N->data)->D
But it seems to be too repetitive. I want it to make more generic, making the macro to know which type it should cast. I tried using __auto_type:
#define DATA(N, D) (__auto_type N->data)->D
But it didn't work. It seems too that typeof don't works with macros. How should I do it?

If me and the other commenters understand correctly, you want to be able to do something like this:
struct a {
int field1;
} a;
struct b {
int field2;
} b;
void *self1 = &a;
void *self2 = &b;
int f1 = MAGIC(self1)->field1;
int f2 = MAGIC(self2)->field2;
for some definition of MAGIC.
This is not possible in C.

Expanding on my comment: you would have to group your common fields into another struct and make that the first field of the possible structs in self->data:
struct common {
int foo;
};
struct a {
struct common common;
int bar;
};
struct b {
struct common common;
int baz;
};
#define $(D) (((struct common*) self->data)->D)

Related

Pointer in const C struct

Assuming that the following structures exist...
typedef struct MyFirstStruct
{
uint8_t someContent;
}
typedef struct MySecondStruct
{
MyFirstStruct* firstStructs;
uint8_t firstStructCount;
}
... and a function gets the following Parameter.
const MySecondStruct* const secondStruct
Is it allowed to change any value?
I am sure that this is not correct:
secondStruct->firstStructCount++.
But neither the Compiler nor PC-Lint complains about secondStruct->firstStructs->someContent++.
Is it allowed to do Change someContent because firstStructs is not const or is the behavior undefined?
Thanks!
The values of the nested struct(s) firstStructs can change as long as the pointer does not change. The constness of the pointer prevents the pointer value from changing, but the constness of the struct only means that its values must not change (i.e. the value of the pointer and the count).
You can modify the struct(s) pointed to by firstStructs arbitrarily without changing the pointer. You can also clearly see from the struct definition that this is legal, because firstStructs is a pointer to struct MyFirstStruct, not a pointer to const struct MyFirstStruct.
Here is an example to understand the principle without the const-pointer to const elements:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;
} Simple;
typedef struct {
Simple* s;
int scount;
} Nested;
int main()
{
Nested x;
x.scount = 10;
x.s = malloc(sizeof(Simple) * x.scount);
const Nested n = x;
for (int i = 0; i < n.scount; ++i)
{
n.s[i].x = i;
printf("%d\n", n.s[i].x);
}
}
It is OK and it was easy to check yourself:
typedef struct
{
uint8_t someContent;
} MyFirstStruct;
typedef struct
{
MyFirstStruct* firstStructs;
uint8_t firstStructCount;
}MySecondStruct;
MyFirstStruct fs;
MySecondStruct str = {.firstStructs = &fs};
const MySecondStruct* const secondStruct = &str;
int main()
{
secondStruct->firstStructs->someContent++;
}
but did not as the posted code was full of syntax errors.
You cant of course change the pointer itself:
This will give you errors:
typedef struct
{
uint8_t someContent;
} MyFirstStruct;
typedef struct
{
MyFirstStruct* firstStructs;
uint8_t firstStructCount;
}MySecondStruct;
MyFirstStruct fs[2];
MyFirstStruct ss;
MySecondStruct str = {.firstStructs = fs};
const MySecondStruct* const secondStruct = &str;
int main()
{
secondStruct->firstStructs++->someContent++;
secondStruct->firstStructs = &ss;
}

How to hold a const value in a struct in C (not C++)?

How can I hold a constant value in a struct? If I put const at LEBEL0 I would not be able to assign to it at LEBEL1. But if I do not put const at LEBEL0, then I will get qualifier lost warning at LEBEL1. Is there any way to do this? I came up with a dirty solution (below) but I think there could be a better one...
typedef struct _MyStruct
{
const SomePointerType pktData; <--LABEL0
}MyStruct;
const SomePointerType SomeFunctionICannotModify()
{
…
}
void SomeFunction()
{
MyStruct data;
….
data.pktData = SomeFunctionICannotModify(); <--LABEL1
....
SomeSubFunction(&data);
}
void SomeSubFunction(MyStruct* data)
{
this function does not modify any fields in "data".
}
PS: The code above is a "model", not a real code, to illustrate my problem. It does not have all the codes in my actual program. Please do not ask questions like "why would you do that", "you do not have to do that", "that code does not compile" and so on.
My dirty solution
typedef struct _ConstHolder
{
const SomePointerType pktData;
}ConstHolder;
typedef struct _MyStruct
{
ConstHolder holder;
}MyStruct;
void SomeFunction()
{
MyStruct data;
….
ConstHolder holder = {SomeFunctionICannotModify()};
data.holder = holder;
....
SomeSubFunction(&data);
}
Your question is difficult to understand, but you may be under the misapprehension that you cannot modify pktData after you've assigned to it. This is not true. Consider the following, which is perfectly legal:
const char * str = "hello";
printf("%s\n", str);
str = "goodbye";
printf("%s\n", str);
The const does not say that you cannot reassign the pointer, but instead that you cannot alter the data to which it points.
It looks to me like the problem is that you are using typedef along with const. The thing about C is that const is not as well implemented and usable as it is in C++. And typedef is not well done either.
This article talks a bit about const in C. As does this article as well on const in C.
For instance if you use the following, it will compile with no problems in Visual Studio 2005
typedef char * pchar;
typedef struct {
int i;
int j;
} Thing;
typedef Thing * SomePtr;
typedef struct {
// const SomePtr pSome;
// const pchar mychar;
const char * mychar;
const Thing *pSome;
} MyStruct;
const SomePtr SomePtrFunc ()
{
static Thing jj = {1, 2};
return &jj;
}
int main(int argc, char **argv)
{
MyStruct josey;
josey.pSome = SomePtrFunc();
josey.mychar = "this";
return 0;
}
If I were to then try to modify what is pointed to by josey.pSome by doing something like adding a line of code such as josey.pSome->i = 0; then I will see a compilation error of error C2166: l-value specifies const object.
However if you use the following code with comments switched around, you get an error message of error C2166: l-value specifies const object
typedef char * pchar;
typedef struct {
int i;
int j;
} Thing;
typedef Thing * SomePtr;
typedef struct {
const SomePtr pSome;
const pchar mychar;
// const char * mychar;
// const Thing *pSome;
} MyStruct;
const SomePtr SomePtrFunc ()
{
static Thing jj = {1, 2};
return &jj;
}
int main(int argc, char **argv)
{
MyStruct josey;
josey.pSome = SomePtrFunc(); // <- error C2166: l-value specifies const object
josey.mychar = "this"; // <- error C2166: l-value specifies const object
return 0;
}
Finally what you could do is to make the typedef to include the const qualifier which would then make the const qualifier part of the type:
typedef const char * pchar;
typedef struct {
int i;
int j;
} Thing;
typedef const Thing * SomePtr;
typedef struct {
SomePtr pSome;
pchar mychar;
// const char * mychar;
// const Thing *pSome;
} MyStruct;

Link Multiple Declarations to Same Definition

I have implemented a linked list in C (not C++) that stores pointers to data. I would like to have multiple declarations for its functions (to provide type safety), but have each of them link to the same definition (because there is no actual difference between pointers to different data types, so using the same code reduces space).
Does anyone have any ideas on how to achieve this (or any better ways to do it)? A portable solution is obviously best, but I really just need something that works in GCC.
I believe you might be able to achieve this using typedefs for function prototypes and
casting the generic solution (which deals in void*s) to the specific prototype. This should be safe for compilation because all pointers would be the same size.
Consider this example:
do_something.h:
typedef void (*do_something_with_int_t)(int *i);
extern do_something_with_int_t do_something_with_int;
typedef void (*do_something_with_string_t)(char *s);
extern do_something_with_string_t do_something_with_string;
do_something.c
#include "do_something.h"
void do_something_generic(void* p) {
// Do something generic with p
}
do_something_with_int_t do_something_with_int =
(do_something_with_int_t)do_something_generic;
do_something_with_string_t do_something_with_string =
(do_something_with_string_t)do_something_generic;
As long as do_something_generic is truly datatype-agnostic (i.e. it really doesn't matter what p points to) then this would be OK.
If it's C (not C++), then the following will work just fine. You can adapt the concept to your needs.
tt.h
typedef struct {
int ii;
} Type_1;
typedef struct {
int ii;
} Type_2;
int foo_1(Type_1* ptr) __attribute__((alias("foo")));
int foo_2(Type_2* ptr) __attribute__((alias("foo")));
tt.c
#include <stdio.h>
#include "tt.h"
int main() {
Type_1 t_1;
Type_2 t_2;
foo_1(&t_1);
foo_2(&t_2);
}
int foo(void* arg) {
printf("foo: %p\n", arg);
}
#include <stdio.h>
struct common_type {
int type;
};
struct type1 {
int type;
int value;
};
struct type2 {
int type;
char* p;
};
int func(void *para) {
switch (((struct common_type*)para)->type) {
case 1:
printf("type1,value:%d\n",((struct type1*)para)->value);
break;
case 2:
printf("type2,content:%s\n",((struct type2*)para)->p);
break;
}
}
int main() {
char *s = "word";
struct type1 t1 = {1,1};
struct type2 t2;
t2.type = 2;
t2.p = s;
func((void*)&t1);
func((void*)&t2);
}

Initializing a Struct of a Struct

If I have a struct in C that has an integer and an array, how do I initialize the integer to 0 and the first element of the array to 0, if the struct is a member another struct so that for every instance of the other struct the integer and the array has those initialized values?
Initialisers can be nested for nested structs, e.g.
typedef struct {
int j;
} Foo;
typedef struct {
int i;
Foo f;
} Bar;
Bar b = { 0, { 0 } };
I hope this sample program helps....
#include <stdio.h>
typedef struct
{
int a;
int b[10];
}xx;
typedef struct
{
xx x1;
char b;
}yy;
int main()
{
yy zz = {{0, {1,2,3}}, 'A'};
printf("\n %d %d %d %c\n", zz.x1.a, zz.x1.b[0], zz.x1.b[1], zz.b);
return 0;
}
yy zz = {{0, {0}}, 'A'}; will initialize all the elements of array b[10] will be set to 0.
Like #unwind suggestion, In C all instances created should initialized manually. No constructor kind of mechanism here.
You can 0-initialize the whole struct with {0}.
For example:
typedef struct {
char myStr[5];
} Foo;
typedef struct {
Foo f;
} Bar;
Bar b = {0}; // this line initializes all members of b to 0, including all characters in myStr.
C doesn't have constructors, so unless you are using an initializer expression in every case, i.e. write something like
my_big_struct = { { 0, 0 } };
to initialize the inner structure, you're going to have to add a function and make sure it's called in all cases where the structure is "instantiated":
my_big_struct a;
init_inner_struct(&a.inner_struct);
Here is an alternative example how you would do things like this with object-oriented design. Please note that this example uses runtime initialization.
mystruct.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
typedef struct mystruct_t mystruct_t; // "opaque" type
const mystruct_t* mystruct_construct (void);
void mystruct_print (const mystruct_t* my);
void mystruct_destruct (const mystruct_t* my);
#endif
mystruct.c
#include "mystruct.h"
#include <stdlib.h>
#include <stdio.h>
struct mystruct_t // implementation of opaque type
{
int x; // private variable
int y; // private variable
};
const mystruct_t* mystruct_construct (void)
{
mystruct_t* my = malloc(sizeof(mystruct_t));
if(my == NULL)
{
; // error handling needs to be implemented
}
my->x = 1;
my->y = 2;
return my;
}
void mystruct_print (const mystruct_t* my)
{
printf("%d %d\n", my->x, my->y);
}
void mystruct_destruct (const mystruct_t* my)
{
free( (void*)my );
}
main.c
int main (void)
{
const mystruct_t* x = mystruct_construct();
mystruct_print(x);
mystruct_destruct(x);
return 0;
}
You don't necessarily need to use malloc, you can use a private, statically allocated memory pool as well.

How to use a function pointer in a C struct?

I want to learn more about using function pointers in C structs as a way to emulate objects-oriented programming, but in my search, I've just found questions like this where the answer is simply to use a function pointer without describing how that would work.
My best guess is something like this
#include <stdio.h>
#include <stdlib.h>
struct my_struct
{
int data;
struct my_struct* (*set_data) (int);
};
struct my_struct* my_struct_set_data(struct my_struct* m, int new_data)
{
m->data = new_data;
return m;
}
struct my_struct* my_struct_create() {
struct my_struct* result = malloc((sizeof(struct my_struct)));
result->data = 0;
result->set_data = my_struct_set_data;
return result;
}
int main(int argc, const char* argv[])
{
struct my_struct* thing = my_struct_create();
thing->set_data(1);
printf("%d\n", thing->data);
free(thing);
return 0;
}
But that give me compiler warnings warning: assignment from incompatible pointer type, so obviously I'm doing something wrong. Could someone please provide a small but complete example of how to use a function pointer in a C struct correctly?
My class taught in C does not even mention these. It makes me wonder whether these are actually used by C programmers. What are the advantages and disadvantages of using function pointers in C structs?
The answer given by Andy Stow Away fixes my compiler warning, but doesn't answer my second question. The comments to that answer given by eddieantonio and Niklas R answer my second question, but don't fix my compiler warning. So I'm pooling them together into one answer.
C is not object-oriented and attempting to emulate object-oriented design in C usually results in bad style. Duplicating methods called on structs so that they can be called using a pointer to the struct as I have in my example is no exception. (And frankly, it violates DRY.) Function pointers in structs are more useful for polymorphism. For example, if I had a struct vector that represented a generic container for a linear sequence of elements, it might be useful to store a comparison_func member that was a function pointer to allow sorting and searching through the vector. Each instance of the vector could use a different comparison function. However, in the case of a function that operates on the struct itself, it is better style to have a single separate function that is not duplicated in the struct.
This makes the answer to what is correct more complicated. Is what is correct how to make my above example compile? Is it how to reformat my above example so that it has good style? Or is it what is an example of a struct that uses a function pointer the way C programmer would do it? In formulating my question, I did not anticipate the answer being that my question was wrong. For completeness, I will provide an example of each answer to the question.
Fixing the Compiler Warning
#include <stdio.h>
#include <stdlib.h>
struct my_struct
{
int data;
struct my_struct* (*set_data) (struct my_struct*, int);
};
struct my_struct* my_struct_set_data(struct my_struct* m, int new_data)
{
m->data = new_data;
return m;
}
struct my_struct* my_struct_create()
{
struct my_struct* result = malloc((sizeof(struct my_struct)));
result->data = 0;
result->set_data = my_struct_set_data;
return result;
}
int main(int argc, const char* argv[])
{
struct my_struct* thing = my_struct_create();
thing->set_data(thing, 1);
printf("%d\n", thing->data);
free(thing);
return 0;
}
Reformatting the Style
#include <stdio.h>
#include <stdlib.h>
struct my_struct
{
int data;
};
void my_struct_set_data(struct my_struct* m, int new_data)
{
m->data = new_data;
}
struct my_struct* my_struct_create()
{
struct my_struct* result = malloc((sizeof(struct my_struct)));
result->data = 0;
return result;
}
int main(int argc, const char* argv[])
{
struct my_struct* thing = my_struct_create();
my_struct_set_data(thing, 1);
printf("%d\n", thing->data);
free(thing);
return 0;
}
Demonstrating a Use for Function Pointer in Structs
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct my_struct
{
void* data;
int (*compare_func)(const void*, const void*);
};
int my_struct_compare_to_data(struct my_struct* m, const void* comparable)
{
return m->compare_func(m->data, comparable);
}
struct my_struct* my_struct_create(void* initial_data,
int (*compare_func)(const void*, const void*))
{
struct my_struct* result = malloc((sizeof(struct my_struct)));
result->data = initial_data;
result->compare_func = compare_func;
return result;
}
int int_compare(const void* a_pointer, const void* b_pointer)
{
return *(int*)a_pointer - *(int*) b_pointer;
}
int string_compare(const void* a_pointer, const void* b_pointer)
{
return strcmp(*(char**)a_pointer, *(char**)b_pointer);
}
int main(int argc, const char* argv[])
{
int int_data = 42;
struct my_struct* int_comparator =
my_struct_create(&int_data, int_compare);
char* string_data = "Hello world";
struct my_struct* string_comparator =
my_struct_create(&string_data, string_compare);
int int_comparable = 42;
if (my_struct_compare_to_data(int_comparator, &int_comparable) == 0)
{
printf("The two ints are equal.\n");
}
char* string_comparable = "Goodbye world";
if (my_struct_compare_to_data(string_comparator,
&string_comparable) > 0)
{
printf("The first string comes after the second.\n");
}
free(int_comparator);
free(string_comparator);
return 0;
}
In your struct definition, change it to
struct my_struct
{
int data;
struct my_struct* (*set_data) (struct my_struct*,int);
};
and now use the above function pointer in main as
thing->set_data(thing,1);

Resources