Link to home
Start Free TrialLog in
Avatar of dustybryn
dustybryn

asked on

Dynamic arrays

Hi,
   I was wondering whether it was possible to use dynamic arrays in C or if not something which does the job of?

  Here is my scenario. I am reading values (of which there are multiples) from an Oracle table. Each time I read a record i want to write it to another table, however I only want to do this if I haven't already written a record for that specific value. For this reason I wanted to write the value to an array and then check this array each time I read in a new record. My problem is that I have no idea how many unique values there are so I don't want to size the array at the start?

p.s. I cannot restrict my result set in oracle as there are other tables that I need to create for every record.

Any ideas would be much appreciated.

Dust.
Avatar of van_dy
van_dy

Hi Dust,
      Well yes it is possible to allocate memory dynamically in C. You
do that by using the stdlib function malloc(). take a look at the manpage for
it.  However your idea of storing the already written values in a dynamically allocated array
will make the search for a record in your array very slow. i would suggest
you to build an in memory binary tree of already stored values.(lookup in a
tree is relatively faster.
Avatar of dustybryn

ASKER

Thanks for this, can you give me some example code please.
SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Yes, I would also recommend using linked lists. But if you must use dynamic arrays, here is how it can be done...

typedef struct
{
...
} my_record;

static my_record *ptr_records = NULL;
static long num_records = 0;

/* Takes a new record and tries to add it to the array.
 * Returns the index to the new record in the array, or -1 if unsuccessful.
 */
long add_record(my_record *rec)
{
    my_record *temp_ptr_records;

   /* Calculate the new size required for realloc */
    size_t newsize = sizeof(my_record) * (num_records+1);

    /* Realloc and check for failure */
    my_record *temp_ptr_records = (my_record *)realloc(ptr_records, newsize);
    if(temp_ptr_records == NULL)
        return -1;

    /* Realloc succeeded */
    ptr_records = temp_ptr_records;
    return num_records++;
}

long search_record(my_record *ptr_rec)
{
/* You get the picture */
...
}

void main(int argc, char **argv)
{
   ....
   /* Unique record found */
   index = add_record
}

A variation on this is to make ptr_records a double pointer - like so: my_record **ptr_records. In this case, you will have to realloc an extra pointer to my_record * every time AND also malloc a my_record every time, and set the newly allocated pointer to point to the newly allocated my_record struct (sorry for the complicated sentence). The advantage is that the realloc will be faster since it has to copy less amount of data to the newly allocated memory (N pointers instead of N my_record structs).

Regards
Hi dustybryn,
What you're trying to achieve is not uncommon. I suggest you have a closer look at PL/SQL's functionality instead of taking the C detour.
You could use a temporary table, for example.

In case you want to work with a C solution, check C's hsearch(), hcreate(), hdestroy() functions. From the man page:

     #include <search.h>
     ENTRY *hsearch(ENTRY item, ACTION action);
     int hcreate(size_t mekments);
     void hdestroy(void);


Cheers!

Stefan
Another alternative is to set a unique index on your target table's column. You could catch the "Unique constraint violated" error and ignore it.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial