swarm-support
[Top][All Lists]
Advanced

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

Re: question about dynamic memory allocation


From: Rick Riolo
Subject: Re: question about dynamic memory allocation
Date: Sat, 13 Mar 1999 06:23:12 -0500 (EST)

paul,
I think marcus addressed the warning and where-does-memory-go-at-the-end
questions.    just to add one word about memory: what you have to take
care to do is to drop objects and free unused space inside loops
(including the loop implicit in your schedules), otherwise
you will have "memory leaks" and your program will use more
and more space as it runs, perhaps crashing or at least slowing
it and other processes down as it takes up all of ram,
and the takes up all of swap space.

I can't recall if he addressed the crash problem,
so I'll say a few words.

You are right that you can treat that pointer like an array.
But it also means if you write beyond the end of the allocated
space, unpredictable things can happen---including (but not restricted to)
crashes.   So what does the crash say...seg fault?   Have you tried gdb
to at least try to get a backtrace?

If adding this dynamic memory use is all that you have changed in a 
working program, my first guess would be that you are writing beyond
the allocated space, eg, doing something like
  position[x] = ...
where x is getting set dynamically by the model, and in
some case its getting set wrong, i.e., to some value that
means it tries to reference memory beyound what you allocated.

- r



Rick Riolo                           address@hidden
Program for Study of Complex Systems (PSCS)
4477 Randall Lab                
University of Michigan         Ann Arbor MI 48109-1120
Phone: 734 763 3323                  Fax: 734 763 9267
http://www.pscs.umich.edu/PEOPLE/rlr-home.html

On Fri, 12 Mar 1999, Paul E. Johnson wrote:

> Date: Fri, 12 Mar 1999 16:23:53 -0600
> From: Paul E. Johnson <address@hidden>
> To: swarm <address@hidden>
> Subject: question about dynamic memory allocation
> 
> 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.
> 
> 

                  ==================================
   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]