Thread: C dynamic memory allocation

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    130

    C dynamic memory allocation

    I'm new here and in c programming so bear with me as much as you can..and sorry if I ask a silly questions.

    I've read on The function malloc -which is used to allocate a certain amount of memory during the execution of a program, moreover as known, the malloc function will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory, and when you finish, you must return it to the operating system by calling the function free, in abbreviation words we can easily say that the malloc function is used for gaining/allocating a temporarily certain amount memory of the prime PC's memory and doing whatever I want there then doing free(right? just to be assure)
    All these things are clearly understood for me, but when I would like to apply the malloc funtion into my code I just get stuck and don't know how to proceed!!
    may anyone give me a practical code's script of using malloc function? doesn't matter what the code would be, just wanted for understanding how it goes behind.


    thanks for your cooperation.



  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Think about a program that need sometime (not allways) an array of 1000 integers.
    Normaly you declare an array:
    Code:
    int main () {
        int array[1000];
    …
    // work with the array or not
    …
        return EXIT_SUCCESS;
    }
    This code reserve allways memory on the stack for the array.
    It doesn't matter if you need it or not.

    Now the same with malloc:
    Code:
    int main () {
        int *array = NULL;
    …
    // if we need the array …
        if (array == NULL) {
            array = malloc(1000 * sizeof(*array));
            if (array == NULL) {
                fprintf(stderr, "Ooops, no memory left.\n");
                return EXIT_FAILURE;
            }
    // you can use it as a normal array like
    //      array[0] = 42;
    // the indexes goes from 0 to 999 in this example
    …
    // at the end, we should free it
            free(array);
            array = NULL;
        }
    …
        return EXIT_SUCCESS;
    }
    In this example, there is only a pointer on the stack that needs less bytes.
    You only allocate memory if you need it.
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by WoodSTokk View Post
    Think about a program that need sometime (not allways) an array of 1000 integers.
    Normaly you declare an array:
    Code:
    int main () {
        int array[1000];
    …
    // work with the array or not
    …
        return EXIT_SUCCESS;
    }
    This code reserve allways memory on the stack for the array.
    It doesn't matter if you need it or not.

    Now the same with malloc:
    Code:
    int main () {
        int *array = NULL;
    …
    // if we need the array …
        if (array == NULL) {
            array = malloc(1000 * sizeof(*array));
            if (array == NULL) {
                fprintf(stderr, "Ooops, no memory left.\n");
                return EXIT_FAILURE;
            }
    // you can use it as a normal array like
    //      array[0] = 42;
    // the indexes goes from 0 to 999 in this example
    …
    // at the end, we should free it
            free(array);
            array = NULL;
        }
    …
        return EXIT_SUCCESS;
    }
    In this example, there is only a pointer on the stack that needs less bytes.
    You only allocate memory if you need it.
    In the last two lines, I can assume then the malloc after freeing it, it would get back to it's previously statement? (as it was before using it)

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Romyo2 View Post
    In the last two lines, I can assume then the malloc after freeing it, it would get back to it's previously statement? (as it was before using it)
    If you mean this two lines, then yes.
    Code:
            free(array);
            array = NULL;
    The first line free the memory, but the address is still on array.
    If you later check if you have an array with 'if (array != NULL)', you will work on an invalid pointer.
    Therefor, after freeing the memory, i set the pointer to NULL.
    If you want the memory until the end of program, you can insert the following line right before the last return.
    Code:
    …
    // end of programm ... cleanup
        if (array != NULL) free(array);
        return EXIT_SUCCESS;
    }
    Other have classes, we are class

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by WoodSTokk
    If you want the memory until the end of program, you can insert the following line right before the last return.
    Code:
    …
    // end of programm ... cleanup
        if (array != NULL) free(array);
        return EXIT_SUCCESS;
    }
    Since free(NULL) has no net effect, the above can be simplified to:
    Code:
    …
    // end of programm ... cleanup
        free(array);
        return EXIT_SUCCESS;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By kwzeet in forum C Programming
    Replies: 4
    Last Post: 07-14-2013, 04:35 PM
  2. dynamic memory allocation
    By silver_comet in forum C Programming
    Replies: 2
    Last Post: 05-30-2011, 12:05 PM
  3. what is dynamic memory allocation
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 06-08-2009, 08:11 AM
  4. Dynamic memory allocation
    By Luciferek in forum C++ Programming
    Replies: 118
    Last Post: 10-02-2008, 11:34 AM
  5. dynamic memory allocation - Please help
    By Space_Cowboy in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2002, 05:20 PM