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

preview_player
Показать описание
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:
Рекомендации по теме
Комментарии
Автор

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.

thinkpad
Автор

@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.

Learnorama
Автор

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

junior
Автор

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.

DJTrancenergy
Автор

@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

willuk
Автор

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

farmdve
Автор

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

RandomGameHall
Автор

@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.

pithikoulis
Автор

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.

Learnorama
Автор

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

abdotassi
Автор

His voice gets increasingly loud xD

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

thatbennyguy
Автор

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

joshuademoraes
Автор

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.

hunchbacked
Автор

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

obzajd
Автор

you will be surprised how much C is still used.

Learnorama
Автор

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

Learnorama
Автор

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

kanrup
Автор

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.

Lundburgerr
Автор

@lepeanut

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

CripplingDuality
Автор

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

Learnorama
join shbcf.ru