C Video Tutorial 8

[ad_1]
Get the Code Here :

Welcome to part 8 of my C Video Tutorial. In this video I cover a topic I have received many requests for. This is basically a C Malloc Tutorial.

Malloc() ( Memory Allocator ) is used to dynamically set aside memory at run time. Because malloc() returns a pointer to that location in memory it can be a bit confusing. I’ll cover how to store both regular data types as well as structs using malloc().

Comments

30 responses to “C Video Tutorial 8”

  1. DjNerDee Avatar

    This video makes me really appreciate everything that Python does for you under the hood

  2. Yufan Liu Avatar

    Hi Derek, fantastic tutorial! Couple of questions: on 09:07 why pProducts +1 should be referenced? It is a pointer already,right?

    also on 09:16, the productName is float, so why still using scanf ("%s") instead of "%f"?

    Staill thanks so much for kindly publishing all these amazing tutorials!

  3. Austin Villanueva Avatar

    Hi I've been watching your videos and it helps me a lot to learn very quickly. I just have question about the pointer returned by malloc ,in the example it return as int pointer. but you use it with the [] operator in scanf and printf. Is it because it's the way on how to access the address of the next 4 bytes for the int? even though it is not an array of int but it is a pointer to dynamic number of integer .I just want to clarify . Thank you!!!

  4. Rick Mac Gillis Avatar

    When do you reference a pointer as a pointer or an address of such, and what about reading and writing to a pointer or the value of a pointer, and… well you get the picture. It's way to complicated to just slap down a pointer and expect it to work. Half the time it gets used without a star, then the other times its used as a reference to its address, yet it's all about the context that it's used in as to when a pointer is used in a specific way. Then all the annoying type checks involving pointers are causing errors especially when I'm following the C11 standards. Why can't I just get decent error messages from GCC and expect C to be consistent? A pointer should be a simple way to pass items around without duplication. There's no logical reason why it needs to be this complicated.

  5. Vitali Kuzmin Avatar

    You're the best!! Your tutorials help a lot!
    Would you please make a tutorial about assembly?

  6. alexander shekhtman Avatar

    I get errors with this code, can someone tell me what is wrong please?

    struct product{

    float price;
    char prodName[30];

    };

    int main(){

    struct product *pProducts;

    int i, j;
    int numofproducts;

    printf("Enter the number of products :");
    scanf("%d", &numofproducts);

    pProducts = (struct product *) malloc(numofproducts * sizeof(struct product));

    for (i = 0; i < numofproducts; ++i){

    printf("Enter a products name: ");
    scanf("%s", &(pProducts+i)->prodName);

    printf("Enter a products price: ");
    scanf("%f", &(pProducts+i)->price);

    }

    printf("Products Stored n");

    for(j = 0; j < numofproducts; ++j){

    printf("%st%.2fn", 
    (pProducts+j)->prodName, 
    (pProducts+j)->price);

    }

    free(pProducts);

    return 0;

    }

  7. ProgrammingExperts Avatar

    I would consider wrong the way you are check if there is enough heap memory available, which if it might happen the user should not be able to see the error message produced by compiler 
    you might do as following
    if(pRandomNumbers==0){
    printf("Not enough memoryn");
    exit(1);
    }
    if the condition satisfy you might ask the user for input

  8. Goran Kavelj Avatar

    Great tutorial very helpful keep up the good work 😉

  9. Mohsan Rasheed Avatar

    The following code converts an int array into a string but I don't understand it completely. Could you explain the behaviour of sprintf inside malloc?

    char** makeStrArr(const int* vals, const int nelems)
    {
    char** strarr = (char*)malloc(sizeof(char) * nelems);
    int i;
    char buf[128];
    for (i = 0; i < nelems; i++)
    {
    strarr[i] = (char*)malloc(sprintf(buf, "%d", vals[i]) + 1);
    strcpy(strarr[i], buf);
    }
    return strarr;
    }
    void freeStrArr(char** strarr, int nelems)
    {
    int i = 0;
    for (i = 0; i < nelems; i++) {
    free(strarr[i]);
    }
    free(strarr);
    }
    void iarrtostrarrinc()
    {
    int i_array[] = { 65, 66, 67, 68, 69 };
    char** strarr = makeStrArr(i_array, 5);
    int i;
    for (i = 0; i < 5; i++) {
    printf("%sn", strarr[i]);
    }
    freeStrArr(strarr, 5);
    }

  10. Neil Z Avatar

    for the last getCharInfo when i type anything ,  prg will return some number instead 1, and rest keep 0.

  11. Derek Banas Avatar

    If you print out the code and take notes as you watch, that should help. I link to the code in the description

  12. Vincent P Avatar

    can you redo this video? You went really fast there.

  13. Derek Banas Avatar

    Thank you 🙂 I'm glad you liked it and that you found the video. Most people don't know about me.

  14. Rafael Pontes Avatar

    Whoa! First time watching one of your videos!
    Great explanation! 🙂

  15. Derek Banas Avatar

    My main languages are Java, C++ and C. I dabble with Ruby now and then, but only for a few specific things. I don't consider myself an expert on PHP, but I know enough to get by. It isn't that hard after you use them for a couple years like I have. You can easily do it with a bit of practice

  16. Derek Banas Avatar

    That is my most popular video, but I don't make videos like that anymore. I never felt the need to try and get views by making the same video over and over like some people do. I just make whatever I think would be interesting 🙂

  17. naustradmas Avatar

    Well i was a bit surprised considering last video i saw was your tutorial on gestures and body language. Great stuff.

  18. millennia Avatar

    Derek, how do you keep up with all those various and differently structured programming languages?

  19. Derek Banas Avatar

    నేను సహాయం కాలేదు సంతోషంగా ఉన్నాను. నేను నిర్మాణాల అనుబంధ జాబితా సృష్టించడానికి ఎలా ఇది మరొక సాధారణ ప్రశ్న కవర్ చేస్తుంది. నేను ఈ ట్యుటోరియల్స్ ఆనందించే వెబ్.

  20. 123japanuser Avatar

    కోచ్ ప్రియమైన,

    ఇప్పుడు నేను ఈ అంశంపై ఇంటర్వ్యూలో ప్రశ్న సమాధానం.

    గాడ్ బ్లెస్

  21. Nico Cvitak Avatar

    If you include stdint.h you can use int32_t to ensure that you are using an int with a width of exactly 4 bytes.

  22. Derek Banas Avatar

    Many more are coming. I'm glad you are enjoying the C tutorials. They are fun to make

  23. Derek Banas Avatar

    I'm glad you enjoyed it 🙂 I'm slowly ramping up the complexity

  24. Lalaka Jeerasinghe Avatar

    Very Important concept and very confusing. Thanks for Clearing that confusion.

  25. James James Avatar

    defo looking forward to the next one !

    What about something on server side C — E.G, what extra libraries are needed?

  26. dasten123 Avatar

    it's a good tutorial

  27. Michał Z. Avatar

    youtube.com/watch?v=1nYDflSL0Mg&list=SP9D558D49CA734A02 great video about malloc internals, I recommend all videos of this course for those who think that they know C and who would like to learn more, especially about C internals 🙂

  28. Derek Banas Avatar

    Yes I covered that in previous tutorials

  29. BLOOMS Avatar

    00:38

    An int may or may not get 4 bytes.  It depends on the architecture.

Leave a Reply

Your email address will not be published. Required fields are marked *