C++ dynamic memory explained for beginners 🧠

preview_player
Показать описание
#dynamic #memory #new

Dynamic memory new operator C++ tutorial example explained
Рекомендации по теме
Комментарии
Автор

#include <iostream>

int main () {

char *pGrades = NULL;
int size;

std::cout << "How many grades to enter in?: ";
std::cin >> size;

pGrades = new char[size];

for(int i = 0; i < size; i++){
std::cout << "Enter grade #" << i + 1 << ": ";
std::cin >> pGrades[i];
}

for(int i = 0; i < size; i++){
std::cout << pGrades[i] << " ";
}

delete[] pGrades;

return 0;
}

BroCodez
Автор

This helped me understand pointers so much more than your original video did. Not that your previous vid didn’t make sense, it just feels much more more practically applicable now compared to before. Much thanks

thechudson
Автор

bro youre an awesome teacher, thanks so much for everything

dc
Автор

Hey do you think you could do an episode on std::vectors ? I think a lot of people could get a ton of use out of them.

dynamagon
Автор

//allocating dynamic memory to pointers and printing the no . of items specified by the user in c++
# include<iostream>

using namespace std;

int main()
{
string *ptr= NULL;
int size;
string temp;
cout<<"Enter the no. of cars to print:";
cin>>size;
ptr=new string[size];
for(int i=0;i<size;i++)
{
cout<<"Enter car no."<<i+1<<" or q to quit:";
getline(cin>>ws, temp);
if(temp=="q")
{
break;
}
else
{
ptr[i]=temp;
}
}
cout<<"\nYou entered the following cars:\n";
for(int i=0;!ptr[i].empty();i++)
{
cout<<ptr[i]<<endl;
}
delete[] ptr;
return 0;
}

JackEvans-hfqt
Автор

Thank you so much!! Wish I found this video sooner

ngchn
Автор

#include <iostream>

int main () {

int size;

std::cout << "How many grades to enter in?: ";
std::cin >> size;

char pGrades[size];

for(int i = 0; i < size; i++){
std::cout << "Enter grade #" << i + 1 << ": ";
std::cin >> pGrades[i];
}

for(int i = 0; i < size; i++){
std::cout << pGrades[i] << " ";
}

return 0;
}

is there a disadvantage of doing it without pointer?

willlagergaming
Автор

Would it be better to use vectors instead when unsure of the array size and what it would be?

TheDreamN
Автор

I see, this is where the New keyword came from, as you know C# doesn't run without it!

Dazza_Doo
Автор

Why do you use NULL instead of nullptr? Is there an important difference between the two?

emilgmelfald