C++, I love you.

Pages: 1234
Even direct, off-heap memory access. :P


Please demonstrate how to do that, because I would legitimately like to know.
see DirectByteBuffer and memory mapped I/O
lol rapidcoder got pissed :P
Java can do pretty much anything that C can...because much of the Java library is just made up of wrapper classes for native methods. That being said, I still have my peeves with Java.

@xander337

1
2
3
4
5
6
7
8
    /**  From java.nio.MappedByteBuffer */
    private native boolean isLoaded0(long address, long length, int pageCount);
    private native void load0(long address, long length);

   /// ...
   /// Many layers of abstraction later...
   /// ...
   ByteBuffer buffer = ByteBuffer.allocateDirect(1024); // Allocate 1024 native bytes. 


Not magical, just obfuscated abstracted to the point where users think that Java's a mystic tool.

Direct native memory access suddenly doesn't seem so direct...as you have the abstraction layers as well as JNI in the way.
Last edited on
I wrote an entire game in Java using JNI. The Java part is opensource:
1
2
3
4
5
6
7
public class Launcher
{
    public static void main(String[] args)
    {
        new Game().Play();
    }
}
1
2
3
4
5
6
7
8
9
public class Game
{
    public Game()
    {
        Init();
    }
    private native void Init();
    public native void Play();
}
It's a really fun game :D


All realistic joking aside, I think I like C++ and Java equally. I just really hate a lot of annoying things in Java.
Last edited on
Alas, your game should be called "UnsatisfiedLinkErrorWorld".

:P
What, my C++ code can't implement the two native methods and then invoke Launcher.main?
You'll need to load the library / libraries like so:

(otherwise Java won't know where to find the native impl. and will throw an UnsatisfiedLinkError)
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/UnsatisfiedLinkError.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class _
{

  private static final class __
  {
    public __()
    {
      ____();
    }

    protected final native synchronized void ____();

    public final native synchronized void _____();
  }

  static
  {
        System.loadLibrary(new StringBuffer().append("my").append("_").append("library").toString());
  }

  public static void main(String[] yearrghhs)
  {
    {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
    new _.__()._____();
    }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  }
}
// Looks like C++ isn't the only language that can be horribly abused to prove a moot point.  :-D 
Last edited on
Actually I used JNI and had the C++ code load the Java code :p
I'll have to check, but I'm reasonably sure that the library will still need to be loaded if you're creating a VM and launching the class in native code. From the class perspective, it won't know whether it was loaded up from native code or not, so I can only assume that the same rules apply to both circumstances.
Last edited on
I've actually done a similar project and it worked fine without having to do any loading Java side :\

// Looks like C++ isn't the only language that can be horribly abused to prove a moot point.


You can write bad Fortran in any language. But the difference is most advanced Java programmers know when they abuse language and know how clean code looks like, while in C++ there are no clean rules of good code.
clean rules of good code

"Clean".
"Good".
Taste.
Opinion.

Either that, or back it up with examples of quality-ambiguous C++ code.

Either that, or back it up with examples of quality-ambiguous C++ code.


Is using exceptions in C++ good or bad? Is doing memory allocation in constructor good or bad? Is using smart pointers everywhere good or bad? Can I store pointers in STL containers (some say you should for performance, some say it is heresy). Is using iostream better, or better stick to stdio? I've seen some style guides which just prohibited many standard C++ features (e.g. at Google), which you could find in some books as an encouraged (and only) way to go (so called modern style).

Oh, what am I talking about, they even didn't come to agreement about using a common class for strings. I think C++ library designers suffer more from NIH syndrome than other coders.

Two smart Java coders can differ by code indent style.
Two smart C++ coders can program in two different C++ languages (each a 20% subset of whole C++, but different subsets).
Last edited on
rapidcoder wrote:
I've seen some style guides which just prohibited many standard C++ features (e.g. at Google), which you could find in some books as an encouraged (and only) way to go (so called modern style).

Yes, Google's public C++ guideline document is laughable "our codebase is broken beyond repair, so please don't use C++ as you normally would when you submit code to us". It doesn't mean that people don't use correct C++ in new projects, or in companies where dysfunctional code is properly encapsulated.

Oh, what am I talking about, they even didn't come to agreement about using a common class for strings

There is only one string class template.
Last edited on

Yes, Google's public C++ guideline document is laughable "our codebase is broken beyond repair, so please don't use C++ as you normally would when you submit code to us". It doesn't mean that people don't use correct C++ in new projects, or in companies where dysfunctional code is properly encapsulated.


Google's code works. It is real and maintained. Their development process is better than good 90% of other companies. I didn't see their C++ code, but their Java code / APIs are f***ing awesome clean. So do you imply smartasses at Google can write clean Java but cannot write clean C++? More bad for C++.

Actually they reject many C++ features, because they think their use would lower quality and productivity, not because their code base. If you adhere to their rules, C++ at Google is also quite a pleasant thing to write in, according to my mates at Google, even those who were diehard Java coders.

Please show me a company that uses a "modern C++" guidelines fully, and have a long-term maintained, successful software product. I haven't heard of such companies.


There is only one string class template


Oh r'ly? Qt uses its own. wxWidgets has its own. Symbian uses its own. C++ Builder library had its own. Some use pure C strings (char arrays). Even in STL - it is a *template*, not a single class. I wouldn't like to work on a project that mixed wide and narrow strings.
Last edited on
Is using exceptions in C++ good or bad? Is doing memory allocation in constructor good or bad?

Ha ha! Thanks for the inspiration.
http://ideone.com/xGNtk

I'm interested in justifications for the other examples, but all right...
Google's code works. It is real and maintained.

You'd be surprised how insanely bad can "real and maintained" code be that fulfills the business needs.

So do you imply smartasses at Google can write clean Java but cannot write clean C++?

Those "smartasses" can (and do, within constraints) write good C++ code as far I've seen. They also made several key contributions to C++11 and are working on more proposals for the next standard revision.

My point is that general-purpose corporate guidelines do not define good or even correct programming style. Read a book.

Qt uses its own. wxWidgets has its own. Symbian uses its own. C++ Builder library had its own. Some use pure C strings (char arrays).

All those libraries predate the 1998 standardization of C++, some by many years. Are you saying that it's a bad thing that C++ permits anyone to write and distribute a library of code, or that it's a fault of C++ that that those library maintainers sacrifice simplicity for backwards compatibility?

Even in STL - it is a *template*, not a single class.

That's what makes it so immensely useful even today, despite the quaint late 90's interface design, with all those member functions.
Last edited on

Read a book.


Which one? There are so many. And they promote different styles of coding. :D
There is *no* such thing in C++ world as good / correct programming style. That is why Google has a detailed C++ style guide, but no equally detailed Java style guide (except setting a tab width and indent style).

Quote from news.ycombinator.com:

And the java style guide?!?! Google has invested a lot in java projects, I don't think they don't care about the style there. Strange...
-----
You have no idea. There has been a thread raging for the past week on checked exceptions. An earlier war raged over 80 vs 100 columns.
-----
Perhaps there's no reason for a Google specific Java style guide because the Java style guide from Sun is pretty good.
-----
There is a Google-specific guide. Its mostly of the form "go read the Sun style guide", with some specific clarifications of things the Sun style guide is quiet on.
Last edited on
Well, Java, C#, Haskell and Python all have their own coding styles (in Python's and Haskell's case, it's even enforced by the parser, although Haskell allows you to get around forced indentation by using braces) so there isn't a need for a detailed coding style. C++ allows you to write code in any way that the compiler can understand, which means that if you want people to abide by specific rules, you need to tell them to do it, because the language won't enforce any specific style by itself.
Pages: 1234