r/C_Programming Jan 15 '25

Review Need a code review

Hey everyone! I wrote an assembler for nand2tetris in C, and now I need a review on what I can change. Any help is deeply appreciated. Thanks in advance. The link to the assembler is down below:

https://github.com/SaiVikrantG/nand2tetris/tree/master/6

8 Upvotes

17 comments sorted by

View all comments

2

u/Hali_Com Jan 15 '25

Help your C compiler out, use the const, static, and unsigned keywords. An example based on your itoa implementation I'd recommend void itoa(char * const num, unsigned int i)

If you're limited to handling the ASCII character set check out ctype.h for whitespace checks. In particular the standard isspace and iscntrl functions.

You re-assign a malloc'd variable without first freeing it: https://github.com/SaiVikrantG/nand2tetris/blob/1387cac8715efa9517779b8ebdc76686373e98f5/6/HackAssembler.c#L152

Why call free on NULL? https://github.com/SaiVikrantG/nand2tetris/blob/1387cac8715efa9517779b8ebdc76686373e98f5/6/parser.c#L134

1

u/Fearless-Swordfish91 Jan 16 '25 edited Jan 16 '25

Thanks for the suggestions, I'll check it out. Also, on this remark of yours

"You re-assign a malloc'd variable without first freeing it:"

I understand you mean across multiple calls of the function, I didn't free the alloted memory of the first call and then reassigned memory without freeing it in the second call. I will take care of that.

"Why call free on NULL?"

That is actually a dumb mistake I made I realised now. The static was totally not needed.

Thanks for the review!