This question already has answers here:
C best practices, stack vs heap allocation
(4 answers)
Closed 6 years ago.
I have a struct like this:
typedef struct {
int hi;
} my_struct;
Is there an advantage in using this:
my_struct *test = malloc(sizeof(my_struct));
test->hi = 1;
Instead of this:
my_struct test;
test.hi = 1;
No, usually it's quite the opposite. If you can use the format to satisfy your requrement
my_struct test;
test.hi = 1;
then it's always better, less overhead in runtime. There's no advantage in general of using memory allocator functions, when you can do without them.
Related
This question already has answers here:
Is the memory allocated for struct members continguous? What if a struct member is an array?
(3 answers)
Closed 3 years ago.
Can I be sure that the following code will work on all platforms?
struct example{
int a;
int b;
} example;
*((int*)(((void*)&example) + sizeof(int))) = 33;
This should change the value of (b) inside (example) to 33.
It will not for sure.
&example + sizeof(int) this operation moves the pointer sizeof(int) * sizeof(example) bytes ahead.
And this line will not compile at all
*(&example + sizeof(int)) = 33;
To know the offset of the particular field in the struct or union use offsetof
http://man7.org/linux/man-pages/man3/offsetof.3.html
This question already has answers here:
How can I access structure fields by name at run time?
(5 answers)
Passing a struct member NAME to function in C?
(1 answer)
How to access members of a `struct' according to a value of a string?
(3 answers)
Closed 5 years ago.
I'm learning about OOP C and I want to write the equivalent of getters and setters.
I have the following struct.
typedef struct {
int item0;
int item1;
int item2;
etc...;
} ItemsStr;
typedef ItemsStr * ItemsPtr;
items_get_item(ItemsPtr item, itemname) {
return item->itemname;
}
I realize that the syntax is invalid but is there a way to accomplish the above?
I also realize that I could write a switch statement but I was wondering if there was a way to use the parameter passed in to deference.
You can use offsetof():
int items_get_item(ItemsPtr items, size_t offset) {
return *(int*)((char*)items + offset);
}
Then the caller does this:
items_get_item(items, offsetof(ItemsStr, item1))
This question already has answers here:
Best approach for struct polymorphism in C [closed]
(3 answers)
easy struct inheritance & pseudo-polymorphism vs strict aliasing
(2 answers)
Is it possible to do inheritance from an abstract/base struct or simulate something along those lines in C?
(3 answers)
Closed 7 years ago.
The problem I'm facing in C is that I'd like to have a series of structs that have a base member from another struct. e.g.
struct foo {
int a;
void (*calculate)(struct foo *);
};
struct bar {
int a;
void (*calculate)(struct foo *);
double b;
};
void do_thing(struct foo *a)
{
a->calculate(a);
}
The problem I'm facing is that the following appears to violate strict aliasing rules.
void foo_calculate(struct foo *a)
{
struct bar *b = (struct bar*)a;
}
The only way I've come up with to do this is to create a union inside struct foo that contains all the structs that inherit from it. Is there an easier way to accomplish this?
This question already has answers here:
variably modified array at file scope in C
(4 answers)
Closed 9 years ago.
I have the following code :
int b = 10; // maximum branching
typedef struct depth * depth;
struct depth{
int number ;
depth child[b] ;//<---- Error here
};
and the following error :
variably modified ‘child’ at file scope
Try this instead:
#define MAX_BRANCHING 10
int b = MAX_BRANCHING; // maximum branching
typedef struct depth * depth;
struct depth{
int number ;
depth child[MAX_BRANCHING] ;//<---- Error here
};
"Variable length arrays" (VLAs) were introduced in C99 and C11, but their use is "conditional" (compilers are not required to implement the feature). In C++, the preferred technique is to use "const int". In C, I would recommend using a #define. IMHO...
http://en.wikipedia.org/wiki/Variable-length_array
If b can't be constant, and you don't want to use heap allocation for the child array, you can use this, rather peculiar workaround (hint: consider NOT using this, but using heap allocation for the array):
typedef struct depth *depth_p;
struct depth
{
int number;
depth_p child[0];
};
The trick is, that the following statement is still valid:
depth_p d = get_depth();
d->child[5]; // <-- this is still valid
In order to use this, you need to create instances of depth_p in this (and only this) way:
depth_p create_depth(int num_children)
{
return (depth_p)malloc(
sizeof(struct depth) + num_children * sizeof(depth_p)
);
}
Firstly, this allocates memory for all the other members (int number) with sizeof(struct depth). Then, it allocates additional memory for the required amount of children by adding num_children * sizeof(depth_p).
Don't forget to free your depth references with free.
Sructs can't have dynamic members so try const int b = 10;
This question already has answers here:
typedef struct vs struct definitions [duplicate]
(12 answers)
Closed 10 years ago.
Such as:
typedef struct _cairo_clip cairo_clip_t;
Why not directly use _cairo_clip? See numerous similar definitions in some code.
The idea behind typedef is to let you skip the struct keyword. Unlike C++, C does not let you do this:
struct _cairo_clip {
int a;
float b;
};
_cairo_clip cc; // Not allowed
struct _cairo_clip cc; // Allowed, but requires a keyword
If you tried to directly use _cairo_clip, you would need to call it struct _cairo_clip.
struct _cairo_clip is more verbose than cairo_clip_t.
Using a typedef also provides some abstraction, because it means that cairo_clip_t can be implemented as either a built-in type or as implemented as a struct, without causing a change in the syntax of the client code.