C Programming Tutorial - 48 - Creating an Expandable Program using the Heap

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Make sure you free the memory that you malloc'd for pointsArray. As the program is written, there is a memory leak, since pointsArray is not freed. Simply add,

free(pointsArray);

before the return 0.

Just a heads up for those watching and also a good spot to explain what a memory leak is.

tdabboud
Автор

There's a bug in the resulting average. It should be 40.4. The reason is the declared variables all need to be set to zero at the top. Like follows: int i, howMany = 0; int total = 0; float average = 0.0; int * pointsArray = 0;

cyworld
Автор

dude you explain malloc so much better than anyone else, thanks for this!

CherryMarz
Автор

i watched almost all your tutorials, i really like them, u did a good job, now i finally understand C code. thx a lot, don't stop, make more videos about C coding for more interesting things :D it's very fun to learn with u :D

toasterv
Автор

total is not initialized so it gets a random value (actually it gets the value of the previous variable which was at that memory address) and that results in wrong answer. So whenever you add numbers, be sure to have the sum set to 0 (that's a problem in C/C++ afaik, in Java it's set to 0 automatically but it's good practice to always initialize your variables)

panaiotovi
Автор

If you set int total = 0.0; program will work correctly .

vladimirsarac
Автор

There is quite serious flaw in both this and previous tutorial, whenever you use malloc() function you need to add a statement that checks if a function actually allocated the amount of memory that you were asking for, because if it happens that malloc() could not find enough unassigned\free space in RAM when you start feeding elements into your array that is too small due to memory shortage you'll be overwriting other data in RAM that is used by other programs, causing crashes and other problems.

privatefactory
Автор

Bucky, you forgot the 'free'.. there is a memory leak.

oriRelastwish
Автор

This was a tricky and deceptive tutorial since it was not providing the correct average, however after introducing the free(pointsArray) it started working, I also had to change the name of variable 'total' I do not know why...

Thanks anyway for your efforts in explaining this complex concept which I could not understand...

venkatramanmn
Автор

Hey Bucky! You forgot to free the memory :P

RumikXxeno
Автор

Hmm
(21+28+62+87+4) / 5 = 40.4, not 56..
I think we need to initialize total to zero at the beginning

And at the and write this - free(Pointsarray); - to free our memory

ghostlynomad
Автор

Thanks for the tutorial. If you're going to ask the user how large the array must be, then couldn't you have just used a static variable? Such as pointsArray[howMany];

RAINE____
Автор

Bucky I like your tutorials, however I find them hard to view when you use small fonts. It would be nice if you could make your fonts bigger in the program listings so people who have a visual handicap could see what your doing? Thanks...

ManWisdomOfSuccess
Автор

Please help!
In the line
PointArray=(int*) malloc( howmany * sizeof(int))
I put 0 instead of howmany ( to get the meaning of heap) and i put 100. There is still the same answer. Why i get the answer when i put howmany =0?
As there is not enough memory for pointArray?

baharehadokht
Автор

in the for loop, what does total += pointsArray[n] means? If it is adding values of all elements, why dereference is not needed? pointsArray[n] should be a memory value instead of a integer value.

mrmabb
Автор

Is it just me or is the average of 21, 28, 62, 87 and 4  40.4 not 56 also it's good to set the total variable to 0 as you did with average, because you can never be sure that the initial value is 0. Anyways I'm loving your vids, keep up the good work!

martinyordanov
Автор

Now what is the difference between allocating memory via "whatever = (int *) malloc(howMany, sizeof(int) );" and "int whatever[howMany];" ? Is it more than the possibility to free the memory later?

IJustPlayI
Автор

I have but one complaint:

The small font is unreadable.
What happened to zooming in your videos?

KabooM
Автор

So what's the difference between creating an array from malloc and pointers to just doing pointsArray[howMany] ?

YoniEdelstein
Автор

Mine didn't work so I followed Daniel Jansen's advice in the comments and set my total to 0. Now it works fine :-)

stuartbrady
welcome to shbcf.ru