RE: Simple Shell Program 11-30-2016, 08:02 AM
#11
You don't need '&' before the function name in your function pointer array because they will decay to the appropriate pointer types.
But why do you free args twice here?
There's not much reason to be using the heap here either way for those allocations that stay within main. sizeof(char) is guaranteed to be 1 too, and why not use a macro if you're going to use sizeof() operator to check the size of things? More efficient than using a function that you haven't even marked as inline (sizeof is a compile-time operator that you place within a runtime function, and you can't guarantee that it'll be inlined unless compiler optimization is enabled and deals with that for you):
sizeof() returns size_t though; you could have done:
Or a generic macro:
Lots of other performance improvements can be made.
But why do you free args twice here?
Code:
do
{
printf("%c%s%c%c ", '$', login, '_', '>');
args = getArgs(&args_size);
status = execute(args);
free_a(args, args_size);
free(args);
}
while (status);
free_a(args, args_size);
free(args);
free(login);There's not much reason to be using the heap here either way for those allocations that stay within main. sizeof(char) is guaranteed to be 1 too, and why not use a macro if you're going to use sizeof() operator to check the size of things? More efficient than using a function that you haven't even marked as inline (sizeof is a compile-time operator that you place within a runtime function, and you can't guarantee that it'll be inlined unless compiler optimization is enabled and deals with that for you):
Code:
int num_builtins()
{
return sizeof(builtin_str) / sizeof(char *);
}sizeof() returns size_t though; you could have done:
Code:
size_t num_builtins()
{
return sizeof(builtin_str) / sizeof(*builtin_str);
}Or a generic macro:
Code:
#define SIZEOFA(arr) (sizeof(arr) / sizeof(*arr))Lots of other performance improvements can be made.
(This post was last modified: 11-30-2016, 08:15 AM by bitm0de.)
- mostly braindead monkeys on this forum.



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)





![[Image: pBD38Xq.png]](http://i.imgur.com/pBD38Xq.png)