In this case, you'd ideally delete/free the memory later on--not sure
how that works from within JNA. On a small scale, you might be able to
live with this memory leak. Others may have advice on delete/free?
--- interface -------------------------------------
import com.sun.jna.Library;
import com.sun.jna.Pointer;
public interface LibCNative extends Library {
public void free(Pointer p);
public Pointer malloc(long size);
}
--- implementation --------------------------------
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
public class LibC {
private static LibCNative lib;
static {
try {
String LIB_NAME = Platform.isWindows() ? "msvcrt" : "c";
lib = (LibCNative) Native.loadLibrary(LIB_NAME, LibCNative.class);
} catch (Throwable e) {
throw new RuntimeException(e.getMessage());
}
}
public static void free(Pointer p) {
if ((p != null) && (!p.equals(Pointer.NULL))) {
lib.free(p);
p = Pointer.NULL;
}
}
public static Pointer malloc(long size) {
return lib.malloc(size);
}
}
--- example ---------------------------------------
Pointer p = LibC.malloc(12345);
LibC.free(p);
Vorsitzende des Aufsichtsrates: Angelika Mozdzen
Sitz und Registergericht: Hamburg, HRB 90934
Vorstand: Jens-U. Mozdzen
USt-IdNr. DE 814 013 983