Dynamic array resizing

This is a discussion on Dynamic array resizing within the C Programming forums, part of the General Programming Boards category; I have written some simple code to add/remove items to/from an array. I am trying to modify the code so ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Dynamic array resizing

    I have written some simple code to add/remove items to/from an array. I am trying to modify the code so that it will accept any data type instead of having a make a new add/del function for each data type I use. Here is what I have:

    This code works just fine but only works with the vhost data type.

    Code:
    typedef struct _vhost vhost;
    struct _vhost
    {
    	char group_id[12];
    	char host[100];
    };
    int vhost_count = 0;
    static vhost *vhost_list;
    
    void add_vhost_list(vhost **list, int *count, vhost vhost_add)
    {
    	vhost *tmp_list = (vhost*)malloc((vhost_count * sizeof(vhost)) + sizeof(vhost));
    
    	int x;
    	for(x = 0; x < vhost_count; x++)
    		tmp_list[x] = (*list)[x];
    
    	if(vhost_count < 1)
    		tmp_list[0] = vhost_add;
    	else
    		tmp_list[x] = vhost_add;
    
    	vhost_count++;
    
    	*list = tmp_list;
    }
    
    void del_vhost_list(vhost **list, int *count, int index)
    {
    	if(vhost_count > 0 && index <= vhost_count)
    	{
    		vhost *tmp_list = (vhost*)malloc((vhost_count * sizeof(vhost)) - sizeof(vhost));
    
    		int x;
    		for(x = 0; x < vhost_count - 1; x++)
    			if(x < index)
    				tmp_list[x] = (*list)[x];
    			else
    				tmp_list[x] = (*list)[x + 1];
    
    		vhost_count--;
    
    		if(vhost_count > 0)
    			*list = tmp_list;
    	}
    }
    
    int main()
    {
    	vhost tmp_vhost;
    	strcpy(tmp_vhost.group_id, "-4724684");
    	strcpy(tmp_vhost.host, "omg.com");
    	add_vhost_list(&vhost_list, &vhost_count, tmp_vhost);
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Last edited by SlyMaelstrom; 03-27-2006 at 01:01 AM.
    Sent from my iPadŽ

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > vhost *tmp_list = (vhost*)malloc((vhost_count * sizeof(vhost)) + sizeof(vhost));
    There's no need to cast malloc in C
    See the FAQ for details.

    > I am trying to modify the code so that it will accept any data type
    The only real 'any' datatype in C is a void*

    So what you end up with is an array of void* pointers to your actual data. This particular function doesn't care about what the pointer really points to, but you then have the problem of manually checking that the types are OK when you add/remove things from a given array.

    Any particular reason why you can't use C++, and std::vector ?
    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.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Using realloc

    Thanks for everyone's help so far, im working on using realloc instead of malloc. So far this is what I have but I get a segmentation fault when I try to add an item with my function add_vhost.

    Code:
    typedef struct _vhost vhost;
    struct _vhost
    {
    	char group_id[12];
    	char host[100];
    };
    static int vhost_count = 0;
    static vhost *vhost_list;
    
    void add_vhost(vhost **item_list, vhost item)
    {
    	*item_list = realloc(vhost_list, (vhost_count * sizeof(vhost)) + sizeof(vhost));
    	*item_list[vhost_count] = item; // Segmentation fault happens here
    }
    
    int main()
    {
    	vhost tmp_vhost1;
    	strcpy(tmp_vhost1.group_id, "-487554");
    	strcpy(tmp_vhost1.host, "omg.com");
    	vhost_list = realloc(vhost_list, (vhost_count * sizeof(vhost)) + sizeof(vhost));
    	vhost_list[vhost_count] = tmp_vhost1;
    	vhost_count++;
    
    	vhost tmp_vhost2;
    	strcpy(tmp_vhost2.group_id, "874481");
    	strcpy(tmp_vhost2.host, "something.net");
    	vhost_list = realloc(vhost_list, (vhost_count * sizeof(vhost)) + sizeof(vhost));
    	vhost_list[vhost_count] = tmp_vhost2;
    	vhost_count++;
    
    	vhost tmp_vhost3;
    	strcpy(tmp_vhost3.group_id, "16547");
    	strcpy(tmp_vhost3.host, "zoom.org");
    	add_vhost(&vhost_list, tmp_vhost3); // Something bad happens
    	vhost_count++;
    
    	int x;
    	for(x = 0; x < vhost_count; x++)
    	{
    		printf("%s\n", vhost_list[x].host);
    	}
    }
    BTW, I'm not using c++ because this is a module I am writing for another application that is written in c.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 06:14 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 09:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM

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