Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3181bc7

Browse files
committed
Always use calloc instead of malloc + memset
In most cases, they will be the same. In some cases, calloc() will do less work because it can skip memset() entirely. In other cases, calloc() can even cheat and not allocate any memory on modern operating systems. However, the use of malloc() + memset() will always do the full amount of work.
1 parent 9a582b8 commit 3181bc7

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

‎stack.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ void init_stack(stack_frame_t *stack, size_t entry_size)
44
{
55
memset(stack, 0, sizeof(stack_frame_t));
66
stack->max_size = entry_size;
7-
stack->store = (stack_entry_t *) malloc(sizeof(stack_entry_t) * entry_size);
8-
for (size_t i = 0; i < entry_size; i++)
9-
memset(&stack->store[i], 0, sizeof(stack_entry_t));
7+
stack->store = calloc(sizeof(stack_entry_t), entry_size);
108
stack->size = 0;
119
}
1210

@@ -111,4 +109,4 @@ size_t get_type_size(stack_entry_type_t type)
111109
assert("unable to recognize stack entry's tag");
112110
}
113111
return size;
114-
}
112+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /