implicit declaration of function free is invalid in c99 - c

In xcode 5 I get this warning:
"implicit declaration of function free is invalid in c99"
How should I free my c structures if I can't use the function free()?

You should include <stdlib.h>.

You get that warning because you're calling a function without first declaring it, so the compiler doesn't know about the function.
All functions need to be declared before being called, there are no "built-in" functions in C.
It's true that free() is a function defined in the standard, but it's still not built-in, you must have a prototype for it.
To figure out which header has the prototype, try searching for "man free" and look for a Linux manual page. Close to the top, it says:
#include <stdlib.h>
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
This tells you that in order to use the listed functions, you should add:
#include <stdlib.h>
to your source code.

Related

What `(void)thread_id;` means/does in c? [duplicate]

In some C project, I have seen this code:
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud;
(void)osize;
/* some code not using `ud` or `osize` */
return ptr;
}
Do the two casts to void serve any purpose?
It is there to avoid warnings from the compiler because some parameters are unused.
The reason for having unused parameters in the prototype is usually because the function needs to conform to some external API - perhaps it is a library function, or a pointer to that function is passed to another function that expects this calling convention. However not all arguments used by the calling convention are actually needed in the function itself.
The reason for mentioning the parameter name in the body is to avoid warnings like
unused.c: In function ‘l_alloc’:
unused.c:3:22: warning: unused parameter ‘ud’ [-Wunused-parameter]
void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
^~
This warning can be suppressed with using the actual parameter in the function body. For example if you do have the following statement:
ud;
This warning is now suppressed. However now GCC will produce another warning:
unused.c:5:5: warning: statement with no effect [-Wunused-value]
ud;
^~
This warning tells that the statement ud;, while being syntactically valid C, does not affect anything at all, and is possibly a mistake, not unlike the statement
abort;
which should perhaps have been written as abort(); instead for it to do something.
And that's where the (void) cast comes in - it will tell the compiler unambiguously and explicitly that the statement is supposed to have absolutely no effect at all.

The reason why we need a function prototype for methods that is imported from its header file

