C Programming Tutorial # 33 – malloc() and free() – Dynamic Memory Allocation – Part 1 [HD]

Visit http://www.CPlusPlusTutor.com
In this tutorial we learn about dynamic memory allocation using malloc(). We also learn about freeing up the dynamically allocated memory. We also see why it is not a good idea to create long local variables since they are created on stack.

This tutorial is part of the following series:


CPlusPlus TutorDotcom says:

you will be surprised how much C is still used.

CPlusPlus TutorDotcom says:

Not in C. new can be used in C++, not in C. For C we are stuck with malloc 🙂 Dont forget to visit CPlusPlusTutor. com

Abdel Travels says:

when i do it in dev-c++ the compiler declare an error (invalid conversion ffrom ‘ void*’ to ‘int*’
please can you help me

CPlusPlus TutorDotcom says:

Well, there may be a slight difference in speed between C code, and C++, the advantages that come with using C++ out-weight that slight difference. C++ is way more powerful than C to develop complex systems relatively quickly. It is much more modular and hence easy to maintain as well. All the cool things that come with using Object Oriented paradigm apply here.

Chriscs7 says:

Check my channel guys for C programming tutorials

DJTrancenergy says:

New for C++
malloc for C

Eric Faytone says:

Stack ! ~ haha

Natesh Mayuranathan says:


What’s your point? It’s still an industry standard.

steve frt says:

Thanks you so much

will34uk says:

@pithikoulis The newest C standard allows this, I believe; arrays on the stack without a constant length used to be illegal and so many people heard it and think it’s wrong

Ahmad Lutfi says:

what’s the name of the compiler you’re using sir?

Kan Rup says:

nice explanation. not sure if I caught how to use free function.

thinkpad20 says:

There are advantages and disadvantages of C versus C++ or any other programming language, and I won’t claim that C is a “better” programming language than C++ (though there are many who would, and those who would claim the opposite), but it’s certainly not obsolete. Its simplicity, ubiquity, efficiency and access to low-level functions make it very useful. And if nothing else C is still #1 on the TIOBE index meaning that it’s the #1 most-used programming language in the world.

CPlusPlus TutorDotcom says:

you are talking about embedded programming. Sure C++ can be used for embedded programming. I do it every day :-).

hunchbacked says:

Yes, I know, for speed.

cash says:

His voice gets increasingly loud xD

Very good tutorial btw, for learning malloc(), free() etc.

Manos Seferidis says:

@amino0o i just tryied that code and it works..

farmdve says:

Thank you for the tutorial. I was wondering what is this stack and why would i need to ever use malloc.

Fred Santos says:

es um caralho a falar fds

CPlusPlus TutorDotcom says:

Thanks for clarifying.

lundburgerr says:

Well, in order to burn the software into a PIC you need to compile your code into .hex files. Which as far as I know cannot be done for C++ programs.
But it can be done for C, hence the compiler issue.
Although I very much prefer the OO way of programming. C still has it’s advantages over C++ in some circumstances.

J Mac says:

Remove annotations if you want to avoid the downvotes!!

junior72692 says:

Am I the only one that get excited as his voice gets louder?

Lord Falcon says:

Nice video. Very helpful.

joshuademoraes says:

how can I buid an array of chars like that: map [3000][3000] with 3000 rows and 3000 columns?

CPlusPlus TutorDotcom says:

That’s correct.

Hassaan Mahmoud says:

ياعم انت عمال تتكرع و تكح و تنف, قرفتني ف الـ C كله الله يسامحك

Ruturaj Zadbuke says:

Are you indian? Accent seems similar to mine…

Divine says:

Where is part #2? -___-

hunchbacked says:

I know, we use C++ for all our software developments.

Manos Seferidis says:

@amino0o is there an explanation to why this is illegal? i thought it was possible until you said the opposite. I don’t know much C but in other languages I tryied it was possible to do what you mentioned.

UnbreakableBeast says:

good tutorial. i was worried because they complain about part 1 not being here.

Mars Castillon says:

i also prefer C++, but some programs and systems still use C.

hunchbacked says:

C++ is not hardware, C++ is software.
I can understand that some operating systems can be written in C instead of C++, but common users don’t write operating systems.
And C++ compilers can also compile ordinary C programs; the converse is not true.

CPlusPlus TutorDotcom says:

@itsdannyftw Yes, you can use malloc to allocate any number of bytes. Just specify the number. And this number can be input from the user, or it could be the length of a string, whatever.

Ákos Tokaji says:


Beast Beast says:

xplained far better than my teacher

DJTrancenergy says:

I’m not a professional software engineer, I’m still student. But for the limited stuff I know, I’ve been told that much of the core and native stuff of operating systems is written C as it is faster.

 Write a comment


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!