7 ms·
Thanks for your feedback! I spent a lot of time chasing down memory leaks with this project, which was fun, but clearly that's not the last of the hurdles with
by jhedwards 11y ago
Thanks for your feedback! I spent a lot of time chasing down memory leaks with this project, which was fun, but clearly that's not the last of the hurdles with C. My dream is to program space robots, which probably will never happen, but I feel one step closer every time I build something in C.
- jotux 11y agoI do a lot of embedded C, so I generally avoid dynamically allocating memory, but when I do the way I try to avoid memory leaks is to always try to allocate and free in the same scope. For example, you malloc in your get_line function but you free in main. I would probably do your line handling in main so the malloc/free are in the same scope. Alternatively, I will sometimes make Create and Destroy functions that do the allocating/freeing but even when I do that I always try to keep the calls within the same scope so it's not easy to loose track of memory. Practically allocating/freeing in the same scope is difficult so it takes a lot of effort to structure your code to accommodate. If you're interested in mission-critical C programming you should check out the JPL C standards: http://lars-lab.jpl.nasa.gov/JPL_Coding_Standard_C.pdf http://lars-lab.jpl.nasa.gov/JPL_Coding_Standard_C.pdf They're a little unrealistic for most non-space projects but it gives you a good idea of how safety-critical software is written in C.
- _sh 11y agoAs well as jotux's excellent reply, I'd like to add that clint, valgrind and friends help out a lot when starting out with C. Learning to use these tools well (by using them all the time), will teach you a lot about the language. I also do a lot of embedded C, and can confirm that dynamic allocation is rare, so why not try restructuring your program to avoid it? It's good practice and will sharpen your skills by raising all sorts of questions like why dynamic allocation is discouraged in embedded systems in the first place, and what alternatives there are, and how to use a profiler to compare different trade-offs. Heaps of exploratory fun to be had! And hey, Graph-Reply is really cool, and so are space robots.
- LukeShu 11y agoI've made a pull request that contains a reasonable mount of feedback. I hope it's useful and instructive!
- e12e 11y agoThose are great reading, thank you (even if I have nothing to do with the project :-). For others: https://github.com/incrediblesound/Graph-Reply/commits?author=LukeShu https://github.com/incrediblesound/Graph-Reply/commits?autho...