Thread: malloc

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    malloc

    Hey guys im loading some data from a file into memory using the structs.. i need to allocate some memory for it using malloc but im unsure which variables i need to use to allocate memory for the structs.

    could someone also explain a little more about the structs


    Code:
    typedef struct category* CategoryTypePtr;
    typedef struct item* ItemTypePtr;
    
    /* Structure definitions. */
    typedef struct price
    {
       unsigned dollars;
       unsigned cents;
    } PriceType;
    
    typedef struct item
    {
       char itemID[ID_LEN + 1];
       char itemName[MAX_NAME_LEN + 1];
       PriceType prices[NUM_PRICES];
       char itemDescription[MAX_DESC_LEN];
       ItemTypePtr nextItem;
    } ItemType;
    
    typedef struct category
    {
       char categoryID[ID_LEN + 1];
       char categoryName[MAX_NAME_LEN + 1];
       char drinkType;      /* (H)ot or (C)old. */
       char categoryDescription[MAX_DESC_LEN];
       CategoryTypePtr nextCategory;
       ItemTypePtr headItem;
       unsigned numItems;
    } CategoryType;
    
    typedef struct gjc
    {
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    You need to malloc() memory for each structure so make a pointer.
    Code:
    int main(){
         GJCType *name;
         name = malloc(sizeof(GJCType));
         name -> headCategory = malloc( sizeof(CategoryType) );
         name -> headCategory -> headItem=malloc( sizeof(ItemType) );
         name -> headCategory -> nextCategory = malloc( sizeof(CategoryType) );
         name -> headCategory -> headItem -> nextItem = malloc( sizeof(ItemType) );
    }
    And so on...
    ~Sven
    Last edited by 00Sven; 04-19-2006 at 08:23 AM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    And don't forget to make type casting!Because malloc() return a *void

  4. #4
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Quote Originally Posted by fnoyan
    And don't forget to make type casting!Because malloc() return a *void
    What are you referring to? Casting the return value of malloc?
    C does type coercian from void to any other type automatically so casting malloc only has the potential of evil side-effects.

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() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  5. malloc always setting length of 8?
    By Zarkhalar in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2004, 11:36 PM