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: Paul E. Johnson
Subject: Re: question about dynamic memory allocation
Date: Sat, 13 Mar 1999 12:00:42 -0600

Rick Riolo wrote:
> 
> 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?

Yes, that's how I suspected the problem had to do with the dynamically
allocated memory.

After I added the import <misc.h> and changed malloc to xmalloc, the
problem went away.  There are a couple of things I don't understand
about this matter, however.

1.  When I got the original error message about "int converted to
pointer without cast", it warned Marcus that there was a problem and I
needed to add a header file.  Here's what I don't get: why didn't it
give me "malloc not found" or some such message? What version of malloc,
that had integer return, would the compiler find to use when I lack a
proper import statement?

2.  The crashes I got were not very predictable. Is that standard?  The
dynamically allocated memory was created inside an object.  I had the
malloc command in the createEnd statement, so that space was allocated
for the needed array whenever an object of the class was created.  The
model would run through the creation of objects without complaint
sometimes, but other times it would crash when printing out the contents
of one of these dynamically allocated arrays.  The gdb did show a
backtrace that pointed at the line of code that was accessing the
contents of a dynamically allocated array, but seemed to indicate the
crash was happening when the first or second element of the array was
accessed. A few times the program ran without any errors.

Now here is what really has me befuddled. I wanted to show you the gdb
output and ask you how to print the contents of objects to see what's in
there.  So I went back to the code, and took out the import <misc.h> and
changed xmalloc back to malloc and recompiled. And the code does not
crash any more.  One difference I see is that the system has more RAM
free, so perhaps the crash I was getting only happens when the
system/other programs are using up just enough of the resources?


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