malloc struct node array

This is a discussion on malloc struct node array within the C Programming forums, part of the General Programming Boards category; Code: if((relation[i] =(Tnode)malloc(sizeof(TableNode)))==NULL)//allocate memory { printf("Failled to allocate memory.\n"); fflush(stdout); exit(1); } TableNode relation[100]; typedef sturct node TableNode; typedef TableNode ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    53

    malloc struct node array

    Code:
    if((relation[i] =(Tnode)malloc(sizeof(TableNode)))==NULL)//allocate memory
                {
                  printf("Failled to allocate memory.\n"); fflush(stdout); exit(1);
                }
    TableNode relation[100];
    typedef sturct node TableNode;
    typedef TableNode * Tnode;
    how u malloc struct node []? Mine is wrong.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by p595285902 View Post
    TableNode relation[100];

    how u malloc struct node []? Mine is wrong.
    You don't. "Relation" is not a pointer array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    So u can't store information in array only the node that array points to ?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    If relation was:
    Code:
    Tnode relation[100];
    ...then it would be an array of pointers to your struct and you could then dynamically allocate memory to those individual array cells (pointers).

    You can store information in an array of struct nodes (TableNodes), you just don't need to do any dynamic allocation since you can simply treat each cell of the array as if it were already a struct node.

    [edit]Avoid casting the return value from the malloc call. The returned void pointer can be safely assigned without it.[/edit]
    Last edited by hk_mp5kpdw; 03-23-2011 at 09:46 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    Code:
    if((newPtr=(Tnode)malloc(sizeof(TableNode)))==NULL)//allocate memory
                {
                  printf("Failled to allocate memory.\n"); fflush(stdout); exit(1);
                }
              strcpy(newPtr->name,rel);//set relation's name
              relation[i]=*newPtr;
    is this what you mean?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Do you want an array of pointers to struct node or do you want an array of struct node. There is little reason for you to do things the way you are doing them. Either have an array of pointers and dynamically allocate memory to those pointers or have an array of structs and don't do any dynamic memory allocation.

    What you are doing above looks like you want to keep using an array of structs but then you are doing some dynamic memory allocation and then copying the allocated struct to the array of struct... which is kind of pointless. It would help if you had (re)posted the declaration of relation array otherwise we are left scratching our heads guessing what it looks like since you're only giving us a part of the complete picture and we are assuming a certain declaration that may or may not be correct.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. parent in a binary search tree
    By roaan in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 08:08 PM
  3. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  4. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  5. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM

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