Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Emulating public and private in C structs

Name: Anonymous 2010-06-15 11:30

Consider:
foo.h:
struct Foo {
        int public_1;
        int public_2;
}
struct Foo * create_Foo ();
void bar_Foo (struct Foo*);
/* etc */

foo.c:
#include "foo.h"
struct Foo_private {
        struct Foo foo; /* ??? */
        int private_1;
        int private_2;
}
struct Foo * create_Foo (void) {
        return malloc (sizeof(Foo_private));
}
void bar_Foo (struct Foo *foo) {
        Foo_private *foo_p = (Foo_private*) foo; /* ??? */
        /* do whatever with foo_p->private_1 and foo_p->private_2 */
}

Is there anything wrong with this? I'm dubious about the lines commented with ???. I don't want to write getters and setters for the 'public' stuff.

Name: Anonymous 2010-06-18 12:41

>>47
No, because it seems to have fuck-all to do with private variables anymore.
Of course it does. If the security context doesn't allow reflection, then there's no way to access private variables.

In contrast to C++, where you can #define private public and access them, or in C, simply do some pointer arithmetic to access the internals of a library. Java provides real, run-time guarantees that encapsulation won't be violated.

And what is this ``modern memory protection'' written in?
There are platforms that don't deal in this at all you know. There are literally Java hardware CPUs that run Java bytecode (or some equivalent transformation of it). Such CPUs and the OS that runs on them don't need memory protection for different processes; they can even all share the same heap, and yet they're perfectly isolated and secure, with no danger of buggy or malicious code violating encapsulation.

Aside from a hardware Java CPU (this is what a VM is simulating), this is actually how many J2ME and all BlackBerry phones work. They run a Java OS, and any third-party application you download just gets loaded into the VM and run alongside the OS. There's no memory space protection because there doesn't need to be; Java is secure against this.

http://en.wikipedia.org/wiki/Java_processor

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List