Watcom Compiler Question - malloc

I have written a piece of C code under QNX 4.25. It is a subsection of a
much larger piece. I compile it using “cc malloctest.c” and get the
following error and warning:

malloctest.err:

malloctest.c(7): Warning! W101: Non-portable pointer conversion
malloctest.c(19): Error! E1062: Inconsistent return type for function
‘mem_alloc’

I have read the man page concerning malloc and tried the code sample. No
problems. But this warning and error have me baffled. Could someone
please tell me what fundamental issue I am missing? I feel like an
absolute idiot, and I know that it is something dumb. Thanks for any
suggestions. The code follows.

malloctest.c:

#define NULL 0

#include <stdlib.h>

main()
{
char * ptr;
int size = 4096;

ptr = mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static void * mem_alloc(int size)
{
char * ptr;

ptr = malloc(size);
return (ptr);
}

Regards,

Douglas Reed

Try these lines this way

ptr = (char *)mem_alloc(size);

ptr = (char *)malloc(size);

Why do you have “mem_alloc” returning a “void”, instead of a “char *”?
“Douglas Reed” <dreed@guise.com> wrote in message news:3BE0210E.73BF728A@guise.com
I have written a piece of C code under QNX 4.25. It is a subsection of a much larger piece. I compile it using “cc malloctest.c” and get the following error and warning:
malloctest.err:

malloctest.c(7): Warning! W101: Non-portable pointer conversion
malloctest.c(19): Error! E1062: Inconsistent return type for function ‘mem_alloc’

I have read the man page concerning malloc and tried the code sample. No problems. But this warning and error have me baffled. Could someone please tell me what fundamental issue I am missing? I feel like an absolute idiot, and I know that it is something dumb. Thanks for any suggestions. The code follows.

malloctest.c:

#define NULL 0

#include <stdlib.h>

main()
{
char * ptr;
int size = 4096;

ptr = mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static void * mem_alloc(int size)
{
char * ptr;

ptr = malloc(size);
return (ptr);
}

Regards,

Douglas Reed

Ivan,

I modified the code as per your suggestion. Still have the error but no
warning (thanks for that). Any thoughts on the error?

malloctest.c(22): Error! E1062: Inconsistent return type for function
‘mem_alloc’

malloctest.c: (modified)

#define NULL 0

#include <stdlib.h>

main()
{
char * ptr;
int size = 4096;

ptr = (char *) mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static char * mem_alloc(int size)
{
char * ptr;

ptr = (char *) malloc(size);
return (ptr);
}

Thanks for your help.

Douglas Reed

Ivan Bannon wrote:

Try these lines this way ptr = (char *)mem_alloc(size); ptr = (char
*)malloc(size); Why do you have “mem_alloc” returning a “void”,
instead of a “char *”?

“Douglas Reed” <> dreed@guise.com> > wrote in message
news:3BE0210E.73BF728A@guise.com…I have written a piece of
C code under QNX 4.25. It is a subsection of a much larger
piece. I compile it using “cc malloctest.c” and get the
following error and warning:

malloctest.err:

malloctest.c(7): Warning! W101: Non-portable pointer
conversion
malloctest.c(19): Error! E1062: Inconsistent return type for
function ‘mem_alloc’

I have read the man page concerning malloc and tried the
code sample. No problems. But this warning and error have me
baffled. Could someone please tell me what fundamental issue
I am missing? I feel like an absolute idiot, and I know that
it is something dumb. Thanks for any suggestions. The code
follows.

malloctest.c:

#define NULL 0

#include <stdlib.h

main()
{
char * ptr;
int size = 4096;

ptr = mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static void * mem_alloc(int size)
{
char * ptr;

ptr = malloc(size);
return (ptr);
}

Regards,

Douglas Reed
\

Ivan,

I got it! The error message is actually referring to the missing
prototype. Putting “static void * mem_alloc(int size) ;” before main()
removes the error. Thanks for all your help.

Douglas Reed

Ivan Bannon wrote:

Try these lines this way ptr = (char *)mem_alloc(size); ptr = (char
*)malloc(size); Why do you have “mem_alloc” returning a “void”,
instead of a “char *”?

“Douglas Reed” <> dreed@guise.com> > wrote in message
news:3BE0210E.73BF728A@guise.com…I have written a piece of
C code under QNX 4.25. It is a subsection of a much larger
piece. I compile it using “cc malloctest.c” and get the
following error and warning:

malloctest.err:

malloctest.c(7): Warning! W101: Non-portable pointer
conversion
malloctest.c(19): Error! E1062: Inconsistent return type for
function ‘mem_alloc’

I have read the man page concerning malloc and tried the
code sample. No problems. But this warning and error have me
baffled. Could someone please tell me what fundamental issue
I am missing? I feel like an absolute idiot, and I know that
it is something dumb. Thanks for any suggestions. The code
follows.

malloctest.c:

#define NULL 0

#include <stdlib.h

main()
{
char * ptr;
int size = 4096;

ptr = mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static void * mem_alloc(int size)
{
char * ptr;

ptr = malloc(size);
return (ptr);
}

Regards,

Douglas Reed
\

At the invocation of mem_alloc, it has not yet been defined so the
compiler, per ANSI, assumes mem_alloc returns int. Try moving
definition of mem_alloc above main.

Douglas Reed wrote:

I have written a piece of C code under QNX 4.25. It is a subsection of
a much larger piece. I compile it using “cc malloctest.c” and get the
following error and warning:

malloctest.err:

malloctest.c(7): Warning! W101: Non-portable pointer conversion
malloctest.c(19): Error! E1062: Inconsistent return type for function
‘mem_alloc’

I have read the man page concerning malloc and tried the code sample.
No problems. But this warning and error have me baffled. Could someone
please tell me what fundamental issue I am missing? I feel like an
absolute idiot, and I know that it is something dumb. Thanks for any
suggestions. The code follows.

malloctest.c:

#define NULL 0

#include <stdlib.h

main()
{
char * ptr;
int size = 4096;

ptr = mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static void * mem_alloc(int size)
{
char * ptr;

ptr = malloc(size);
return (ptr);
}

Regards,

Douglas Reed

I used a prototype instead, and it worked. Thanks for the advice.

Doug

Dean Douthat wrote:

At the invocation of mem_alloc, it has not yet been defined so the
compiler, per ANSI, assumes mem_alloc returns int. Try moving
definition of mem_alloc above main.

Douglas Reed wrote:

I have written a piece of C code under QNX 4.25. It is a subsection
of a much larger piece. I compile it using “cc malloctest.c” and get
the following error and warning:

malloctest.err:

malloctest.c(7): Warning! W101: Non-portable pointer conversion
malloctest.c(19): Error! E1062: Inconsistent return type for
function ‘mem_alloc’

I have read the man page concerning malloc and tried the code
sample. No problems. But this warning and error have me baffled.
Could someone please tell me what fundamental issue I am missing? I
feel like an absolute idiot, and I know that it is something dumb.
Thanks for any suggestions. The code follows.

malloctest.c:

#define NULL 0

#include <stdlib.h

main()
{
char * ptr;
int size = 4096;

ptr = mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static void * mem_alloc(int size)
{
char * ptr;

ptr = malloc(size);
return (ptr);
}

Regards,

Douglas Reed
\

It is because you are defining a return type of static void, yet actually returning a char *.
“Douglas Reed” <dreed@guise.com> wrote in message news:3BE032F8.B86D00F8@guise.com
Ivan,
I modified the code as per your suggestion. Still have the error but no warning (thanks for that). Any thoughts on the error?

malloctest.c(22): Error! E1062: Inconsistent return type for function ‘mem_alloc’

malloctest.c: (modified)

#define NULL 0

#include <stdlib.h>

main()
{
char * ptr;
int size = 4096;

ptr = (char *) mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static char * mem_alloc(int size)
{
char * ptr;

ptr = (char *) malloc(size);
return (ptr);
}

Thanks for your help.

Douglas Reed

Ivan Bannon wrote:

Try these lines this way ptr = (char *)mem_alloc(size); ptr = (char *)malloc(size); Why do you have “mem_alloc” returning a “void”, instead of a “char *”?
“Douglas Reed” <dreed@guise.com> wrote in message news:3BE0210E.73BF728A@guise.com…I have written a piece of C code under QNX 4.25. It is a subsection of a much larger piece. I compile it using “cc malloctest.c” and get the following error and warning:
malloctest.err:

malloctest.c(7): Warning! W101: Non-portable pointer conversion
malloctest.c(19): Error! E1062: Inconsistent return type for function ‘mem_alloc’

I have read the man page concerning malloc and tried the code sample. No problems. But this warning and error have me baffled. Could someone please tell me what fundamental issue I am missing? I feel like an absolute idiot, and I know that it is something dumb. Thanks for any suggestions. The code follows.

malloctest.c:

#define NULL 0

#include <stdlib.h>

main()
{
char * ptr;
int size = 4096;

ptr = mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static void * mem_alloc(int size)
{
char * ptr;

ptr = malloc(size);
return (ptr);
}

Regards,

Douglas Reed

That is true, but even changing that did not solve the problem. All
declarations of the function, even where it is called by another piece
of code, must be consistent with the prototype. So if file “a.c”
contains the function, and file “b.c” calls it, if the return type in
the code “b.c” is not the same (i.e. a different pointer), the compiler
will flag the function prototype in “a.c” as being inconsistent. A very
misleading error message.

By the way, this code body is inherited from another company, and was
written prior to ANSI code standards, so the pointers are not
consistent. I am cleaning it function by function as I go. And now that
I know what to look for, my compiles are cleaner, and I am spending less
time trying to kill the bogies.

Thanks again for all your help.

Doug

Ivan Bannon wrote:

It is because you are defining a return type of static void, yet
actually returning a char *.

“Douglas Reed” <> dreed@guise.com> > wrote in message
news:3BE032F8.B86D00F8@guise.com…Ivan,

I modified the code as per your suggestion. Still have the
error but no warning (thanks for that). Any thoughts on the
error?

malloctest.c(22): Error! E1062: Inconsistent return type for
function ‘mem_alloc’

malloctest.c: (modified)

#define NULL 0

#include <stdlib.h

main()
{
char * ptr;
int size = 4096;

ptr = (char *) mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static char * mem_alloc(int size)
{
char * ptr;

ptr = (char *) malloc(size);
return (ptr);
}

Thanks for your help.

Douglas Reed

Ivan Bannon wrote:

Try these lines this way ptr = (char *)mem_alloc(size);
ptr = (char *)malloc(size); Why do you have “mem_alloc”
returning a “void”, instead of a “char *”?

“Douglas Reed” <> dreed@guise.com> > wrote in
message news:3BE0210E.73BF728A@guise.com…I
have written a piece of C code under QNX 4.25.
It is a subsection of a much larger piece. I
compile it using “cc malloctest.c” and get the
following error and warning:

malloctest.err:

malloctest.c(7): Warning! W101: Non-portable
pointer conversion
malloctest.c(19): Error! E1062: Inconsistent
return type for function ‘mem_alloc’

I have read the man page concerning malloc and
tried the code sample. No problems. But this
warning and error have me baffled. Could someone
please tell me what fundamental issue I am
missing? I feel like an absolute idiot, and I
know that it is something dumb. Thanks for any
suggestions. The code follows.

malloctest.c:

#define NULL 0

#include <stdlib.h

main()
{
char * ptr;
int size = 4096;

ptr = mem_alloc(size);
if (ptr == NULL)
printf(“no memory allocated”);
else
{
printf(“Pointer = %6.6X\n”, ptr);
free(ptr);
}
exit(0);
}

static void * mem_alloc(int size)
{
char * ptr;

ptr = malloc(size);
return (ptr);
}

Regards,

Douglas Reed
\