#28: Dynamic Memory Allocation in C | C Programming for Beginners

Поділитися
Вставка
  • Опубліковано 3 гру 2024

КОМЕНТАРІ • 97

  • @programizstudios
    @programizstudios  2 роки тому +1

    🚀 Loved the tutorial? Take it further with Programiz PRO!
    Refine your skills, conquer coding fears, and build confidence with interactive lessons, quizzes, and challenges. Strengthen your programming foundation and master C today!
    👉Start Here: bit.ly/c-master

  • @LucasFCunha
    @LucasFCunha 6 місяців тому +57

    "Dynamic memory allocation allows us to allocate memory dynamically" 10/10 explanation.

    • @sutofana
      @sutofana 3 місяці тому +1

      HAHAHAHHAHAHAHAH

  • @CakeInvasion11
    @CakeInvasion11 6 місяців тому +10

    The best for beginners is to read a complete tutorial. Without skipping important steps. It's important to UNDERSTAND how things work, not just make them work

    • @chess.pawntoqueen
      @chess.pawntoqueen 4 місяці тому +1

      actually some people understand better in practice

  • @irfanquader6417
    @irfanquader6417 2 роки тому +11

    Your website has helped me a lot in my coding journey, thanks a lot for your contribution to the community.

  • @nickralston4140
    @nickralston4140 7 місяців тому +5

    Great videos, Computer science can get really confusing at parts, especially when you go from java/python to c, currently taking a course in college, and these videos have helped me greatly

  • @guidoxenofonte1475
    @guidoxenofonte1475 Рік тому +2

    i have a test on tuesday about this subject and you helped me a lot on learning it. i really hope that more and more people will be helped by this and the other videos of yours. God bless y'all and thx for the great content :D

  • @Tallaab0
    @Tallaab0 10 місяців тому +2

    good explanation; B for the question asked last.

  • @videoventures6571
    @videoventures6571 Рік тому +4

    You are doing an amazing job . I have learned alot

  • @brettchallice8132
    @brettchallice8132 10 місяців тому +1

    There's an error @ 8:15. "In our previous example we have allocated 400 bytes of memory to store 4 integer values." Each integer is four bytes, and thus you're allocating for four integers (4 bytes x 4 values) = 16 bytes of allocated memory.

    • @twinkleramchandani5193
      @twinkleramchandani5193 3 місяці тому +1

      no , n was equal to 100 in that example thus 100 * 4 = 400 bytes . int the next example she changed the value of n to 4 thus 4 * 4 = 16 bytes

  • @simo_the_goat
    @simo_the_goat Рік тому +9

    random comment for engagement

  • @programizstudios
    @programizstudios  2 роки тому +7

    Q. What is the correct way to allocate memory to store 12 double data?
    A. malloc(20 * double)
    B. (double*) malloc(12 * sizeof(double))
    C. (double) malloc(20 * sizeof(double))
    D. (double*) malloc( sizeof(20 * int))

  • @rayanalasow7035
    @rayanalasow7035 Рік тому +2

    helped a lot. thanks

  • @med105keshar4
    @med105keshar4 2 роки тому +2

    best for beginner💓

  • @Quankibatngu
    @Quankibatngu 10 місяців тому +1

    Thank you very much

  • @errorhostnotfound1165
    @errorhostnotfound1165 Рік тому +2

    I'm getting a warning for ptr = realloc(ptr, n * sizeof(int)); saying that realloc could return NULL, which "will cause the original memory block to be leaked", any know how to fix this?

  • @plankalkulcompiler9468
    @plankalkulcompiler9468 Рік тому +1

    Thanks for the video, it helped a lot!

  • @programizstudios
    @programizstudios  2 роки тому +9

    [ Programiz Quiz ]
    Q. What is the correct way to allocate memory to store 12 double data?
    a. malloc(20 * double)
    b. (double*) malloc(12 * sizeof(double))
    c. (double) malloc(20 * sizeof(double))
    d. (double*) malloc( sizeof(20 * int))

    • @rickzalman4136
      @rickzalman4136 2 роки тому +6

      The answer is b.
      The answer is choice b , because you are allocating a block of memory on the heap for 12 elements , each element being a double.
      1: You allocating 12* sizeof(double)
      You can only use sizeof function on data types such as, sizeof(int), sizeof(float), sizeof(double)
      2: and then you must type cast the void pointer to the block(array) of type double, so it can point to the right data type, in the above example,
      you type cast it to (double*).

    • @balajis3843
      @balajis3843 2 роки тому +1

      b

    • @VivekSarmaKommanamanchi
      @VivekSarmaKommanamanchi 2 роки тому

      b

    • @errorhostnotfound1165
      @errorhostnotfound1165 Рік тому

      @@rickzalman4136 if the pointer was created as a double pointer, would you still need to type cast it to double?

    • @rickzalman4136
      @rickzalman4136 Рік тому

      @@errorhostnotfound1165
      You need too!
      // Example program in C
      #include
      #include // needed for
      // malloc and free.
      #define ROWS 4 // number of
      // pointers to each array .
      #define COLS 2 // number of
      // elements in each array.
      int main(){
      double ** matrix; // type is double in this example.
      // compiler needs this information
      //
      // each row(array) is a pointer to a
      // pointer to an array of size COLS.
      matrix= (double**)malloc(ROWS*sizeof(double*));
      // Remember a multidimensional array is really an array which contains arrays.
      int j, k ; // needed for loops
      // next allocate each pointer in
      // each row to a pointer which points to an array with a size of COLS.
      //
      for(j=0; j< ROWS; j ++)
      matrix[i] = (double*)malloc(COLS*sizeof(double) );
      // fill 2D array with values
      for(j= 0; j< ROWS; j ++) // inner loop
      for(k=0; k

  • @tommyleespartan5640
    @tommyleespartan5640 Рік тому +2

    Great video

  • @yogithabale
    @yogithabale 4 місяці тому +3

    mam , we should also use #include at the starting of code then only we are able to run the code

  • @saisharan4200
    @saisharan4200 5 місяців тому

    Beauty with Brain🤗☺️

  • @b7sh_b7sh
    @b7sh_b7sh Рік тому +1

    amazing teacher😍

    • @saisharan4200
      @saisharan4200 5 місяців тому

      That's true
      Beauty with brain

  • @mollahji
    @mollahji 16 днів тому

    shouldn't the stdlib.h header file be included when using the memory allocation functions?

  • @laylalilly6797
    @laylalilly6797 2 роки тому +1

    When i run the realloc() code in visual studio code it returns an error as its expecting a void data type yet the malloc() worked.
    Can anyone 'point' me in the right direction?
    Thanks
    int *ptr, i , n1, n2;
    ptr = (int*) malloc(n1 * sizeof(int));
    ptr = realloc(ptr, n2 * sizeof(int)); // error expecting void type in ptr ?

  • @gabrielsantana3861
    @gabrielsantana3861 2 роки тому

    nice

  • @tteokmember
    @tteokmember Рік тому +8

    #include
    #include
    int main() {

    int n, i, *ages;

    n = 4;

    ages = (int*) malloc(n * sizeof(int));

    if(ages == NULL){
    printf("ERROR! Memory not allocated
    ");
    return 0;
    }

    printf("Input %d values:
    ", n);
    for(i = 0; i < n; i++){
    scanf("%d", ages + i);
    }

    printf("Initial array elements:
    ");
    for(i = 0; i

    • @K6619.
      @K6619. 9 місяців тому +1

      isnt ages[4] basically the same with *(ages+4) ?

  • @a1aurielius933
    @a1aurielius933 3 місяці тому

    if return 0 terminates main function then what does return 1 do¿

  • @racingfan372
    @racingfan372 Рік тому

    Quiz answer should be B

  • @onomemafuru9496
    @onomemafuru9496 2 роки тому

    Madam please what about calloc?

  • @ayoubhechmi7609
    @ayoubhechmi7609 Місяць тому

    oui

  • @Kenara_Music
    @Kenara_Music Рік тому

    I think the answer in B

  • @yogithabale
    @yogithabale 4 місяці тому

    opt B

  • @TheHeraldOfSpring
    @TheHeraldOfSpring 3 місяці тому

    ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT !

  • @abhisheek05
    @abhisheek05 2 роки тому +2

    Plz make course on C++.

    • @prachetnaik7090
      @prachetnaik7090 2 роки тому +2

      Check there website they have course for c ,c++,java,python.

  • @thiagaodavez5465
    @thiagaodavez5465 Рік тому

    gud

  • @user-iq2xd5fq8n
    @user-iq2xd5fq8n 11 місяців тому

    B

  • @onic9623
    @onic9623 Рік тому +1

    Option B : (double*) malloc(12 * sizeof(double))
    -----------------------------------------------------------------------------------------------------
    #include
    int main() {
    int n = 4, i;
    int* ages;
    ages = (int*) malloc(n * sizeof(int));
    if(ages == NULL) {
    printf("Memmory Cannot Allocated");
    return 0;
    }
    printf("Enter Four Ages:
    ");
    for(i = 0; i < n; i++) {
    scanf("%d", ages + i);
    }
    printf("Initial Array Elements:
    ");
    for(i = 0; i < n; i++) {
    printf("%d ", *(ages + i));
    }
    n = 6;
    ages = realloc(ages, n * sizeof(int));
    ages[4] = 32;
    ages[5] = 59;
    printf("
    Final Array Elements:
    ");
    for(i = 0; i

  • @pavanstrange58
    @pavanstrange58 2 роки тому +2

    mam u r so pretty 😍

  • @thesun3267
    @thesun3267 Рік тому

    bobloc

  • @countrysideshowyaigrock4689
    @countrysideshowyaigrock4689 Рік тому +1

    #include
    int main() {
    int quantity = 4;
    int* ages;
    ages = (int*)malloc(quantity * sizeof(int));
    if (ages == NULL) {
    printf("Memory can`t be allocated");
    return 0;
    }
    printf("Enter input values:
    ");
    for (int i = 0; i < quantity; ++i) {
    scanf("%d", ages + i);
    }
    printf("Input values:
    ");
    for (int i = 0; i < quantity; ++i) {
    printf("%d
    ", *(ages + i));
    }
    quantity = 6;
    ages = realloc (ages, quantity * sizeof(int));
    ages[4] = 32;
    ages[5] = 59;
    printf("We adding new values:
    ");
    for (int i = 0; i < quantity; ++i) {
    printf("%d
    ", *(ages + i));
    }
    free(ages);
    return 0;
    }

  • @jitendrajitu5233
    @jitendrajitu5233 2 місяці тому

    😌

  • @seabasschukwu6988
    @seabasschukwu6988 Рік тому

    ❤❤

  • @twinkleramchandani5193
    @twinkleramchandani5193 3 місяці тому

    b

  • @ankitrawat5285
    @ankitrawat5285 2 роки тому +1

    you are cute padma

  • @sandeepvarma8453
    @sandeepvarma8453 7 місяців тому

    programming task:
    #include
    #include
    int main(){
    int n=4;
    int* ages;
    if(ages==NULL){
    printf("memory is not allocated");
    }
    ages=(int*)malloc(n*sizeof(int));
    printf("enter input values: ");
    for(int i=0;i

  • @dav0203
    @dav0203 6 днів тому

    Aziz = ages😂

  • @sohamgupta2452
    @sohamgupta2452 Рік тому

    I finished the programming task and wanted to challenge myself by making it so the user decides how many spaces need to be added and what inputs should be added. I think I did it correctly but when I ran my code there was a "Segmentation Fault" which I don't know.
    Code:
    // Online C compiler to run C program online
    #include
    #include
    int main() {
    int n = 4;
    int* ages;
    int num1, num2, add;
    ages = (int*) malloc(n*sizeof(int));
    if(ages == NULL){
    printf("ERROR! Memory not allocated
    ");
    return 0;
    }
    printf("Enter your ages:
    ");
    for(int i = 0; i

  • @pjguitar15
    @pjguitar15 7 місяців тому

    Are you Nepali?

  • @1NazareeM618
    @1NazareeM618 Рік тому

  • @RyanTaylor-er1ct
    @RyanTaylor-er1ct 10 місяців тому

    free videos

  • @dav0203
    @dav0203 6 днів тому

    Say Shazam😂😂

  • @ankitborkar5161
    @ankitborkar5161 2 роки тому

    There no, code of Programming task. I want to know.. What's the Exact output.. Please Provide the code..

    • @cartoonsmation
      @cartoonsmation Рік тому

      #include
      int main() {

      int n, i, *ages;

      n = 4;

      ages = (int*) malloc(n * sizeof(int));

      if(ages == NULL){
      printf("ERROR! Memory not allocated
      ");
      return 0;
      }

      printf("Input %d values:
      ", n);
      for(i = 0; i < n; i++){
      scanf("%d", ages + i);
      }

      printf("Initial array elements:
      ");
      for(i = 0; i

  • @binkusarma3398
    @binkusarma3398 2 роки тому

    L0l0ß

  • @YUPENGMA-h1y
    @YUPENGMA-h1y Рік тому

    #include
    #include
    int main() {
    int n = 4;
    int* age;

    age = (int*)malloc(n * sizeof(int));

    if (age == NULL) {
    printf("Memory can not be allocated");
    return 0;
    }

    printf("Enter four values for age:
    ");
    for (int i = 0; i < n; ++i) {
    scanf("%d", age + i);
    }

    printf("Four values of age are:
    ");
    for (int i = 0; i < n; ++i) {
    printf("%d
    ", *(age + i));
    }

    n = 6;
    age = (int*)realloc(age, n * sizeof(int));
    age[4] = 32;
    age[5] = 59;

    printf("All six age elements:
    ");
    for (int i = 0; i < n; ++i) {
    printf("%d
    ", age[i]);
    }

    free(age);

    return 0;
    }

  • @davebull6321
    @davebull6321 Рік тому

    # include
    int main(){
    int n = 4;
    int* ages;
    ages = (int*)malloc(n* sizeof(int));
    if(ages == NULL){
    printf("Memory cannot be allocated");
    return 0;
    }
    printf("Enter input value
    ");
    for(int i = 0; i < n; ++i){
    scanf("%d",ages + i);
    }
    n = 6;
    ages[4] = 32;
    ages[5] = 59;
    ages = realloc(ages,n* sizeof(int));
    printf("Input value
    ");
    for(int i = 0; i < n; ++i){
    printf("%d
    ",*(ages + i));
    }
    free(ages);
    return 0;
    }

  • @cleevenslouis7439
    @cleevenslouis7439 11 місяців тому

    #include
    #include
    int main()
    {
    int n = 4;
    int *ages;
    ages = (int *)malloc(n * sizeof(int));
    if (ages == NULL)
    {
    printf("Error allocating memory
    ");
    return 0;
    }
    printf("Enter the value of n:
    ");
    for (int i = 0; i < n; i++)
    {
    scanf("%d", ages + i);
    }
    printf("print the ages :
    ");
    for (int i = 0; i < n; i++)
    {
    printf("%d ", *(ages + i));
    }
    printf("
    let's resize the array for 6 input:
    ");
    ages = realloc(ages, 6 * sizeof(int));
    // preserve the 5 and 6 places for some specific values
    *(ages + 4) = 32;
    *(ages + 5) = 59;
    printf("print the ages :
    ");
    for (int i = 0; i < 6; i++)
    {
    printf("%d ", *(ages + i));
    }
    free(ages);
    return 0;
    }

  • @JASMIN-jg2vn
    @JASMIN-jg2vn 5 місяців тому

    #include
    #include
    int main() {
    int n = 4;
    int* ages;
    ages = (int*) malloc(n * sizeof(int));
    if (ages == NULL) {
    printf("Memory cannot be allocated");
    return 0;
    }
    printf("Enter input values:
    ");
    for(int i = 0; i < n; i++) {
    scanf("%d", ages + i);
    }
    printf("
    Allocated Values:
    ");
    for(int i = 0; i < n; i++) {
    printf("%d
    ", *(ages + i));
    }
    n = 6;
    ages[4] = 32;
    ages[5] = 49;
    ages = realloc (ages, n * sizeof(int));
    printf("
    Newly Allocated Values:
    ");
    for(int i = 0; i < n; i++) {
    printf("%d
    ", *(ages + i));
    }
    free(ages);
    return 0;
    }

  • @b7sh_b7sh
    @b7sh_b7sh Рік тому

    #include
    int main() {
    int n;
    int* ages;
    n = 4;
    ages = (int*) malloc(n * sizeof(int));
    if(ages == NULL){
    printf("Memory cannot be allocated
    ");
    return 0;
    }
    printf("Input %d values:
    ", n);
    for(int i = 0; i < n; i++){
    scanf("%d", ages + i);
    }
    printf("first elements:
    ");
    for(int i = 0; i

  • @PedroLopes-k5f
    @PedroLopes-k5f Рік тому

    #include
    #include

    int main(){

    int* ages;

    int n = 4;


    ages = (int*) malloc(n * sizeof(int));



    if(ages == NULL){
    printf("ERROR! Memory not allocated
    ");
    return 0;

    }

    printf("Enter input values:
    ");
    for (int i = 0; i < n; ++i) {
    scanf("%d", ages + i);
    }
    printf("Insert all elements:
    ");
    for (int i = 0; i < n; ++i ) {
    printf("%d
    ", *(ages + i));

    }

    n = 6;

    ages = realloc (ages, n * sizeof(int));

    ages [4] = 32;
    ages [5] = 59;

    printf("new Values:
    ");
    for (int i = 0; i < n; ++i){
    printf("%d
    ", *(ages + i));

    }
    free(ages);

    return 0;
    }

  • @AYUBBASHIRAHMED
    @AYUBBASHIRAHMED 3 місяці тому

    #include
    #include
    int main()
    {
    int n = 4;
    int* age=(int*)malloc(n*sizeof(int));
    if(n==0){
    printf("MEMORY IS NOT AVAILABLE");
    return 0;
    }
    printf("ENTER FOUR AGES: ");
    for(int i=0; i

  • @streamvision145
    @streamvision145 6 місяців тому

    #include
    #include
    int main() {
    int* ages;
    ages = (int*) malloc(4*sizeof(int));
    for(int i=0;i

  • @JeongWoonKim-i4j
    @JeongWoonKim-i4j Рік тому

    #include
    #include
    int main() {
    int n = 4;
    int* ages;
    ages = (int*) malloc(n * sizeof(int));
    if (ages == NULL) {
    printf("Ages momory cannot be allocated");
    return 0;
    }
    printf("Enter the ages:
    ");
    for (int i = 0; i < n; ++i) {
    scanf("%d/n", ages + i);
    }
    printf("Age:
    ");
    for (int i = 0; i < n; ++i) {
    printf("%d
    ", *(ages +i));
    }
    n = 6;
    ages[4] = 32;
    ages[5] = 59;
    ages = realloc (ages, n * sizeof(int));
    printf("New ages memory:
    ");
    for (int i = 0; i < n; ++i) {
    printf("%d
    ", *(ages + i));
    }
    free(ages);
    return 0;
    }

  • @hunter9544
    @hunter9544 7 місяців тому

    // Online C compiler to run C program online
    #include
    #include
    int main() {
    int* ages;
    int n=4;
    ages=(int*) malloc(n* sizeof(int));
    printf("Enter input values:
    ");
    for (int i=0; i

  • @fateemahamosuro4788
    @fateemahamosuro4788 10 місяців тому

    B

  • @Eileen-in-the-coding-maschine
    @Eileen-in-the-coding-maschine 9 місяців тому

    #include
    #include
    int main() {
    int a = 4;
    int* ages;
    ages = (int *) malloc(a * sizeof(int));
    printf("Enter input values for ages:
    ");
    for (int i = 0; i < a; ++i) {
    scanf("%d", ages + i);
    }
    printf("Input values of ages:
    ");
    for (int i = 0; i < a; ++i) {
    printf("%d
    ", *(ages + i));
    }
    a = 6;
    ages = realloc(ages, a * sizeof(int));
    ages[4] = 32;
    ages[5] = 59;
    printf("All the ages:
    ");
    for(int i = 0; i < a; ++i) {
    printf("%d = %p
    ", *(ages + i), ages + i);
    }
    free(ages);
    return 0;
    }
    B is the correct answer.

  • @zayedsyed9120
    @zayedsyed9120 4 місяці тому

    #include
    #include
    int main(){
    // Initialize
    int* ages = NULL;
    int var = 4;
    // Allocate Memory
    ages = (int*) malloc(var * sizeof (int));
    if (ages == NULL){
    printf("Memory cannot be allocated
    ");
    return 0;
    }
    printf("Enter Values
    ");
    for (int i = 0; i < var; i++){
    scanf("%d", &ages[i]);
    }
    for (int i = 0; i < var; i++){
    printf("%d
    ", ages[i]);
    }
    // Reallocate Memory
    var = 6;
    ages = realloc (ages, var * sizeof(int));
    if (ages == NULL){
    printf("Memory cannot be reallocated
    ");
    return 0;
    }
    ages[4] = 32;
    ages[5] = 59;
    printf("Newly Allocated Memory
    ");
    for(int i = 0; i < var; i++){
    printf("%d
    ", ages[i]);
    }
    // Free Memory
    free(ages);
    return 0;
    }

  • @dnwint2
    @dnwint2 6 місяців тому

    B

  • @nagashreenb4763
    @nagashreenb4763 21 день тому

    B