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

Facebook – https://www.facebook.com/TheNewBoston-464114846956315/
GitHub – https://github.com/buckyroberts
Google+ – https://plus.google.com/+BuckyRoberts
LinkedIn – https://www.linkedin.com/in/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/
Twitter – https://twitter.com/bucky_roberts

Comments

SenorQuichotte says:

again you need to use uint, you are throwing away half the usable space by using int instead of uint

ori refael says:

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

Sara S says:

I want to post some codes on your forum but I don’t know how to do it! how can I submit a post on your forum? I registered to be a member but I don’t find the link to post on your website! I have a question regarding:
conditional_expression?expression1:expression2

אילן אילן says:

Buckey you are awsome

Akshay Pimpalkar says:

i am struggling to watch the video number 48 and 49…it is not being played or it takes infinite time to buffer…while other videos are running properly. Please +Bucky Roberts

ahvala7 says:

Why couldn’t you ask the user how many numbers he wants to enter-int a, scan this a, and then create int tab[a].

Porus Hiruthayaraj says:

this program does’nt work for me

Porus Hiruthayaraj says:

Hey bucky your program is wrong because the exact average is 40.4 not 56…
my program is :
#include
#include
#include
#include
#include

int main () {

int i, howMany;
int total;
int * pointsArray;

printf(“How many numbers you want to average ? n”);
scanf(” %d”, &howMany);

pointsArray = (int *) malloc(howMany * sizeof(int));

printf(“type it in !!! n”);

for(i=0; i

Maximiliano Contreras says:

free()

Nick Kendall says:

how can i make it printf(“你叫什么名字?”); ?

cy world says:

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;

Walter Ventura says:

How do I make a 2d array with indefinite size?

I want the user to input the number of rows and columns, and just then create the matrix.

Can someone help?

Andjaradji Rooseno Prabowo says:

the average code doesn’t work….I found out that the error comes from initializing the variable “total” it should be “float” from the beginning…..cmiiw…

crist2000a says:

well, I know what is heap but from this example did not get the benefit of using it.

Rob Seaver says:

One thing I learned is that in C (c99) on a Linux box, an uninitialized int in the global (file) scope will default to 0. However, in function scope (i.e. in the main() function), it’s an indeterminate, or bizarre value. In my specific case, the value was 4195824, which was really throwing my total and average off :-D. Apparently, in Windows environments, an uninitialized int always defaults to 0. StackOverflow was quite useful in explaining this issue: http://stackoverflow.com/a/1597426

Honky Tonks says:

How come you don’t just declare total as a double instead of an int and type cast?

Deniel Salvacion says:

1 questions guys, can someone explain to me what is the use of ‘heap’part in the overall program? will wait for ur kind reply :)

Porus Hiruthayaraj says:

hi check out this program please :

#include
#include
#include
#include
#include

int main (void) {

int i;
int howMany;
int total = 0;
int number[howMany];
float average = 0.0;

printf(“how many number do you want to average ?n”);
scanf(“%d”, &howMany);

for(i=0;i

Th3BlackPhoenix says:

Guys i don’t get why in some videos he uses “fGet/fImput” and in other videos he uses “printf/scanf”…?

RumikXxeno says:

Hey Bucky! You forgot to free the memory 😛

Count Dooku says:

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?

Yousef Elsayed says:

Thanks a lot :)

Adin Begovic says:

I dont see the REAL USE of this crap Heap ,for example watch this BEST MATCHS programme  i could have just done it with array :

int i,howmany ;
int array[] ;
printf(“how many numbers do you want to enter ? ” ) ;
scanf(“%d”,  &howmany) ;
for(i=0;i
#include
#include “dev.h”
#include
#include
#include
#include

int main()
{
int i,howmany;
int sum=0.0;
float avg=0.0;
int *Pointsarray;
printf(“how many numbrs u want to enter ?n”);
scanf(“%d”,&howmany);
Pointsarray=(int *) malloc(howmany * sizeof(int));
printf(“enter the numbers n”);

for(i=0;i

Julius Martin says:

how do i use this for strings?

Matthias Novak says:

/*
feel free to comment on this code.
*/

#include
#include

int main()
{
int numSize, i;
int total = 0;
int * pIntArray;
float average = 0.0;

printf(“How many numbers do you want to calculate the average?n”);
scanf(” %d”, &numSize);

pIntArray = (int *) malloc(numSize * sizeof(int));

printf(“Enter them below…n”);

for (i=0; i < numSize; i++) { scanf(” %d”, &pIntArray[i]); total += pIntArray[i]; } average = (float) total / (float) numSize; printf(“Average is %.2fn”, average); free(pIntArray); return 0; }

Mai Long says:

i still don’t get the advantage of using the heap. we can define the memory size by coding like that
int size;
printf(“how many number do you want to average:n”);
scanf(“%d”, &size);
int pointArray[size];

petros petros says:

how it calculates me the average wrong?i mean if i enter 5 numbers lets say 4,4,4,4,4 it will calculate the average 17.200001
why is that? plz help!!

Radu B says:

Enter them then:
23.2
55.4
2.65
65.4
5
9.23
The average of your 6 numbers is: 6564233273361341000000000000000.00

this is what i got after input 6 numbers.What have I done wrong? As I said in a previous comment i did copy/paste the code from the tutorial but modified (except variable i) from int to float.Also I did initialize all variables, set them equal to 0.I also entered free(pointsArray); after i print out the average and should probably mention I use Windows 10.Could it be a Windows 10 bug?

Stuart Brady says:

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

Sn0w CrAB says:

???? it doesn’t work, when I type in two numbers, 50 and 100, it gives me 79.00000?
int main()
{
    int i, howMany;
    int total;
    float average = 0.0;
    int * pointsArray;
    printf(“How many numbers would you like to average? n”);
    scanf(” %d”, &howMany);
    pointsArray = (int * ) malloc(howMany * sizeof(int));
    printf(“Enter them boy! n”);
    for(i = 0; i

Bahareh BozC says:

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?

Christopher Desir says:

Question: Still Confuse with total+=pointsarray. how can all the numbers be store when the variable total is not set 0.0. i though you need to make that variable 0.0 so the total can store ever loop?

Hasan Al Banna says:

i tried to add five numbers 12+12+12+12+12 and the result is 110!

Smart James says:

I’m not native english speaker and I’m wondering what does “tomato tomatoes” mean xd.

Steve man says:

well i can store more than i declare that i can . for example if i allocate memory for four integers i can store five with no compiling error or something. why is that?

AJay Gupta says:

can anyone explain whats use of free(pointername);

keerthi shastry says:

why should we use &pointarray in scanf because we are declaring pointarray as a pointer

Yoni Edelstein says:

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

Vincze Francisc says:

It’s true. You will get the right average only if you set everything to 0 at the beginning. By the way, why isn’t there an “&” before average in the last printf? Shouldn’t be used one there, because it’s a float?

Muhammad ibn paša Hasanović says:

I started watching this video lessons on 1.5 video speed. And now, when I go back to normal, I understand how Bucky is speaking slowly. God damn dude, you are lazy.

ManWisdomOfSuccess says:

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…

Ravi Desai says:

Can you not do this without malloc but just declare int array[n] in stack after n has already acquired value from user input?
Thanks

Qlimakz says:

howcome you dont need to declare your array at the start, all you have is a pointer?

ying tang says:

i have a problem with my code, please can someone help me?
When I type number after the expression”” how many numbers do you want to put in?” and then I type the numbers, it always require me to type one more number. For example, i type 3 to say that I will type 3 numbers , but it ask me to type 4 numbers after the “put your numbers now” expression.
However, the output of total and average is correct, which is the total of first 3 numbers and the average of the first 3 numbers that I typed in.

here is my code.

#include
#include
#include
#include
#include

int main()
{
int i,howMany;
int *num;
int total=0;
float average=0.0;

printf(“how many numbers do you want to put in?n”);
scanf(” %d”,&howMany);

num=(int*)malloc(howMany*sizeof(int));

printf(“put your numbers nown”);
for(i=0;i

Timur Öner says:

I have one question. Then I allocate a smaller amount memory say 3*sizeof(int), it still accepts 4 integers or more. What is explonation of that? Shouldn’t it crash because I enter more number that it can accept?

bhargava reddy says:

Buddy i don’t see where you done wrong but practically the average of the numbers you gave in run is 40.7% 😛 
So the program has done a mistake 😛 
Anyway cool videos you got here bro! :) Impressed! (Y)

cornelxbox25 says:

This works:

#include
#include
#include
#include
#include
#include

int main()
{

int i, howMany, *pPointsArray, total = 0;

printf(“How many numbers do you want to average? n”);
scanf(” %d0″, &howMany);

pPointsArray=(int *) malloc(howMany * sizeof(int));

printf(“Enter them hoss! n”);
for(i=0;i

DeasyProductions says:

I know this might be a stupid question but what is the difference between using your code and something like this:

int newArray[howMany];
int newTotal = 0;
float newAverage = 0.0;

for (i = 0; i < howMany; i++) { scanf(“%d”, &newArray[i]); newTotal += newArray[i]; } newAverage = (float)newTotal / howMany; printf(“My way average is: %f”, newAverage);

VIPIN KUMAR 13BEC0600 says:

hey bucky , i think the average u hv got is wrong , it should be 40.4,,,
plzzz do reply

Christopher Pilcher says:

What is the difference between using malloc and not?    
    int howMany, i;

    scanf(“%d”, &howMany);

    int pointArray[howMany];

    pointArray[0] = 4;

    printf(“%d”, pointArray[0]);

Compared to

    int howMany, i;

    scanf(“%d”, &howMany);

    int pointArray = malloc(howMany * sizeof(int));

    pointArray[0] = 4;

    printf(“%d”, pointArray[0]);

These both function exactly the same on my end. Why use malloc?

Write a comment

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.


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

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles▷ Show Cars▷ Motorbikes▷ Scooters▷ Bicycles▷ Rims & Tires▷ Luxury BoatsFashion▷ Sunglasses▷ Luxury Watches▷ Luxury Purses▷ Jeans Wear▷ High Heels▷ Kinis Swimwear▷ Perfumes▷ Jewellery▷ Cosmetics▷ Shaving Helpers▷ Fashion HatsFooding▷ Chef Club▷ Fooding Helpers▷ Coktails & LiquorsSports▷ Sport Shoes▷ Fitness & Detox▷ Golf Gear▷ Racquets▷ Hiking & Trek Gear▷ Diving Equipment▷ Ski Gear▷ Snowboards▷ Surf Boards▷ Rollers & SkatesEntertainment▷ DIY Guides▷ Zik Instruments▷ Published Books▷ Music Albums▷ Cine Movies▷ Trading Helpers▷ Make Money▷ Fishing Equipment▷ Paintball Supplies▷ Trading Card Games▷ Telescopes▷ Knives▷ VapesHigh Tech▷ Flat Screens▷ Tech Devices▷ Camera Lenses▷ Audio HiFi▷ Printers▷ USB Devices▷ PC Hardware▷ Network Gear▷ Cloud Servers▷ Software Helpers▷ Programmer Helpers▷ Mobile Apps▷ Hearing AidsHome▷ Home Furniture▷ Home Appliances▷ Tools Workshop▷ Beddings▷ Floor Layings▷ Barbecues▷ Aquarium Gear▷ Safe Boxes▷ Office Supplies▷ Security Locks▷ Cleaning ProductsKids▷ Baby Strollers▷ Child Car Seats▷ Remote ControlledTravel▷ Luggages & Bags▷ Airlines Seats▷ Hotel Rooms▷ Fun Trips▷ Cruise Ships▷ Mexico Tours