Creating a function that mallocs space for my new type

This is a discussion on Creating a function that mallocs space for my new type within the C Programming forums, part of the General Programming Boards category; I'm not sure exactly what I need to use as an argument to malloc to allocate space in the table_allocate(int) ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Creating a function that mallocs space for my new type

    I'm not sure exactly what I need to use as an argument to malloc to allocate space in the table_allocate(int) function. I was thinking just count_table* cTable = malloc(sizeof(count_table*)) , but that doesnt do anything with the size parameter. Am i supposed to allocate space for the list_node_t also? Below is what I am working with.

    in the .h file I'm given this signature:

    Code:
    //create a count table struct and allocate space for it                         
    //return it as a pointer                                                        
    count_table_t* table_allocate(int);

    Here are the structs that I'm supposed to use:

    Code:
    typedef struct list_node list_node_t;
    
    struct list_node {
      char *key;
      int value;
    
      //the next node in the list                                                   
      list_node_t *next;
    };
    
    typedef struct count_table count_table_t;
    
    struct count_table {
      int size;
      //an array of list_node pointers                                              
      list_node_t **list_array;
    };

    Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have to keep track of your own size.

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

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    I understand I need to keep track of size as I go, but the int parameter for the allocation function is meant to specify the size of the count table. I don't understand what I would use the size parameter for if I'm just keeping track of size as I go.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    It sounds like you are supposed to write cTable->size = size; and probably also initialise cTable->list_array, either to a null pointer, or by using malloc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  3. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

Tags for this Thread


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