GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Oct-2007, 08:45
Weasel Weasel is offline
New Member
 
Join Date: Oct 2007
Posts: 4
Weasel is on a distinguished road

C 2D array problem. Noob.


Hi all,

Apologies for covering old ground AGAIN... I'm having trouble with a 2D array, pointers, malloc and free (a classic combo). I'm getting a variety of core dumps when running the code below... can anyone spot the problem?

The core dumps are for reasons such as: "free(): invalid next size (fast)". So I guess I'm freeing something I shouldn't be, or corrupting something with overflow. Help!

Firstly, I have declared the structures in my software as follows:
CPP / C++ / C Code:
typedef struct waitingtype {
        char* sym_name;
        char* bulk_name;
        char** alloc_names;
        int alloc_count;
} port_wait_struct;

typedef struct maptype {
        port_wait_struct** waiting_ports;
        int waiting_count;

/* Other stuff here, removed to avoid confusion */
} global_structure;

I am creating the initial pointer as follows:
CPP / C++ / C Code:
global_structure* bulks;
	bulks = (global_structure*)malloc(sizeof(global_structure));
	bulks->waiting_ports = NULL;
	bulks->waiting_count = 0;

The problematic code follows (by the time I get here, I have char arrays param1, param2 and param3 which are all less than 256 characters and are null terminated) :

CPP / C++ / C Code:

	int found=-1;
	for( int i=0; i<bulks->waiting_count; i++ )
	{
		if( strcmp( bulks->waiting_ports[i]->bulk_name, param1 ) == 0 )
		{
			found = i;
		}
	}
	
	if( found == -1 )
	{
		found = bulks->waiting_count;
		port_wait_struct** new_struct = (port_wait_struct**)malloc( found * sizeof(port_wait_struct*) );
		
		// Only copy old values across if there were any
		if( bulks->waiting_count > 0 )
		{
			for( int i=0; i<bulks->waiting_count; i++ )
			{
				new_struct[i] = bulks->waiting_ports[i];
			}
		}
		
		// Put our new value on the end.
		new_struct[found] = (port_wait_struct*)malloc( sizeof( port_wait_struct ) );
		new_struct[found]->bulk_name = (char*)malloc( 256 * sizeof(char) );
		new_struct[found]->sym_name = (char*)malloc( 256 * sizeof(char) );

		strncpy( new_struct[found]->bulk_name, param1, 256 );
		new_struct[found]->bulk_name[256 - 1] = '\0';
		strncpy( new_struct[found]->sym_name, param3, 256 );
		new_struct[found]->sym_name[256 - 1] = '\0';

		new_struct[found]->alloc_count = 0;
		new_struct[found]->alloc_names = NULL;

		// Free old one and put our new one in its place.
		if( bulks->waiting_ports NEQ NULL )
		{
			free( bulks->waiting_ports );
		}
		bulks->waiting_ports = new_struct;
		bulks->waiting_count++;
		
	}
	
	// By here we have a 'found' value, whether it's a new one or an
	// old one.  We need to add ourselves onto the list of names.
	int old_count = bulks->waiting_ports[found]->alloc_count;
	char** new_names = (char**)malloc( (old_count+1) * sizeof(char*) );
	
	// Copy old ones across if there were any.
	if( old_count > 0 )
	{
		for( int i=0; i<old_count; i++ )
		{
			new_names[i] = bulks->waiting_ports[found]->alloc_names[i];
		}
	}
	
	new_names[old_count] = (char*)malloc( 256 * sizeof(char) );
	strcpy( new_names[old_count], param2 );
	
	free( bulks->waiting_ports[found]->alloc_names );
	bulks->waiting_ports[found]->alloc_names = new_names;
	bulks->waiting_ports[found]->alloc_count++;

It's supposed to be an implementation of a map of char-arrays to 'lists' of other char-arrays. param1 is the 'key' and param2 is the char-array to be stored in the 'list'. param3 is kind of irrelevent, but needs to be stored beside the 'key'.

