I'm very confused on a couple of issues.
- creating multiple structs of two modifiable variables
- increase the amount of structs if needed
- store information in the two modifiable variables using pass by value.
- Can these values be accessed in constant time?
So lets say I have this..
void * OPAQUE;
typedef struct head Head;
typedef struct data Data;
struct head
{
int size;
int capacity;
Data *info;
};
struct data
{
int key;
int values;
}
passing in values using this function...
void insert_data(OPAQUE *hOpaque, int key, int data);
//cast to the known type..
How do I create multiple structures of Data and with each iteration. Each struct gets a new value input so...;
key = 52; data = 43;
those values will be in the first object. Now.. what if I was giving 20 keys and 20 datas. Then I would resize to accommodate the influx of more values to create more structures. I would have 20 structures in total.
Slightly confused on how I should approach this and how it could be done.
Thanks
mallocandfreefunctions? Then you're on your way. Next have you heard of thereallocfunction? If not read more about it. Now when you know about these functions, then start experimenting.realloc(well, you can callmallocto allocate more memory,memcpyto copy the old data, thenfreethe old memory, doing it in a single call toreallocis just much easier). Allocating memory for a structure is just the same as for any other type:Data *my_data = malloc(sizeof *my_data * NUMBER_OF_STRUCTURES_NEEDED);mallocandfreeismallocandfree. They don't care what you use the memory for, themallocfunction just allocates a certain number of bytes. And doing all what you want manually is just whatreallocdoes in a single call. Also there is a possibility thatreallocmight actually extend or reuse the already allocated memory skipping the copying, saving you precious CPU cycles.