Skip to content

Commit 869220b

Browse files
committed
Use designated initializer more
Designated initializers are supported in C99 and simplify struct initializations. All members not explicitly mentioned are default initialized to 0, and also modern compilers can warn on of those missing ones.
1 parent 5846b7d commit 869220b

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

Hashtable.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@ static size_t nextPrime(size_t n) {
109109
}
110110

111111
Hashtable* Hashtable_new(size_t size, bool owner) {
112-
Hashtable* this;
113-
114-
this = xMalloc(sizeof(Hashtable));
115-
this->items = 0;
116-
this->size = size ? nextPrime(size) : 13;
117-
this->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem));
118-
this->owner = owner;
112+
size = size ? nextPrime(size) : 13;
113+
114+
Hashtable* this = xMalloc(sizeof(Hashtable));
115+
*this = (Hashtable) {
116+
.items = 0,
117+
.size = size,
118+
.buckets = xCalloc(size, sizeof(HashtableItem)),
119+
.owner = owner,
120+
};
119121

120122
assert(Hashtable_isConsistent(this));
121123
return this;

Vector.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ Vector* Vector_new(const ObjectClass* type, bool owner, int size) {
2525

2626
assert(size > 0);
2727
this = xMalloc(sizeof(Vector));
28-
this->growthRate = size;
29-
this->array = (Object**) xCalloc(size, sizeof(Object*));
30-
this->arraySize = size;
31-
this->items = 0;
32-
this->type = type;
33-
this->owner = owner;
34-
this->dirty_index = -1;
35-
this->dirty_count = 0;
28+
*this = (Vector) {
29+
.growthRate = size,
30+
.array = xCalloc(size, sizeof(Object*)),
31+
.arraySize = size,
32+
.items = 0,
33+
.type = type,
34+
.owner = owner,
35+
.dirty_index = -1,
36+
.dirty_count = 0,
37+
};
3638
return this;
3739
}
3840

0 commit comments

Comments
 (0)