It core dumps if I run the problematic chunk as a function with a whole bunch of test data, and it varies how far through the test data I get.

Help! Much appreciated!
  #2  
Old 16-Oct-2007, 12:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Post

Re: C 2D array problem. Noob.


Quote:
Originally Posted by Weasel
... can anyone spot the problem...

The following allocates memory that you are going to use as an array of pointers to your structs.
CPP / C++ / C Code:
    port_wait_struct** new_struct = malloc(number_of_rows * sizeof(port_wait_struct*));
You have allocated pointers that will be used to refer to the rows of the 2-D array.

For each pointer, you have to allocate an array of structs (memory for all of the structs that are going to be on that row) before you have anything that you can use.

Maybe something like
CPP / C++ / C Code:
    for(i=0; i<however_many_structs_there_are_for_row_number_i; i++)
    {
	new_struct[i] = malloc(sizeof(port_wait_struct));
    }

Now you have the equivalent of a 2-D array of structs. You can access its members by
CPP / C++ / C Code:
    new_struct[i][j]
Where


i is greater than or equal to zero and less than number_of_rows

j is greater than or equal to zero and less than however_many_structs_there_are_for_row_number_i


Now, make sure that you don't change the values of new_struct or any of the new_struct[i] values anywhere in your program. Then when you are through with the array, deallocate the storage by something like the following:

CPP / C++ / C Code:
    for(i=0; i<however_many_structs_there_are_for_row_number_i; i++)
    {
	free(new_struct[i]);
    }
    free(new_struct);


Regards,

Dave
  #3  
Old 16-Oct-2007, 19:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C 2D array problem. Noob.


Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
    for(i=0; i<however_many_structs_there_are_for_row_number_i; i++)
    {
	new_struct[i] = malloc(sizeof(port_wait_struct));
    }
Besides being wrong, that's just plain silly. I hope it's obvious that it should be more like
CPP / C++ / C Code:
    for(i=0; i<number_of_rows; i++)
    {
	new_struct[i] = malloc(sizeof(port_wait_struct) * however_many_structs_there_are_for_row_number_i);
    }

and, for de-allocating:
CPP / C++ / C Code:

    for(i=0; i<number_of_rows; i++)
    {
	free(new_struct[i]);
    }
    free(new_struct);

I regret the error.

Regards,

Dave
  #4  
Old 17-Oct-2007, 02:25
Weasel Weasel is offline
New Member
 
Join Date: Oct 2007
Posts: 4
Weasel is on a distinguished road

Re: C 2D array problem. Noob.


Hi Dave,

Thanks for the reply... I may have wrongly described it as a '2d array' when in fact it's more like an array of pointers to structures that have arrays of pointers to char arrays.

I was also looking more for the bug in my code... which I've now found!
CPP / C++ / C Code:
	if( found == -1 )
	{
		found = bulks->waiting_count;
		port_wait_struct** new_struct = (port_wait_struct**)malloc( found * sizeof(port_wait_struct*) );

should be

CPP / C++ / C Code:
	if( found == -1 )
	{
		found = bulks->waiting_count;
		port_wait_struct** new_struct = (port_wait_struct**)malloc( [b](found+1)[/b] * sizeof(port_wait_struct*) );

So I've been allocating my last struct into unmalloced space, which is why it's been going mental!

Thanks for the help though,
Weasel.
  #5  
Old 17-Oct-2007, 02:26
Weasel Weasel is offline
New Member
 
Join Date: Oct 2007
Posts: 4
Weasel is on a distinguished road

Re: C 2D array problem. Noob.


Obviously without the bold tags around the (found+1), i was trying to emphasize the change!
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with filling a char array in a windows GUI app. ryver C++ Forum 1 13-Aug-2006 08:22
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
Noob question on c arrays and functions brett C Programming Language 1 20-Apr-2005 03:59
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
problem reading to a dynamic array noamfrie C Programming Language 9 02-Jan-2005 18:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.