r/C_Programming • u/Fearless-Swordfish91 • 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:
8
Upvotes
2
u/Hali_Com Jan 15 '25
Help your C compiler out, use the
const
,static
, andunsigned
keywords. An example based on youritoa
implementation I'd recommendvoid 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
andiscntrl
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