Monday, 15 March 2010

c - Realloc pollfd - conversion to non-scalar -



c - Realloc pollfd - conversion to non-scalar -

i'm using poll function. library has construction so:

struct pollfd{ int fd; short events; short revents; }

now, in code have array of these events , need able realloc , give more memory space more items. have:

#define nrconnections 10 #define stepsize 5 struct pollfd pollfd[nrconnections]; int main(void){ void * temp; temp = realloc((void *)pollfd, (sizeof(pollfd)/sizeof(pollfd[0])) + stepsize); if(temp != null){ pollfd = (struct pollfd *)temp; } }

now, can't seem code within if(temp != null) correct. can't without cast because it's incorrect. can't (struct pollfd) cast because that's non-scalar. isn't working. because pollfd variable done without malloc(), or else wrong?

thanks in advance.

update struct pollfd *pollfd; pollfd = malloc(nrconnections * sizeof(struct pollfd)); if(pollfd == null){ printf("error malloc\n"); fflush(stdout); exit(0); }

this should right initialization then? how create deed array? mean, how access eg 0'th element?

the first argument of realloc must pointer before returned phone call malloc, realloc or calloc, or null pointer. in case, it's not of them.

c malloc realloc

No comments:

Post a Comment