@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
Could you use malloc to allocate memory for a "fixed number" of bytes that is "specified" by the user? Lets say, you're storing 100 different types of words. Instead of creating a character array such as:
char pWords[10000];
which in this video, you stated is bad, since it might overwrite a different program's stack (which may result in a crash).
Hence, could you do malloc(strlen(charactervariablearrayhereorsomething))? Or it doesn't really matter how much memory you allocate at all?
@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.
another main reason to use dynamic memory allocation in both C and C++ is if you are only going to know the size of that piece of memory at run time. it is illeagel to do this
int x = 100;
int y[x];
but with dynamic memory allocation it is possible.
@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.
when i do it in dev-c++ the compiler declare an error (invalid conversion ffrom ' void*' to 'int*'
please can you help me
abdotassi77 3 weeks ago
good tutorial. i was worried because they complain about part 1 not being here.
UnbreakableBeast 1 month ago
@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
will34uk 4 months ago
Thank you for the tutorial. I was wondering what is this stack and why would i need to ever use malloc.
farmdve 7 months ago
His voice gets increasingly loud xD
Very good tutorial btw, for learning malloc(), free() etc.
thatbennyguy 7 months ago 2
your voice reminds me of a prankcaller in @OwnagePranks :D
20ArvinVelasco10 10 months ago
Where is part #2? -___-
Shorty20122012 1 year ago
C++ IS OLD!!!!!
lepeanut 1 year ago
@lepeanut
What's your point? It's still an industry standard.
CripplingDuality 1 year ago
Could you use malloc to allocate memory for a "fixed number" of bytes that is "specified" by the user? Lets say, you're storing 100 different types of words. Instead of creating a character array such as:
char pWords[10000];
which in this video, you stated is bad, since it might overwrite a different program's stack (which may result in a crash).
Hence, could you do malloc(strlen(charactervariablearrayhereorsomething))? Or it doesn't really matter how much memory you allocate at all?
itsdannyftw 1 year ago
@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 1 year ago
mmmmmmm, now i am getting excited !!!!! thank you.
nawaryoussef1 1 year ago
another main reason to use dynamic memory allocation in both C and C++ is if you are only going to know the size of that piece of memory at run time. it is illeagel to do this
int x = 100;
int y[x];
but with dynamic memory allocation it is possible.
amino0o 1 year ago
@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 1 year ago
@amino0o i just tryied that code and it works..
pithikoulis 1 year ago
@pithikoulis maybe in declaration but certainly not in operations , its down to the compiler
amino0o 1 year ago