Thread: What does malloc() do?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    What does malloc() do?

    What does malloc() do?

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    Allocates memory.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    dynamically allocates space in memory
    used for linked lists
    prolly other things too

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    I still dont get it....

    When is it used, can i have an example?

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    When you don't know how much memory you will need to allocate when you are writing the program.

  6. #6
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59

  7. #7
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    no problem , here some example ....

    1. for allocate char ...

    in the main function.
    Code:
    int main ()
    {
    int *ptr;
    ...
    ...
    ptr = (int*)malloc (sizeof (100));
    // 100 refer to how big the size you should need to have?
    ...
    ...
    free (ptr); // you should free the momory after you using it.
    return 0;
    }
    2 . to allocate struct in linked list concept ...

    Code:
    typedef struct STRLINK{
    char data1;
    STRLINK *next;
    } STR;
    typedef STR *STRPTR;
    
    int main ()
    {
    STRPTR startptr;
    
    startptr = (STRPTR)malloc (sizeof(STR));
    // the size is accordingly to the structure
    ..
    ..
    free (startptr);
    return 0;
    }

    remember, from the example 1, if you enter input more than sizeof 100, well... it's alright, dont be so brother, the memory allocate will resize it for you.

    -- beely

  8. #8
    Registered User unixOZ's Avatar
    Join Date
    Feb 2002
    Posts
    91
    damonbrinkley:

    Allocates memory.
    You could have been a bit more helpful, dont you think??
    Or do you just go out of your way to be rude?

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    Thanks...

    I get it now, malloc() is needed to allocate memory when you dont know how much you are going to need when you write the program, or when the amount of memory needed changes based on a variable.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by unixOZ
    damonbrinkley:
    You could have been a bit more helpful, dont you think??
    Or do you just go out of your way to be rude?
    If you have seen the function malloc in any literature on programming, then it's painfully obvious what it does. Every C book I have ever seen it used in states what it does. This holds especially true in the common "teach yourself" or "learn X" style books. Any, and I do mean any introductory C book, and just as likely, every intermediate / advanced book you will ever encounter covers what malloc does.

    Basicly, if you know enough to ask the question, you should already have your answer. Furthermore, since this is the great and wonderful internet, we have resources at our disposal called search engines. Two seconds with a search engine will tell you what any standard ANSI function in C does and how to correctly use it.

    A better question would have been: "Why did you need to ask?"

    Furthermore, and this should be in the sticky links at the top of the page if it isn't already, every question asked here should be as specific as possible.

    I mean seriously, what does malloc do? It allocates memory. Period. That is all it does. Malloc returns a block of allocated memory, whose size is measured in the number of bytes passed to it. That is all. End of story.

    So again, what does malloc do? It allocates memory, or fails to do so.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, lets play a little game. I call it spot the mistake. I ask the questions, someone answers:

    - What header file is missing from the example code given here to demonstrate the use of malloc()?

    Based on this:
    >>ptr = (int*)malloc (sizeof (100));
    - Is the cast necessary?

    Based on this:
    >ptr = (int*)malloc (sizeof (100));
    - How many int's can we store in the memory malloc()'d here?

    Based on this:
    ptr = (int*)malloc (sizeof (999));
    - How many int's can we store in the memory malloc()'d here?

    Now, a final question:
    - What's wrong with this example code?

    You could have been a bit more helpful, dont you think??
    Or do you just go out of your way to be rude?
    The question was simply what does malloc do. It does little more than allocate memory, so the answer was accurate, imho.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Hammer
    OK, lets play a little game. I call it spot the mistake. I ask the questions, someone answers:

    - What header file is missing from the example code given here to demonstrate the use of malloc()?

    Based on this:
    >>ptr = (int*)malloc (sizeof (100));
    - Is the cast necessary?

    Based on this:
    >ptr = (int*)malloc (sizeof (100));
    - How many int's can we store in the memory malloc()'d here?

    Based on this:
    ptr = (int*)malloc (sizeof (999));
    - How many int's can we store in the memory malloc()'d here?

    Now, a final question:
    - What's wrong with this example code?


    The question was simply what does malloc do. It does little more than allocate memory, so the answer was accurate, imho.
    0. <stdlib.h>
    1. Not that argument again . Of course, it's not nessecary, but some here prefer it, its a stylistic thing.
    2. At most 50, but possibly a few or maybe very many less.
    3. At most 499, but possibly a few or maybe very many less.
    4. Which example code?
    hello, internet!

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    moi: go sit in the corner, face the wall, and don't come back until you're behaving properly
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Salem
    ::thinks moi will want to revise those answers::
    did i miss something
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM