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

  • Thread starter Thread starter nenyan
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the use of the malloc function in a subfunction within a C program, specifically addressing warnings related to incompatible implicit declarations of the built-in functions 'malloc' and 'free'. Participants explore whether these warnings are significant despite the code functioning correctly.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports receiving warnings about incompatible implicit declarations of 'malloc' and 'free' when using them in a subfunction.
  • The same participant questions the significance of these warnings since the code appears to work well.
  • Another participant suggests using Google search as a potential resource for troubleshooting.
  • A later reply indicates that the warnings can be resolved by including the header file , which contains the necessary declarations for 'malloc' and 'free'.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the importance of the warnings, and multiple views regarding the resolution of the issue are presented.

Contextual Notes

The discussion does not address the implications of not including or the potential consequences of ignoring the warnings.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 36 ·
2
Replies
36
Views
3K
Replies
1
Views
2K
Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K