Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transfer from C /C ++ ( URGENT)

 
Frank Zheng
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Frank Zheng
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Zheng
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Norm Miller
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18302
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic