简体   繁体   中英

Using malloc() properly

I use malloc while i was writing code in C, but i am getting

[Warning] conflicting types for built-in function 'malloc'

this warning when i compile.

and this is the code that i use :

starttimer(AorB,increment)
int AorB;  /* A or B is trying to stop timer */
float increment;
{

 struct event *q;
 struct event *evptr;
 char *malloc();

 if (TRACE>2)
    printf("          START TIMER: starting timer at %f\n",time);
 /* be nice: check to see if timer is already started, if so, then  warn */
/* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next)  */
   for (q=evlist; q!=NULL ; q = q->next)  
    if ( (q->evtype==TIMER_INTERRUPT  && q->eventity==AorB) ) { 
      printf("Warning: attempt to start a timer that is already started\n");
      return;
      }

/* create future event for when timer goes off */
   evptr = (struct event *)malloc(sizeof(struct event));
   evptr->evtime =  time + increment;
   evptr->evtype =  TIMER_INTERRUPT;
   evptr->eventity = AorB;
   insertevent(evptr);
} 

thanks in advance.

You need to #include <stdlib.h> , and remove your bogus declaration: char *malloc();

Also, you need to find a newer C reference! The K&R function declaration syntax has been obsolete for a very long time.

Consider changing:

starttimer(AorB,increment)
int AorB;  /* A or B is trying to stop timer */
float increment;
{

to the (sane-looking) ANSI C standard:

int starttimer(int AorB, float increment) {
char *malloc();

I am not sure why you redeclare the library function malloc . Any how here is the declaration of malloc

void *malloc(size_t size);

Please include <stdlib.h>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM