Mastering Memory in C: How to use malloc like a pro

preview_player
Показать описание
Dive into the essentials of memory management in C as we explore the malloc function. Short for “memory allocation,” malloc lets your program reserve memory during runtime, a crucial skill for efficient C programming.

We'll walk through examples on how to use malloc, discuss common pitfalls, and share best practices for keeping your code clean and bug-free.

#CProgramming #memorymanagement #malloc #42torials #CodingTips #42Berlin #Born2Code #LearnToCode
Рекомендации по теме
Комментарии
Автор

Pros don't use malloc and free directly, so the title is somewhat misleading. They implement custom memory management strategies which can vary depending on a subsystem by subsystem basis. One element of this is the use of wrapped versions of malloc and free.

HoSza
Автор

I am new to this but shouldn't you set the copy to null explicitly after freeing it? If you didnt inorder to showcase printing garbage then it makes sense

finally-a-girl-is-noone
Автор

I really don't like how you use C89, but whatever.
Your main() should be main(void)
Your len should be size_t because strlen() returns size_t not int
Your duplicate_string() should call exit(EXIT_FAILURE) instead of exit(1)
You glossed over printing copy after it was left dangling without mentioning that this is extremely dangerous
Remember to set copy to null after freeing it so that it isn't dangling

xBiggs
join shbcf.ru