Malloc - when to use?

This is a discussion on Malloc - when to use? within the C Programming forums, part of the General Programming Boards category; I never quite understand the use of malloc. When I declare some variables, doesn't the system allocate some random memory ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    35

    Malloc - when to use?

    I never quite understand the use of malloc. When I declare some variables, doesn't the system allocate some random memory for them to be placed? In what cases should I use malloc?
    I have a struct like this;
    Code:
    struct Student
         {
         char name[20];
         int number, id, *testmarks;
         double total;
         };
    I am expected to get these values from the user. That's nothing than scanf, but then I am expected to allocate the required memory. What code shoud I write to allocate the required memory for these struct or struct elements? And what will happen if I don't make it?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by farukyaz View Post
    I never quite understand the use of malloc. When I declare some variables, doesn't the system allocate some random memory for them to be placed? In what cases should I use malloc?
    I have a struct like this;
    Code:
    struct Student
         {
         char name[20];
         int number, id, *testmarks;
         double total;
         };
    I am expected to get these values from the user. That's nothing than scanf, but then I am expected to allocate the required memory. What code shoud I write to allocate the required memory for these struct or struct elements? And what will happen if I don't make it?
    No C does not allocate memory... You have to do it all manually. You need to use malloc() or calloc() anytime you want to create variables that are not stored on the program's stack... and after reserving memory you have to free() it... every time.

    For example:
    Code:
    int x;              // these are stored on the stack.  No Malloc.
    char y[32];
    
    int *z;            // these you need to allocate memory for
    char *a;
    
    z = malloc(100 * sizeof(int));  // create z[100];
    a = malloc(1024 * sizeof(char));  // make a long string
    
    // do a bunch of stuff
    
    free(z);
    free(a);

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well if you were expecting a maximum of 10 marks, you could have this.
    Code:
    struct Student
         {
         char name[20];
         int number, id;
         int testmarks[10];
         double total;
         };
    The point of having a pointer is that you DON'T know until run-time how many of something you're going to have to deal with. It might be only 3, or it could be a thousand.

    int testmarks[1000]; would simply be wasteful 99% of the time, and just broken the other 1% when more than 1000 were needed.

    So, we have pointers, so we can allocate what we need, when we know how many we need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by CommonTater View Post
    No C does not allocate memory... You have to do it all manually. You need to use malloc() or calloc() anytime you want to create variables that are not stored on the program's stack... and after reserving memory you have to free() it... every time.

    For example:
    Code:
    int x;              // these are stored on the stack.  No Malloc.
    char y[32];
    
    int *z;            // these you need to allocate memory for
    char *a;
    Thanks for the reply. I understood that some variables are stored on the stack because they have limits or a certain value, right? And why isn't a pointer, in your code "int *z" stored the same way? Will I need malloc whenever I use a pointer?

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by Salem View Post
    Well if you were expecting a maximum of 10 marks, you could have this.
    Code:
    struct Student
         {
         char name[20];
         int number, id;
         int testmarks[10];
         double total;
         };
    The point of having a pointer is that you DON'T know until run-time how many of something you're going to have to deal with. It might be only 3, or it could be a thousand.

    int testmarks[1000]; would simply be wasteful 99% of the time, and just broken the other 1% when more than 1000 were needed.

    So, we have pointers, so we can allocate what we need, when we know how many we need.
    Thanks a lot, things are getting clearer now.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by farukyaz View Post
    Thanks for the reply. I understood that some variables are stored on the stack because they have limits or a certain value, right? And why isn't a pointer, in your code "int *z" stored the same way? Will I need malloc whenever I use a pointer?
    No you don't need malloc for all pointers...

    If you're doing, for example:
    Code:
    int x;  // variable
    
    int *y;  // pointer
    
    y = &x; // y now uses the storage allocated to x
    What you need to do is grab some good tutorials from the web (there are some excellent ones on this forums parent site) and start reading. Programming isn't guess work... educate yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Using malloc in C. Please help me.
    By newcprogram in forum C Programming
    Replies: 2
    Last Post: 07-25-2009, 02:11 AM
  3. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  4. Why use malloc()
    By I-See-C in forum C Programming
    Replies: 30
    Last Post: 11-27-2007, 03:44 AM
  5. Malloc
    By incognito in forum C Programming
    Replies: 5
    Last Post: 02-25-2004, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21