swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

question about dynamic memory allocation


From: Paul E. Johnson
Subject: question about dynamic memory allocation
Date: Fri, 12 Mar 1999 16:23:53 -0600

I decided to do some retooling of a project and it requires some dynamic
memory allocation.  Inside objects, arrays must be created, but it may
not be known at compile time how large these must be.  

I've not messed with this before, and while I understand the principles
pretty well, there is a compiler warning I need to ask about.

I'm following the C manual where I declare, in the header file, a
variable such as this:

   double * position;

And then, in the .m file, the memory for the array is allocated like
so:  when the object is created, it learns that its spaceSize is some
integer, and then it does this:


  position = malloc(spaceSize * sizeof(double));
  if (positn == NULL)
    {
      fprintf(stderr, "OUT OF MEMORY \n");
      exit(8);
    }

At compile time, I get this warning, which ordinarily I'd ignore, but
this code crashes, so I'm trying to nail down every issue. The warning
points at the position= malloc line above:

 Point.m: In function `_i_Point__createEnd':
 Point.m:52: warning: assignment makes pointer from integer without a
cast

I have used two other malloc statements in this same set of files and
neither of them causes the warning.  Interesting?  I can force the
warning to go away by writing it like this:
 position = (double *) malloc(spaceSize * sizeof(double));

But since the crash still happens, I don't guess it helped :)

I suppose I should also confess my innocence about using these pointers.
When I use the pointer position, I use it as if it were an array, and do
things like:

     position[0]= 57;
My understanding is that this is allowed and it seems to work for the
most part, and it may be unrelated to the crash I'm experiencing.

Oh, one last thing. When the swarm program quits, does it know how to
free the memory that I've created with malloc?  I understand that if I
want to drop an object, I need to make special care to free(position). 
But if I don't drop it and swarm ends, are the memory globs given back
to the good people who need them?
-- 
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]