malloc() and C compiler bickering

S

srikar2097

174: struct node *head = malloc(sizeof(struct node)); /* A */

COMPILER WARNING:-
-------------------------------------
linked_list.c: In function 'append_as_tail':
linked_list.c:174: warning: incompatible implicit declaration of built-
in function 'malloc'

This is snippet from a program I was writing. I keep getting this
warning when ever I use malloc() as stated in label-A.
what is the problem here?

As I understand malloc() returns a pointer to void and it could be
typecasted to whatever data type malloc was used. But this
was for older versions of C. With ANSI C, I think there is no need to
do type casting. So what is the compiler bickering about ?

Thanks...
 
H

Harald van Dijk

174: struct node *head = malloc(sizeof(struct node)); /* A */

COMPILER WARNING:-

#include said:
This is snippet from a program I was writing. I keep getting this
warning when ever I use malloc() as stated in label-A.
what is the problem here?

It's not the use of malloc, which is fine, it's that you haven't provided
the compiler with a declaration of malloc, so the compiler has to guess.
When it has to guess, it has to give malloc a return type of int. malloc
doesn't return an int. The compiler is warning you about that.
 
S

srikar2097

It's not the use of malloc, which is fine, it's that you haven't provided
the compiler with a declaration of malloc, so the compiler has to guess.
When it has to guess, it has to give malloc a return type of int. malloc
doesn't return an int. The compiler is warning you about that.

Harald, What do you mean by a "declaration of malloc()" could you
explain this statement?

Are you mentioning about a function prototype declaration of malloc().
I understood the explanation you gave in general but this one
statement is not clear.

Thanks in advance...
 
S

srikar2097

It's not the use of malloc, which is fine, it's that you haven't provided
the compiler with a declaration of malloc, so the compiler has to guess.
When it has to guess, it has to give malloc a return type of int. malloc
doesn't return an int. The compiler is warning you about that.

Harlad I got it!!!

I included the library stdlib.h and no warnings !!!

But I was under the impression that the declaration for mem allocation
functions in C are present in alloca.h? I was using this file all
along? What is the difference?
 
S

Stephen Sprunk

srikar2097 said:
Harlad I got it!!!

I included the library stdlib.h and no warnings !!!

But I was under the impression that the declaration for mem allocation
functions in C are present in alloca.h? I was using this file all
along? What is the difference?

<alloca.h> is not a standard header, though on a POSIX system (which I'm
guessing you're using), it declares the function alloca(). malloc() is
declared in the standard header <stdlib.h>.

If you're not familiar with the standard headers, you would be well
served to pick up a copy of K&R2 (and read it, of course) before you
learn any more bad habits.

S
 
S

Stephen Sprunk

srikar2097 said:
Harald, What do you mean by a "declaration of malloc()" could you
explain this statement?

Are you mentioning about a function prototype declaration of malloc().
I understood the explanation you gave in general but this one
statement is not clear.

Thanks in advance...

"#include <stdlib.h>", among other things, puts the equivalent of this
in your code:

void *malloc(size_t size);

This declaration specifies that malloc() returns a (void *). Since your
code does not #include <stdlib.h> nor provide this declaration itself,
the compiler is going to assume that the undeclared function malloc()
returns int. On some platforms, this incorrect assumption will cause
your code to not work properly, which is why the compiler helpfully
warns you about it.

S
 
C

Coos Haak

Op Sat, 13 Dec 2008 08:06:46 -0800 (PST) schreef srikar2097:
174: struct node *head = malloc(sizeof(struct node)); /* A */

COMPILER WARNING:-
-------------------------------------
linked_list.c: In function 'append_as_tail':
linked_list.c:174: warning: incompatible implicit declaration of built-
in function 'malloc'

This is snippet from a program I was writing. I keep getting this
warning when ever I use malloc() as stated in label-A.
what is the problem here?

As I understand malloc() returns a pointer to void and it could be
typecasted to whatever data type malloc was used. But this
was for older versions of C. With ANSI C, I think there is no need to
do type casting. So what is the compiler bickering about ?

Thanks...

Try
#include <stdlib.h>
 
C

Coos Haak

Op Sat, 13 Dec 2008 20:29:01 +0100 schreef Coos Haak:
Op Sat, 13 Dec 2008 08:06:46 -0800 (PST) schreef srikar2097:


Try
#include <stdlib.h>

Sorry, missed the replies of Harald and Stephen!
 
S

srikar2097

<alloca.h> is not a standard header, though on a POSIX system (which I'm
guessing you're using), it declares the function alloca().  malloc() is
declared in the standard header <stdlib.h>.

If you're not familiar with the standard headers, you would be well
served to pick up a copy of K&R2 (and read it, of course) before you
learn any more bad habits.

S

I am running my program in a Mac OS X with a gcc compiler. i.e. you
guessed right. I am running a POSIX system. Anyway thanks for all the
help guys...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top