Thread: Creating a function that mallocs space for my new type

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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