Hi all!
I'm having evil troubles getting some simple code to work. I'm making a simple C routine that adds and remove strings to and from an array of strings. Can anyone spot what I've done wrong?
Pop_Msg simply puts a message to my output, and Set_bulks and Get_bulks get and set the structure I'm using.
My use of pointers is awful, and I'm also concerned about my freeing of memory.
Can anyone help?
Weasel.
I'm having evil troubles getting some simple code to work. I'm making a simple C routine that adds and remove strings to and from an array of strings. Can anyone spot what I've done wrong?
Code:
// Bulking tracker...
// Provides functionality for storing an array of strings in the
// row memory space, together with the size of the array.
// Parameters:
// ADD xxxx - Adds an entry at the end of the array.
// REMOVE xxxx - Removes an entry (if it exists).
// RESET - Resets to a 0 length array.
typedef struct bulkstype {
char** bulks;
int count;
} bulks_struct;
// Debug stuff.
char popmsg[128];
bool DEBUG = true;
// Param one : mode can be one of the following:
// ADD
char trigger[33];
Get_trigger(trigger, 0);
// Param two : String to add/remove.
char param1[16];
Get_param(param1, 1);
// Get our structure.
bulks_struct* bulks;
Get_bulks( bulks );
// By the time we get here, it's a valid structure.
if (strcmp(trigger,"ADD") == 0)
{
if( DEBUG )
{
sprintf( popmsg, "Adding to %s at %d", param1, bulks->count );
Pop_Msg( popmsg );
}
int position = bulks->count + 1;
//Create new one, but one bigger in size.
bulks_struct* bulks_new;
bulks_new = new bulks_struct();
bulks_new->count = position;
bulks_new->bulks = new char*[position];
// Copy original data across.
for( int i=0; i<(position-1); i++ )
{
strcpy(bulks_new->bulks[i], bulks->bulks[i]);
}
// Add new item at end.
strcpy( bulks_new->bulks[position-1], param1);
// Clear up old stuff and assign me as the new bulks thing.
delete( bulks );
bulks = bulks_new;
}
else if (strcmp( trigger, "REMOVE" ) == 0)
{
if( DEBUG )
{
sprintf( popmsg, "Removing %s", param1 );
Pop_Msg( popmsg );
}
int count = bulks->count;
int found = -1;
int i=0;
// Search for matching entry.
for( i=0; i<count; i++ )
{
if( strcmp(bulks->bulks[i], param1) == 0 )
{
if( DEBUG )
{
sprintf( popmsg, "Found %s at %d", bulks->bulks[i], i );
Pop_Msg( popmsg );
}
found = i;
}
}
// If we didn't find it... quit
if( found == -1 )
{
sprintf( popmsg, "Couldn't find it, exiting." );
Pop_Msg( popmsg );
return 0;
}
//Create new one, but one smaller in size.
bulks_struct* bulks_new;
bulks_new = new bulks_struct();
bulks_new->count = count-1;
bulks_new->bulks = new char*[count-1];
// Copy original data across.
int current=0;
for( int i=0; i<(count); i++ )
{
// Except of course the item we don't want.
if( i <> found )
{
strcpy(bulks_new->bulks[current], bulks->bulks[i]);
current++;
}
}
// Clear up old stuff and assign me as the new bulks thing.
delete( bulks );
bulks = bulks_new;
}
else if (strcmp( trigger, "RESET" ) == 0)
{
if( DEBUG )
{
sprintf( popmsg, "Resetting at size %d", bulks->count );
Pop_Msg( popmsg );
}
// Create a new collection with 0 size.
bulks_struct* bulks_new;
bulks_new = new bulks_struct();
bulks_new->count = 0;
// Clear up old stuff and assign me as the new bulks thing.
delete( bulks );
bulks = bulks_new;
Set_UserPointer((char *)bulks);
}
// DEBUG, prints out current structure.
char cur_str[16];
for( int i=0; i<(bulks->count); i++ )
{
strcpy( cur_str, bulks->bulks[i] );
sprintf( popmsg, "%d : %s", i, cur_str );
Pop_Msg( popmsg );
}
Set_bulks( bulks );
Pop_Msg simply puts a message to my output, and Set_bulks and Get_bulks get and set the structure I'm using.
My use of pointers is awful, and I'm also concerned about my freeing of memory.
Can anyone help?
Weasel.