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

  • Thread starter Thread starter nenyan
  • Start date Start date
AI Thread Summary
The discussion centers around the use of the malloc function in a C program, which triggers warnings about incompatible implicit declarations for both malloc and free. Despite the code functioning correctly, the warnings indicate a missing header file. The solution provided is to include the stdlib.h header, which contains the necessary declarations for memory allocation functions. This inclusion resolves the warnings and ensures proper function declarations are recognized by the compiler.
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]);

}
 
Technology news on Phys.org
Borek said:
Have you tried Google search? It often helps.

thank you
 
In case you haven't already figured it out, you need to #include <stdlib.h> for the declarations of malloc and free.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top