1

I'm very confused on a couple of issues.

  1. creating multiple structs of two modifiable variables
  2. increase the amount of structs if needed
  3. store information in the two modifiable variables using pass by value.
  4. 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

6
  • 2
    Do you know anything about dynamic memory allocation? I.e. the malloc and free functions? Then you're on your way. Next have you heard of the realloc function? If not read more about it. Now when you know about these functions, then start experimenting. Commented Aug 2, 2016 at 17:05
  • I should have been more specific, But I don't want to use realloc and I know how to use malloc for variables but not structures. Im sure it's the same concept but I can't grasp around it Commented Aug 2, 2016 at 17:08
  • If you want to be able to change the size of an "array" dynamically, then you have no other choice than to use realloc (well, you can call malloc to allocate more memory, memcpy to copy the old data, then free the old memory, doing it in a single call to realloc is 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); Commented Aug 2, 2016 at 17:10
  • You can change the size by creating a new structure that's big enough to point to more structures, I just have to malloc a new space for more structures by creating a temporary, copy the data then free. Then set it equal to temp. I can do this with variables. Unless this is impossible for structures Commented Aug 2, 2016 at 17:12
  • malloc and free is malloc and free. They don't care what you use the memory for, the malloc function just allocates a certain number of bytes. And doing all what you want manually is just what realloc does in a single call. Also there is a possibility that realloc might actually extend or reuse the already allocated memory skipping the copying, saving you precious CPU cycles. Commented Aug 2, 2016 at 17:16

2 Answers 2

1

You can use malloc() to allocate the structures you need:

Data *info;

Could be used to malloc() a structure(s) of type Data, so if you want to allocate 20 Data structs:

// Inside the function that receives the pointer to Headm assume h
// be the poiner to the Head structure:
h -> info = malloc(sizeof(Data) * 20);

Or dynamically reallocate the structure as needed, if we want to increase to 21 structs, for example:

h -> info = realloc(h -> info, (sizeof(Data) * 21));

Another option might be to do:

 struct  head
 {
   int size;
   int capacity;
   Data info;
 };

 struct data
 {
   int key;
   int values;
 }

 Head **h = malloc(sizeof(Head *) * 20);

To allocate an array 20 Head structures pointer, thus you don't have to malloc() the Data struct. you can then use each Head pointer to allocate individual Head structures:

 int s;
 for (s = 0; s < 20; s++) {
     if ((h[s] = malloc(sizeof(Head))) == NULL) {
         perror("malloc()");
         exit(EXIT_FAILURE);
     }
 }

I suppose there are lot's of ways you can do it, depends on your needs.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Just to give you an example, you can do it all manually like

Data *new_data = malloc(old_size + 1 * sizeof *new_data);
if (new_data == NULL)
{
    // Handle error
}
memcpy(new_data, old_data, old_size * sizeof *old_data);
free(old_data);
old_data = new_data;
++old_size;

Or you could do

Data *new_data = realloc(old_data, old_size++ * sizeof *new_data);
if (new_data == NULL)
{
    // Handle error
}
old_data = new_data;

Not counting the error checking and handling, the number of statements have been cut by 60%.

You decide which is simpler. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.