1

OK, so I've got the following C code:

//for malloc
#include <stdlib.h>

//to define the bool type
#if __STDC_VERSION__ >= 199901L
#include <stdbool.h>
#else
typedef int bool;
#endif

//define structs
typedef struct A{
   int integerValue;
    char characterValue;
} A;

typedef struct B{
    float floatValue;
    bool booleanValue;
} B;

int main(int argc, const char * argv[])
{
    //allocate a void pointer array
    void* myArray[3];

    //fill the array with values of different struct types
    myArray[0] = malloc(sizeof(A));
    myArray[1] = malloc(sizeof(B));
    myArray[2] = malloc(sizeof(A));
}

but I want to be able to dynamically resize the array. I know that you can dynamically resize an array containing just one type like this:

int* myArray;
myArray = malloc(3*sizeof(int));
myArray[0] = 3;

myArray = realloc(myArray,4*sizeof(int));
printf("%i",myArray[0]);

but how would you do this in the upper situation (it needs to be able to handle a virtually infinite number of types). Would it work to reallocate the array with realloc(myArray,newNumberOfIndices*sizeof(ElementWithBiggestSize)), or is there a more elegant way to achieve this?

6
  • 1
    Your array should be storing pointers to these struct elements, not the actual elements themselves. That eliminates the problem you're facing. Commented Nov 5, 2013 at 17:24
  • Why are you using an array of void * ? Commented Nov 5, 2013 at 17:25
  • @CharlieBurns how would you otherwise create an array of multiple types? Commented Nov 5, 2013 at 17:27
  • @user1833511 you wouldn't/shouldn't. An array of pointers to different types, but not an array of different types, you're just asking for trouble stuffing different sized things into non-bounded space. Commented Nov 5, 2013 at 17:31
  • The short awnser is yes it will work, but you are storing pointers in an array (as per your first example and not dependant on sizeof elements) unless of course you want the elements to be tighly packed in memory in which case you will need to keep track of how big the stored data is already (Not a good idea) Commented Nov 5, 2013 at 17:32

1 Answer 1

1
B* b_pointer = (B*) malloc (sizeof(B*));
void** arr = (void**) malloc (3 * sizeof(void*));
arr[0] = b_pointer;

void** new_arr = (void**) malloc (6 * sizeof(A*));
memcpy(new_arr, arr, 3 * sizeof(A*));
// now arr[0] == new_arr[0] == b_pointer;

free(arr);
// now new_arr[0] == b_pointer;

Note that if you're allocating pointers, it doesn't really matter which struct or array (or i don't know what) you want to point to.

PS: happy debugging using void pointer array with different structs

edit: renaming "b_instance to b_pointer", just trying to make it less confusing

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

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.