Is the use of the malloc function in a subfunction causing warnings?

  • Thread starter Thread starter nenyan
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 3K views
nenyan
Messages
67
Reaction score
0
I use function malloc in a subfunction. then I got warrnings:
incompatible implicit declaration of built-in function 'malloc'
incompatible implicit declaration of built-in function 'free'

The code works well. Is it matter?
Code:
#include <stdio.h>
#include <math.h>

int min_in(double *a, int n)
{
  int i, min_i;
  double t;
  t=a[0];
  min_i=0;
  for(i=1;i<n;i++)
  {
    if (t>a[i])
    {
      t=a[i];
      min_i=i;
    }
  }
  return min_i;
}  


void fourweight (int *a, double *b, int n)
{
	int i, k;
	double *c;
	if ( !(c=(double *)malloc(n*sizeof(double))) )    
printf("memory error\n");
	for(i=0;i<n;i++)
		c[i]=b[i];
	for(i=0;i<4;i++)
	{k=min_in(c,n);
	a[i]=k;
	c[k]=10000.0;
	}
free(c);
}

int main()
{
	int i, a[4]={0,0,0,0};
	double b[10]={0.1, 0.2, 0.3, 0.5, 0.11, 0.21, 0.31, 0.9, 1.1, 0.01};
fourweight(a, b, 10);
for (i=0;i<4;i++)
printf("%d, ", a[i]);

}
 
Physics news on Phys.org