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

  • Thread starter nenyan
  • Start date
In summary, the conversation discusses the use of the function malloc in a subfunction and the resulting warnings about incompatible implicit declarations of built-in functions. The code is functioning properly, but there is a question about the need to include <stdlib.h> for declarations of malloc and free.
  • #1
nenyan
67
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
  • #2
  • #3
Borek said:
Have you tried Google search? It often helps.

thank you
 
  • #4
In case you haven't already figured it out, you need to #include <stdlib.h> for the declarations of malloc and free.
 
  • #5


I would say that the use of the malloc function in a subfunction is not causing the warnings. The warnings are due to the fact that the compiler is not able to recognize the malloc and free functions, which are defined in the <stdlib.h> header file. To resolve this issue, the <stdlib.h> header file needs to be included in the code. It is important to include all necessary header files in order to avoid any potential errors or warnings. Additionally, the code may work well despite the warnings, but it is always best to address any warnings to ensure the code is functioning correctly and to improve its readability and maintainability.
 

1. What is malloc and how does it work?

Malloc is a function in the C programming language that is used to dynamically allocate memory during program execution. It allocates a certain amount of memory specified by the user and returns a pointer to the first byte of the allocated memory.

2. Why do we need to use malloc?

Malloc is used when we do not know the amount of memory that will be needed for a particular variable or data structure at compile time. It allows us to allocate memory at run time, making our programs more flexible and efficient.

3. What is the difference between malloc and calloc?

Malloc and calloc are both functions used for dynamic memory allocation in C. The main difference between them is that malloc only allocates memory, while calloc allocates and initializes the allocated memory to 0. Additionally, calloc takes two arguments for the size and number of elements, while malloc only takes one argument for the size of the memory to be allocated.

4. How does free() work?

Free is a function that is used to deallocate memory that was previously allocated using malloc, calloc, or realloc. It takes a pointer to the memory that needs to be freed and returns the memory back to the system, making it available for other programs to use.

5. Can we use malloc to allocate memory for arrays?

Yes, malloc can be used to allocate memory for both single variables and arrays. When allocating memory for an array, the size argument should be the total size of the array (number of elements multiplied by the size of each element).

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
1
Views
934
  • Programming and Computer Science
Replies
1
Views
635
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
889
  • Programming and Computer Science
Replies
7
Views
4K
Back
Top