Is there an equivalent library in java to the CopyMemory API?
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.
Here's a simple CopyMemory in C:
void CopyMemory(char *src, char *dest, int length){
int i;
for (i=0;i++;i<length)
*dest[i] = *src[i];
}
Of course, if you want to copy an object instead of a string, you'd either have to typecast the object pointers to char pointers when you call this CopyMemory, or create (function overloading is nice...) an alternate CopyMemory designed for use with the object you wish to copy.
This should come in handy for me as well, in Java it would become something like this:
public void CopyMemory(char[] src, char[] dest, int length)
{
int i;
for(i = 0; i < length; i++)
{
dest = src;
}
}
Quote from: tA-Kane on April 17, 2003, 02:30 PM
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.
Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
int i;
for (i=0;i++;i<length)
*dest[i] = *src[i];
}
Of course, if you want to copy an object instead of a string, you'd either have to typecast the object pointers to char pointers when you call this CopyMemory, or create (function overloading is nice...) an alternate CopyMemory designed for use with the object you wish to copy.
Too bad Java doesn't have pointers.
Tuberload: get the 1.4 SDK and take a look at java.nio.ByteBuffer. It should have everything you need except for string ops, which are pretty easy to code yourself. Remember: bytebuffer.order(ByteOrder.LITTLE_ENDIAN), or you'll find yourself IP banned. BTW, they work natively with SocketChannels, which are also nonblocking.
Hope this helps.
Quote from: tA-Kane on April 17, 2003, 02:30 PM
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.
Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
int i;
for (i=0;i++;i<length)
*dest[i] = *src[i];
}
Of course, if you want to copy an object instead of a string, you'd either have to typecast the object pointers to char pointers when you call this CopyMemory, or create (function overloading is nice...) an alternate CopyMemory designed for use with the object you wish to copy.
You should use void pointers instead :p
Quote from: Etheran on April 18, 2003, 07:03 PMYou should use void pointers instead :p
I thought about that, but since I don't know how to access/set a void pointer without typecasting, I would just typecast back to a char and do it like it already is.
Being that that could confuse him, I figured I shouldn't do such.
Quote from: tA-Kane on April 18, 2003, 08:30 PM
Quote from: Etheran on April 18, 2003, 07:03 PMYou should use void pointers instead :p
I thought about that, but since I don't know how to access/set a void pointer without typecasting, I would just typecast back to a char and do it like it already is.
Being that that could confuse him, I figured I shouldn't do such.
You can't. What one would do is put have the prototype take void* parameters, and inside the function body, create seperate identifiers which are cast to char*. That way, your callers don't need to cast every time.
Quote from: tA-Kane on April 17, 2003, 02:30 PM
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.
Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
int i;
for (i=0;i++;i<length)
*dest[i] = *src[i];
}
Eh, that wouldn't work. Either use dest
= src or *dest = *src. Using both together you're actually derefrencing the pointers twice.
Quote from: St0rm.iD on April 17, 2003, 08:50 PM
Quote from: tA-Kane on April 17, 2003, 02:30 PM
Quote from: Tuberload on April 17, 2003, 12:01 PM
Is there an equivalent library in java to the CopyMemory API?
Its not hard to make your own, if you cannot find one.
Here's a simple CopyMemory in C:void CopyMemory(char *src, char *dest, int length){
int i;
for (i=0;i++;i<length)
*dest[i] = *src[i];
}
Of course, if you want to copy an object instead of a string, you'd either have to typecast the object pointers to char pointers when you call this CopyMemory, or create (function overloading is nice...) an alternate CopyMemory designed for use with the object you wish to copy.
Too bad Java doesn't have pointers.
Tuberload: get the 1.4 SDK and take a look at java.nio.ByteBuffer. It should have everything you need except for string ops, which are pretty easy to code yourself. Remember: bytebuffer.order(ByteOrder.LITTLE_ENDIAN), or you'll find yourself IP banned. BTW, they work natively with SocketChannels, which are also nonblocking.
Hope this helps.
That's a lie, Java has pointers, they're everywhere. They're just hidden under a cute little layer saying "Look that's not a pointer! That's a reference! Look over there [runs]"
Anyway, Eibro's right, you are dereferencing the pointer twice in that c++ code and it won't work.
A reference is an auto-dereferenced pointer. All objects in Java are pointers that you cannot get the address of. Thus, all objects in Java are references.
$t0rm: 2
iago: 0
Quote from: St0rm.iD on April 20, 2003, 03:54 PM
A reference is an auto-dereferenced pointer. All objects in Java are pointers that you cannot get the address of. Thus, all objects in Java are references.
$t0rm: 2
iago: 0
sort of like vb, except you can do varptr/objptr/strptr/byval -_-
BTW, if you need "real" copy memory, look into System.arraycopy() and java.lang.Cloneable.
Obviuously I've still got a lot to learn regarding C/C++. But, I think I've got the basic concept down.
Well, you wouldn't actually NEED CopyMemory in C, because you have memcpy. I suppose it would go like this:
void copymemory(void* src, void* dest, int len) {
for (int i = 0; i < len; i++) {
*dest = *src;
src++;
dest++;
}
Quote from: St0rm.iD on April 21, 2003, 06:20 PM
I suppose it would go like this:
void copymemory(void* src, void* dest, int len) {
for (int i = 0; i < len; i++) {
*dest = *src;
src++;
dest++;
}
or like this:
#define copymemory memcpy
Storm: Thank you for the useful info. It was hard to find buried under all the other crap:)
I've been programming in Java about 8 months now and have my Oracle certification and working on getting the sun cert, but there is still a lot to learn.
Quote from: Camel on April 22, 2003, 12:45 AM
Quote from: St0rm.iD on April 21, 2003, 06:20 PM
I suppose it would go like this:
void copymemory(void* src, void* dest, int len) {
for (int i = 0; i < len; i++) {
*dest = *src;
src++;
dest++;
}
or like this:
#define copymemory memcpy
That could cause annoying syntax errors; memcpy returns a value, while copymemory does not.
since when was it mandatory to recieve what a function returns?
Quote from: Camel on April 22, 2003, 12:03 PM
since when was it mandatory to recieve what a function returns?
It's a good idea when you're replacing a standardized function. Besides, some C compilers have been known to warn about unchecked return values - perhaps you should use (void)memcpy.
Skywing you're fighting an uphill battle. Most people are too lazy to be concerned with the proper way to do things. Even though learning these techniques will save them and other people much aggravation in the future, the lazy ones only care that their code compiles right now. Nevermind all the problems that come with being a crappy programmer and distributing crappy code. If it compiles and runs today, it's all they need or care about, right?
Quote from: Grok on April 22, 2003, 12:15 PMNevermind all the problems
As the saying goes, "I'll cross that bridge when I get there."
Or we could just use memcpy and be happy.
good call, storm
+1
DAMN, MORE KARMA :(