2

i wasn't able to do a dynamic array of function pointers, i have troubles understanding how to work with a dynamic array of function pointers when having a pointer to the func_cmp pointer.

int(*func_cmp[])(void *,void*);

numElements++; 
func_cmp=(func_cmp*)realloc(func_cmp, numElements*sizeof(func_cmp*));
func_cmp[numElements-1]=*func_cmp;

i'm not sure about the realloc line.

5
  • What is your question? Commented May 16, 2015 at 15:59
  • what is wrong in the realloc line? what do i need to make it work? Commented May 16, 2015 at 16:00
  • I don't think you can create an array of functions; you can create an array of function pointers. Commented May 16, 2015 at 16:00
  • exactly, sorry for the mistaken question, i meant an array of function pointers Commented May 16, 2015 at 16:04
  • One thing to keep in mind with your realloc approach is that calls to malloc/calloc/realloc are relatively expensive. So creating an initial bock of pointers sufficient to meet your anticipated needs is generally the best approach. Then call realloc, if and only if you need more. It's a tradeoff. If you are on some embedded system with virtually no spare mem, then your realloc-for-each approach may make sense. Otherwise, allocate a sensible number to begin with an then realloc as-needed. Commented May 16, 2015 at 20:18

1 Answer 1

8

Clearest way is to use typedef

#include <stdlib.h>

typedef int (*functype)(void *a, void *);

functype funcs[100];  // static array                                           

functype *moreFuncs; // dynamic array

int main() {
  int capacity = 16;  // initial capacity
  int n = 0;          // initial size
  moreFuncs = malloc(capacity*sizeof(functype));  // heap dynamic array

  // ...

  // adding element and need more space
  if (n >= capacity) {
     capacity *= 2;
     moreFuncs = realloc(moreFuncs, capacity);
     moreFuncs[n++] = <address of new function>;
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

it's not usefull if i allocate memmory i won't use, thats why i'm trying to use realloc!
@user2943683 that's fine -- use malloc to allocate the initial array and resize via realloc as needed.
@user2943683 What are you trying to do, exactly? If you have a dynamically allocated block of memory whose size you wish to change, call it like the malloc above + the pointer to that memory as the first argument.
everytime my function receives a function pointer as a parameter i'm trying to use the realloc to add one more funcion pointer to my array, so i have an static int variable which counts every time the function is called and i reallocate the memmory. that's the "numElements++" for in the code above
@user2943683 the usual scheme is allocate a small amount initially (e.g. capacity = 10, size = 0) and when size >= capacity first double the capacity , realloc using the new capacity, add the new element, and increment the size. Calling realloc each time you add an entry is generally not a sensible way to resource memory.
|

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.