Skip to content

Commit

Permalink
Use designated initializer more
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cgzones committed Apr 5, 2024
1 parent 5846b7d commit 869220b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
16 changes: 9 additions & 7 deletions Hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ static size_t nextPrime(size_t n) {
}

Hashtable* Hashtable_new(size_t size, bool owner) {
Hashtable* this;

this = xMalloc(sizeof(Hashtable));
this->items = 0;
this->size = size ? nextPrime(size) : 13;
this->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem));
this->owner = owner;
size = size ? nextPrime(size) : 13;

Hashtable* this = xMalloc(sizeof(Hashtable));
*this = (Hashtable) {
.items = 0,
.size = size,
.buckets = xCalloc(size, sizeof(HashtableItem)),
.owner = owner,
};

assert(Hashtable_isConsistent(this));
return this;
Expand Down
18 changes: 10 additions & 8 deletions Vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ Vector* Vector_new(const ObjectClass* type, bool owner, int size) {

assert(size > 0);
this = xMalloc(sizeof(Vector));
this->growthRate = size;
this->array = (Object**) xCalloc(size, sizeof(Object*));
this->arraySize = size;
this->items = 0;
this->type = type;
this->owner = owner;
this->dirty_index = -1;
this->dirty_count = 0;
*this = (Vector) {
.growthRate = size,
.array = xCalloc(size, sizeof(Object*)),
.arraySize = size,
.items = 0,
.type = type,
.owner = owner,
.dirty_index = -1,
.dirty_count = 0,
};
return this;
}

Expand Down

0 comments on commit 869220b

Please sign in to comment.