Hi all.
I'm trying to rewrite three similar functions into one generic function. Currently I have something like this that works: (Retyped to summarise, not the full code!)
#define GROWSIZE 100
#define SQL_MAX_QUERY_SIZE 1024
/* This struct is mostly pointless now. I'm fixing a few other bits of code before I do away with it. */
typedef struct {
unsigned char *string;
} SQL_TRANSACTION;
/* For good or bad, these are global */
SQL_TRANSACTION *SQL_inserts = NULL;
unsigned int numInserts = 0;
unsigned int numInsertsAllocated = 0;
SQL_TRANSACTION *SQL_updates = NULL;
unsigned int numUpdates = 0;
unsigned int numUpdatesAllocated = 0;
SQL_TRANSACTION *SQL_deletes = NULL;
unsigned int numDeletes = 0;
unsigned int numDeletesAllocated = 0;
....
Each SQL_TRANSACTION type has a function like this:
/* Add *string to the inserts array */
void SQL_Addto_Inserts(SQL_TRANSACTION *trans) {
if (numInserts == numInsertsAllocated) {
/* Increase the allocations */
numInsertsAllocated += GROWSIZE;
/* I know the return pointer shouldn't be the same pointer passed to realloc, but for now this works and will do! */
SQL_inserts = (SQL_TRANSACTION*) realloc(SQL_inserts, (SQL_MAX_QUERY_SIZE * numInsertsAllocated));
if (SQL_inserts == NULL) {
printf("realloc failed\n");
exit(1);
}
}
/* More code to format the trans->string and
memcpy it to SQL_inserts[numInserts] */
++numInserts;
}
Everything works well like it is, but I want to replace the three functions with one generic function, but I can't work out how to point to the correct array to work with. I have something similar to this:
....
/* Transaction types */
#define SQL_UPDATE 1
#define SQL_INSERT 2
#define SQL_DELETE 3
....
/* transType is one of the above transaction types, passed by the calling function */
void SQL_Addto_Array(unsigned int transType, SQL_TRANSACTION *trans) {
SQL_TRANSACTION **array = NULL; /* Is it correct to use the double ** notation for what I'm trying to do? */
unsigned int *num = NULL;
unsigned int *allocated = NULL;
switch (transType) {
case SQL_UPDATE:
/* Point the pointers to the correct places */
num = &numInserts;
allocated = &numInsertsAllocated;
array = &SQL_inserts; /* Is this incorrect? */
break;
case SQL_INSERT:
....
/* Code for the other types */
....
} /* End switch */
if (*num == *allocated) {
*allocated += SQL_GROWSIZE;
array = (SQL_TRANSACTION*) realloc(array, (SQL_MAX_QUERY_SIZE * (*allocated))); /* I'm fairly certain this is wrong too! */
if (array == NULL) {
printf("realloc failed\n");
exit(1);
}
}
/* More code to format the trans->string */
/* How do I memcpy trans->string now? */
memcpy(array[*num], trans->string, strlen(trans->string)+1); /* Will probably explode the program! (The +1 is to copy the \0 byte) */
++(*num);
}
The above attempt crashes with...
*** glibc detected *** ./wiimpdb.x86: realloc(): invalid pointer: 0x080f29f0 ***
... and anything else I've tried has similar results. I don't really understand the use of pointers to pointers, and reading the docs/tutorials I've found online has just confused me more. Whats the correct way to point the array pointer to where it needs to point, and how do I realloc the pointer it points to? *head explodes* [dead]
[Edited by - DrTwox on August 16, 2008 12:05:47 AM]