aspose file tools*
The moose likes Java in General and the fly likes Transfer from C /C ++ ( URGENT) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Transfer from C /C ++ ( URGENT)" Watch "Transfer from C /C ++ ( URGENT)" New topic
Author

Transfer from C /C ++ ( URGENT)

Frank Zheng
Ranch Hand

Joined: Jun 12, 2001
Posts: 76
Hi,
I need to to translate some C codes into Java.
C codes:
foo(int[] i);
// in a method
{
...
int* k;
k=(int*)malloc(sizeof(int) *2)+1;
foo(k);
...
}
Could anybody help to give me a hint how to safely handle this pointer arithmetic in Java.
Any recommands of the reference materials would be appreciated also.
Thanks,
Frank


Sun Certified Java Programmer
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You don't have pointers or pointer arithmetic or malloc in Java. This is basic to the language. If you don't understand this, I suggest going through the Java Tutorial at java.sun.com .
Arrays are first-class objects in Java.
Are you trying to allocate an array of 2 ints? Even in
C++, that "+1" serves no purpose. In Java, you just do this:
int[] k = new int[2];
foo(k);


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Frank Zheng
Ranch Hand

Joined: Jun 12, 2001
Posts: 76
Ron,
Thanks for your reply. With the code like this,
{
...
int* k;
k=(int*)malloc(sizeof(int) *2)+1;
foo(k);
...
}
I feel the Java equivlant is :
int [] k=new int[3];
foo(k);
The "+1" means to allocate one more piece of momory of the size of int.
Correct me if I was wrong.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

some other C -> Java conversion problems, such as C unions, global variables, arrays, and structures are explained here.
Jamie
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Actually no.
k=(int*)malloc(sizeof(int) *2)+1;
k=(int*)malloc(4*2)+1;
k=(int*)malloc(8)+1;
looks like k is pointing to the second entry in a two entry array to me.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Frank Zheng
Ranch Hand

Joined: Jun 12, 2001
Posts: 76
I hope anybody who have done the similiar jobs(translate c to Java) could share some experiences (Tips and tricks) to me. Not limited to the pointer arithmetics.
Thanks a lot.
Frank Zheng
Ranch Hand

Joined: Jun 12, 2001
Posts: 76
Thomas,
I would agree with you. That exact What I was confused about.
Is this equivlent to the following:
k=new int[2];
k=k[0+1]; //THIS IS WRONG? is K still an array
In c/c++, pointer can be considered to be the beginning of an array and can be passed to a function that accept an array. How could java do this.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Whoops, I misread the original C++ code. My apologies.
A Java array is a first-class Object. You can't pass a pointer to somewhere in the middle of the array, because Java doesn't have pointers. You need to rethink what your code is trying to do.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It's really not creating an array. It's allocating enough space to hold two ints. It's then setting a pointer to the location in memory where the second int could be stored. It's then passing that location to the foo method. The question becomes, what is the foo method doing? Does the foo method need the first int? Is the first int used anywhere?
Frank Zheng
Ranch Hand

Joined: Jun 12, 2001
Posts: 76
Thanks Thomas for your point.
Yes. The first pointer is needed somewhere in c codes.
Something Like:
k[-1]=k[1];
It is so strange, isn't it?
Well, it seems I need find some workaround to solve this problem.
If only the c code-writter could add more comments about what he is trying to do.
Many Thanks again.
PS. If you have found any more reference about transfer C to java, please kindly let me know to help me finish this tedious job.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Frank Zheng:
It is so strange, isn't it?
Having coded in C for several years I can assure you that all C-coders are twisted monsters who struggle to produce unreadable and unmaintainable code. Fortunately, C is a great language for doing exactly that.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thomas said:
I can assure you that all C-coders are twisted monsters who struggle to produce unreadable and unmaintainable code

Hey Thomas, I may be a twisted monster, but my C/C++
code is in no way unreadable and unmaintainable not
by me anyway, and I'm the guy who wrote it, so there!
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Norm Miller
Ranch Hand

Joined: May 21, 2002
Posts: 56
Frank,
"Effective Java", by Joshua Bloch has a chapter on Java equivalents for C constructs.
Namely,
Replace Structures with Classes.
Replace Unions with Class Hierarchies.
Replace enum constructs with classes.
Replace function pointers with classes and interfaces.
There is more info about this book on the Sun site.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Just out of curiosity, why convert C code to JAVA instead of just rewriting what was written in C to JAVA. Going through the C code and trying to convert the files seems like it would take longer than just seeing what the program does, and the writing a JAVA program to do it.
Maybe your situation cannot work this way. Just an idea.


GenRocket - Experts at Building Test Data
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Gregg Bolinger:
Just out of curiosity, why convert C code to JAVA instead of just rewriting what was written in C to JAVA. Going through the C code and trying to convert the files seems like it would take longer than just seeing what the program does, and the writing a JAVA program to do it.
Maybe your situation cannot work this way. Just an idea.

I just went through this too! It was legacy C code written on an old VAX/VMS system. It took me 2 days of trying to convert it before I gave up. The next day I asked the users what the program did and started from scratch. Finished the new java program by the end of the day!
Jamie
Frank Zheng
Ranch Hand

Joined: Jun 12, 2001
Posts: 76
I just went through this too! It was legacy C code written on an old VAX/VMS system. It took me 2 days of trying to convert it before I gave up. The next day I asked the users what the program did and started from scratch. Finished the new java program by the end of the day!
Jamie

Well, I am not that lucky. For my case, start from scratch in Java is almost impossible for me due to lack of the background electronic engineering knowledge. It was said that the original c code was written by an expert of DSP.
Anyway, Thanks all your responses.
Frank Zheng
Ranch Hand

Joined: Jun 12, 2001
Posts: 76
I just went through this too! It was legacy C code written on an old VAX/VMS system. It took me 2 days of trying to convert it before I gave up. The next day I asked the users what the program did and started from scratch. Finished the new java program by the end of the day!
Jamie

Well, I am not that lucky. For my case, start from scratch in Java is almost impossible for me due to lack of the background electronic engineering knowledge. It was said that the original c code was written by an expert of DSP.
Anyway, Thanks all your responses.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16308
    
  22

Twisted Monster? Hey! I resemble that remark!
There's really not enough context there to make total sense of it, but my guess is that the author wrote a routine that wants to return 2 or 3 ints. There's some ambiguity there that would make sense if it was "char"s instead of ints being returned, but the most literal equivalent would probably be something like this:
final int ASIZE = 2;
int[] retVals = new int[ASIZE+1];
foo( retVals );
Note that retVals would be initialized to { 0, 0, 0} and foo would then be responsible for any value-replacements that would be done:
public void foo( int[] retVals )
{
retVals[0] = 123;
retVals[1] = 456;
}
Twisted monsters, indeed! you haven't seen my Perl code!


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transfer from C /C ++ ( URGENT)