Coder's Guild Mailing List

RE: calloc vs. malloc

Posted by Frank Hale on 2000-07-15

>This this in mind, if you were allocating a section of
>memory and then were stepping through it (directly
>with pointers), calloc would make sure that you could
>do that. Where as if you used malloc there would be
>the chance that it would work and that it wouldn't.
>
>I am not able to get any documentation at this point
>and so I can't confirm what I have just said, but I do
>tend to think that I am correct (for once perhaps). :)
>
>Eian


I think your defintely correct. If I use malloc after a while of testing it will crash chaotically. What I mean by that is that it will crash for no reason and then if I run it again it will either run as expected or crash unexpectedly.

If I use calloc I haven't seen that sort of weirdness.

In case you wonder what I am doing, I am experimenting with Java's JNI interface. I am trying to write a java binding to the Xlib C API (X Windows System). I am programming it on Linux, and using Suns-JDK1.3beta. Xlib is very complicated and deals with alot of pointers and structures. I have to create Java classes for all these structures and also store a handle to the native pointer inside either an java int or long and then pass it to the java native methods which in turn cast that handle to whatever it points too which is mostly a pointer to some struct in Xlib. JNI is very ugly to program. Things get complicated quickly. Pulling data from Java classes to C can also get very ugly and its easy to loose your place in code and forget whats going on. 

The first test of this library is mostly debug information for me to see if its working. But at the same time I am rewriting the Aewm window manager in Java using this JNI interface. Its a fun project and it would be very cool to see if I can get it to work. 

Best of all if I get this thing working I will have all the nice things in java to build into my desktop, and have a native layer to access anything inside Xlib. (Very Exciting!!!)

Ofcouse I could be a total geek too!

Frank

"HOOAH AIRBORNE!!!"


Get your FREE Email and Voicemail at Lycos Communications - http://comm.lycos.com