Thread: alloca() function

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    alloca() function

    I would like to dynamically allocate memory for nodes in a linked list. I used to use malloc() and free() afterwards, but heard lately that it's simpler (with certain limitations like running out of memory) to use alloca().

    My question is what to put inside the brackets. If this is my person:

    Code:
    typedef struct node
    {
        char name[10];
        int age;
        int id;
        struct node *next;
    } Person;
    what should this line look like

    Code:
    Person *new = alloca(???)
    I tried to put sizeof(Person) but it fails miserably

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And by "fails miserably" you mean what, exactly?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I would like to dynamically allocate memory for nodes in a linked list.
    So what good is alloca()?
    Your list would go out of scope as soon as the function calling alloca() returned.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    When I input name: "abc", age: "20", id: "1" the printout function returns this to the screen

    NAME: ????!?!? AGE: 1832928 ID: 29382235

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    @Salem - did I then misunderstand the purpose of alloca? Is it not suppose to do the allocating and freeing of memory instead of ... Me?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you read the description of alloca?
    Code:
    DESCRIPTION
           The  alloca() function allocates size bytes of space in the stack frame
           of the caller.  This temporary space is automatically  freed  when  the
           function that called alloca() returns to its caller.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    yes i did read it and thats what led me to believe that instead of remembering to free everything I could use alloca

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It saves you from having to free it, but it doesn't save you from having to think
    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.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    dang nabbit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM