Allocating dynamic memory for structs

This is a discussion on Allocating dynamic memory for structs within the C Programming forums, part of the General Programming Boards category; Hi, i have a small problem i can't seem to figure out, although i remember the problem from previous code ...

  1. #1
    Nevyn
    Guest

    Allocating dynamic memory for structs

    Hi, i have a small problem i can't seem to figure out, although i remember the problem from previous code uh somewhere and i know it should be possible somehow :]

    Anyway, lets say i have a struct looking like this:

    struct foo {
    int x;
    char bar[20];
    };

    And a function to put data into the struct looking like this:

    void foobar (struct foo **barf, int elements) {
    int i;
    for(i=0; i<elements; i++) {
    barf[i]->x = 1;
    strcpy(barf[i]->bar, "FooBar");
    }
    }


    And a main function looking like this:

    int main(void) {
    struct foo *bar;
    const int elements = 4;
    bar = malloc(elements*sizeof(struct foo));
    /* Now we should have a 4 elements array of struct foo */
    /* And writing data to the struct in this function should be no problems but, calling foobar() to write the data causes a segfault. */
    foobar(&bar, elemenrs);
    free(bar);
    }


    I'm sure the answer is simple, and managing this should be possible somehow

    Cheers

  2. #2
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    corrected code

    struct foo {
    int x;
    char bar[20];
    };

    void foobar (struct foo *barf, int elements) {
    int i;
    for(i=0; i<elements; i++) {
    (&barf[i])->x = 1;
    strcpy(barf[i]->bar, "FooBar") ;
    }
    }



    int main(void) {
    struct foo *bar;
    const int elements = 4;
    int i;
    bar = (struct foo*)malloc(elements*sizeof(struct foo));
    /* Now we should have a 4 elements array of struct foo */
    /* And writing data to the struct in this function should be no problems but, calling foobar() to write the data causes a segfault. */

    foobar(bar,elements);
    free(bar);

    }
    =============Regards, Mangesh=======

  3. #3
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    one correction

    Hello,

    The code sent by me has a small correction in function foobar()

    void foobar (struct foo *barf, int elements)
    {
    int i;
    for(i=0; i<elements; i++) {
    (&barf[i])->x = 1;
    strcpy((&barf[i])->bar, "FooBar") ;
    }
    }


    Thanx and regards,
    Mangesh.

  4. #4
    Nevyn
    Guest

    Cool

    Ahhh thats it, thanks man

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > (&barf[i])->x = 1;
    > strcpy((&barf[i])->bar, "FooBar") ;
    Which reduces to (when you remove unnecessary indirections)
    barf[i].x = 1;
    strcpy( barf[i].bar, "FooBar") ;
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 05:04 AM
  2. Help allocating and reallocating memory
    By shnuffy in forum C Programming
    Replies: 1
    Last Post: 03-11-2006, 08:38 PM
  3. allocating some memory
    By supaman in forum C Programming
    Replies: 7
    Last Post: 03-09-2006, 10:34 PM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 03:38 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

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