I am currently studying about pointer from K&R.
In page, 109 the authors declare function prototypes for methods
int getline(char *, int);
char *alloc(int);
even after, the source file import their standard libraries in the beginning.
#include<stdio.h>
#include<string.h>
isn't their declaration is done in their header file? What is the reason the authors declare the function prototypes of these methods when they are already declared in their own header files?
There is a getline function introduced around 2010 (long after the book was written) but it is not the same as the function you have mentioned. The library function's prototype is:
size_t getline (char ** __lineptr, size_t * __n, FILE * __stream) ;
Also there is no alloc in C. There are three similarly named library functions for memory allocation.
void *malloc(size_t n);
void *calloc(size_t n, size_t size
void *realloc(void *p, size_t size)
I believe you will find the above functions as part of library in K&R.
So K&R had to declare the prototypes of getline and alloc functions and implement them.

Include only a particular definition from header file

I have used strlen() function from the string.h library without including the header which I want to include from the header file which is initially implemented, because I am writing my own implementation of strcpy(),if I include the header it says it's multiple definitions of strcpy ().
So how do I include only a particular definition from the header file.
Do I need to use extern keyword?
#include <stdio.h>
#include <stdlib.h>
#include "exercise 5.5.h"
int main() {
char *s = "hello";
char *t = "helli";
int n = 3;
if (n > strlen(t))
printf("\nsorry the value of n is greater than the size of t");
else {
S = strncpy(s, t, n);
printf("\nther is %d", x);
}
}
The header has definition of strncpy
Terminal trace
exercise. 5.5_main.c:10:7: incompatible implicit declaration of built-in function "strien
exercise 5.5 main.c:10:7: note: include <string.h> or provide a declaration of 'strlen
I don't want to include string.h but how do I explicitly provide definition of strlen
Header
char* strncat(char *s, char *t, int n);
char* strncpy(char *s, char *t, int n);
int strncmp(char *s,char *t, int n);
Reimplementing a standard library function like strcpy can be tricky. Since it's a standard library function, its name is in a sense "reserved" -- you're not supposed to use it yourself. (It's not quite as strongly reserved as is a keyword like switch, but it's still generally a bad idea to try to write a function named strcpy -- not to mention the fact that it's usually perfectly unnecessary!)
In answer to your explicit question, no, there's no way to "selectively include" just your own selection of the declarations in a system header file such as <string.h>.
If for some reason you need to write your own version of strcpy, you have several choices, depending on circumstances.
Rename your own function. For example, call it my_strcpy. (This is the usual approach.)
Make sure the definition of your function is perfectly correct, and matches the declaration in the standard header file exactly. For example, if you have strcpy(char *dst, char *src) {...} or char *strcpy(char *dst, char *src) {...}, those are both wrong -- it needs to be char *strcpy(char *dst, const char *src) {...}.
Don't use the standard strlen function, either, meaning that you don't have to do a #include <string.h> at all. If you need it, write your own version of strlen, too. (This is often the requirement if the reason you're writing your own strcpy is as a teaching exercise: often the assignment says "You may not use any other functions from the standard library.")
Instead of doing a #include <string.h> because you're calling strlen, provide your own prototype for it at the top of your file: extern size_t strlen(const char *);. (This is generally an extremely bad idea for several reasons, and is not a step to be taken except under extreme circumstances and when you know exactly what you're doing.)
It may also be significant to note whether the "redefinition" error you're getting is coming from the compiler or the linker. If it's a compile-time error such as "conflicting types for 'strcpy'", it indicates you probably need to pay attention to point 2 just above. But if it's a link-time error like "ld: duplicate symbol '_strcpy'" there may not be anything you can do about it, and you'll have to fall back on point 1.

Incompatible implicit declaration of built-in function ‘malloc’

I'm getting this error:
warning: incompatible implicit declaration of built-in function ‘malloc’
I am trying to do this:
fileinfo_list* tempList = malloc(sizeof(fileinfo_list));
Just for the reference the struct used at hand is:
typedef struct {
fileinfo** filedata;
size_t nFiles;
size_t size;
size_t fileblock;
} fileinfo_list;
I don't see anything wrong with what I've done. I'm just creating a tempList with the size of 1 x fileinfo_list.
You likely forgot to #include <stdlib.h>
You need to #include <stdlib.h>. Otherwise it's defined as int malloc() which is incompatible with the built-in type void *malloc(size_t).
You're missing #include <stdlib.h>.
The stdlib.h file contains the header information or prototype of the malloc, calloc, realloc and free functions.
So to avoid this warning in ANSI C, you should include the stdlib header file.
The only solution for such warnings is to include stdlib.h in the program.

How to resolve "conflicting types error" in C?

For the following C code (for swapping two numbers) I am getting the "conflicting types" error for swap function:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
printf("enter the numbers to be swapped");
scanf("%d%d",&a,&b);
printf("before swap");
printf("a=%d,b=%d",a,b);
swap(&a,&b,sizeof(int));
printf("after swap");
printf("a=%d,b=%d",a,b);
getch();
}
void swap(void *p1,void *p2,int size)
{
char buffer[size];
memcpy(buffer,p1,size);
memcpy(p1,p2,size);
memcpy(p2,buffer,size);
return(0);
}
Compiler diagnostics:
<source>:10:6: warning: implicit declaration of function 'swap' [-Wimplicit-function-declaration]
swap(&a,&b,sizeof(int));
^~~~
program:15:6: warning: conflicting types for 'swap'
void swap(void *p1,void *p2,int size)
^~~~
Can anybody tell why that error is coming?
What is the solution for that?
The problem is that swap was not declared before it is used. Thus it is assigned a "default signature", one which will in this case not match its actual signature. Quote Andrey T:
The arguments are passed through a set
of strictly defined conversions. int *
pointers will be passed as int *
pointers, for example. In other words,
the parameter types are temporarily
"deduced" from argument types. Only
the return type is assumed to be int.
Aside from that, your code produces a bunch of other warnings. If using gcc, compile with -Wall -pedantic (or even with -Wextra), and be sure to fix each warning before continuing to program additional functionality. Also, you may want to tell the compiler whether you are writing ANSI C (-ansi) or C99 (-std=c99).
Some remarks:
Put spaces after commas.
Make main return an int.
And make it return 0 or return EXIT_SUCCESS.
Import the definition of getch: #include <curses.h>.
Or just use getchar.
Import the definition of memcpy: #include <string.h>.
Don't return something in a void function.
You may want to use malloc to allocate a buffer of variable size. That will also work with older compilers:
void swap(void *p1, void *p2, int size) {
void *buffer = malloc(size);
memcpy(buffer, p1, size);
memcpy(p1, p2, size);
memcpy(p2, buffer, size);
free(buffer);
}
You need to declare swap before using it. For example, put swap above main, or add a prototype for swap like this:
void swap(void *,void *,int);
int main ()
Incidentally main should be int not void and usually it returns the value zero, unless there is an error.
First off, the actual error message wouldn't hurt.
Secondly, making buffer of [size] only works on some compilers (that's a new feature, not all compilers have it yet). Are you sure yours does?
Thirdly, you need to declare swap before calling it. Add a prototype at the top of the file:
void swap(void *p1,void *p2,int size);
You failed to declare your swap explicitly, forcing the compiler to make assumptions about the function at the point of the call. The compiler, in accordance with C rules, will assume that swap is
int swap(int *, int *, size_t)
Later you declare your swap as
void swap(void *, void *, int)
which is obviously different from what the compiler assumed. This is the conflict the compiler is telling you about.
Also, your void swap attempts to return 0. What were you trying to achieve by that?
P.S. It's int main, not void main.
P.P.S. The program is not guaranteed to produce any output if its output does not end in a new-line character.
You may wonder why the program compiles at all without a prototype for swap(), and that's because the compiler is more than a C99 tool. It also compiles C89 and K&R C programs.
C89 added the prototypes. Prior to C89, the compiler didn't need to see the declaration (the prototype) of a function unless it returned something other than int and the types of the formal parameters were not known to the compiler at all. The compiler just called every function with the types of the actual arguments, which received a set of default argument promotions to simplify things. The programmer would run the lint utility to cross-check actual and formal parameters. This program is still shipped with the BSD distributions.
K&R programs and their corresponding code styles are still accepted by your compiler, so when it sees a function for which no prototype is available it just goes ahead and calls it anyway.
In this case, you switch paradigms in between the call and the definition of the function. The K&R C assumptions the compiler made about the undeclared function when it had to generate a call turned out not to be valid. Even if you had written the whole program in the K&R style the compiler would have made the same complaints when it found out the real types of the function arguments.
With GCC, this is a warning,
when you dont declare a function before using it, the compiler tries to guess the declaration using the type of call made to that function.Hence the behavior.
Well, it compiles on http://codepad.org (after removing the getch(), which is irrelevant). Maybe your compiler complains about using memcpy on non-restricted pointers?
In swap() p1 and p2 are not guaranteed not to be aliases. This is an actual bug waiting to happen - calling swap on &a[i] and &a[j] might blow up memcpy when i==j. Either use memmove (which is guaranteed not to blow up on overlapped areas) or declare the pointers restricted.

Resources