[comp.lang.c] Problems with malloc

storm@cs.mcgill.ca (Marc WANDSCHNEIDER) (04/02/91)

I have written a program in an attempt to impliment queues in ANSI C.  How-
ever, I have run into a few problems.  My compiler at home (TC++ 1.0)
behaves INCREDIBLY strangely with the following pieces of code:

m,m1...m5 are all pointers to a struct of type Message (ie, they're type Link)
msgalloc() returns a type Link, and is sort of taken from the prototype given
on page 146 of K & R 2nd ed.

	m1 = msgalloc();
	messagemakenull(m1);
	m1->source = 7;
	m1->dest = 1;
	m1->sent = 1.0;

	m2 = msgalloc();
	messagemakenull(m2);
	m2->source = 6;
	m2->dest = 2;
	m2->sent = 2.0;
etc...

	m4 = msgallloc();   /* <--- WARNING 1 (see below) */
	messagemakenull(m4);
	m4->source = 4;
	m4->dest = 8;
	m4->sent = 4.0;

Link msgalloc(void)
{
	return (Message *) malloc(sizeof(Message));
}

void messagemakenull(Link makemenull)
{
	makemenull->next = NULL;
	makemenull->source = 0;
	makemenull->dest = 0;
	makemenull->sent = 0;
}

Now, the problem is, TC++ 1.0 will compile this when I run it from the  DOS
prompt, but will give me the warning NON-PORTABLE FUNCTION CONVERSION IN
MAIN at the line m4 = msgalloc(); as indicated above.

However, when I choose BUILD ALL it under the IDE of TC, it SOMETIMES
 generates the same line as an ERROR, but it always generates a LINK ERROR
which states UNDEFINED SYMBOL _MSGALLOC IN MODULE QUEUES.C.

Can somebody please tell me what the *%*&^ is going on.  All I really want
to do is something similar to the NEW(pointer to type X) in PASCAL....

Any help would save my hair.

./*-
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
storm@cs.mcgill.ca         McGill University           It's 11pm, do YOU
Marc Wandschneider         Montreal, CANADA            know what time it is?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

jinx@milton.u.washington.edu (Everyone Knows JINX) (04/02/91)

Did you prototype your functions? If you didn't, you might have to
explicitly cast msgalloc() to Message *.

*shrug*

It's happened to me.

Jimbo

lamont@uts.amdahl.com (Rick LaMont) (04/03/91)

In article <1991Apr1.171908.2198@cs.mcgill.ca> storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes:
>m,m1...m5 are all pointers to a struct of type Message (ie, they're type Link)
>
>	m4 = msgallloc();   /* <--- WARNING 1 (see below) */
>
>Now, the problem is, TC++ 1.0 will compile this when I run it from the  DOS
>prompt, but will give me the warning NON-PORTABLE FUNCTION CONVERSION IN
>MAIN at the line m4 = msgalloc(); as indicated above.
>
>However, when I choose BUILD ALL it under the IDE of TC, it SOMETIMES
> generates the same line as an ERROR, but it always generates a LINK ERROR
>which states UNDEFINED SYMBOL _MSGALLOC IN MODULE QUEUES.C.
>

Since m1-m5 are declared and used consistently, I suspect that m4 is
also a macro or function.  Check your header files, even the system
header files.  I've had lint complain about variables named y0 or y1
because of system Bessel functions.

Just a hunch.


Rick LaMont

karl@ima.isc.com (Karl Heuer) (04/04/91)

In article <1991Apr1.232358.18938@milton.u.washington.edu> jinx@milton.u.washington.edu (Everyone Knows JINX) writes:
>Did you prototype your functions? If you didn't, you might have to
>explicitly cast msgalloc() to Message *.

This is incorrect advice.  If the (non-int) function isn't declared (whether
by prototype or by Classic C declaration is irrelevant), then the program is
wrong, and no amount of casting will make it right.  Non-int% functions *must*
be explicitly declared& before use.

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint
________
% Even int-valued functions *should* be, but for historical reasons the
  language doesn't require it.
& The best way to declare a function is to include the header file associated
  with it, if any.