7 Replies - 4773 Views - Last Post: 04 December 2009 - 03:57 PM Rate Topic: -----

#1 dsptl  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 09-February 09

C++ Deep Binding

Posted 04 December 2009 - 12:17 PM

Hi

Can someone give me an EXAMPLE program of below:

demonstrating that C++ uses deep binding to determine the referencing environment for a subprogram that is passes as a parameter.

Just need a small complete program example, I cant find any source
Is This A Good Question/Topic? 0
  • +

Replies To: C++ Deep Binding

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: C++ Deep Binding

Posted 04 December 2009 - 12:19 PM

KYA, apparently this person has NOT read your latest blog entry ;)
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C++ Deep Binding

Posted 04 December 2009 - 12:20 PM

deep binding?


edit: jack, hehe apparently not

This post has been edited by KYA: 04 December 2009 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 Shadowrunner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

Re: C++ Deep Binding

Posted 04 December 2009 - 01:25 PM

View PostKYA, on 4 Dec, 2009 - 11:20 AM, said:

deep binding?


edit: jack, hehe apparently not


While his question does sound pretty much exactly like my homework problem, as well as is a simple request for code, both of which are put in nice red letters at the top of a post saying it will not be done, I am also in the same boat as him, and it is associated with a homework question, as such if it is "out of bounds" to ask for assistance, just let me know and I'll be content.

For our assignment, we are asked to prove deep-binding in C++. In the class lecture (as well as his notes) we are given an example using Javascript, which is as follows:
function sub1() { 
  var x;
  function sub2() 
	{ alert(x); // Creates a dialog box with the value of x
  }; 
  function sub3() {
  var x;
  x = 3;
  sub4(sub2);
  }; 
  function sub4(subx) {
  var x; x = 4; subx();
  };
  x = 1; 
  sub3();
};


Now, there are a lot of differences between Javascript and C++, obviously, so he informed us that we are allowed to use this code and translate it to C++ as proof. But that's where I suppose I need help. Obviously you cannot declare functions within functions in C++, so the functions would have to be declared seperately, as such (my own code):
void sub1 ( ) {
	int x;
	x = 1;
	sub3 ( );
}

int sub2 ( ) {
	cout << x;
}


void sub3 ( ) {
	int x;
	x = 3;
	sub4(sub2( ));
}

int sub4 (int *subx) {
	int x;
	x = 4;
	*subx;
}


(Note: I wound up making some of them int in my attempts to get it to compile and run, as I was having a lot of fun with void error messages about returns...)

now, this wont work as we have to declare the various functions:
void sub1 ();
int sub2 ();
void sub3 ();
int sub4 (int subx);


After several errors, I've done the saddeningly unthinkable, and I believe I proved shallow binding rather than deep binding with the following code:
#include <iostream>
using namespace std;

void sub1 ();
int sub2 ();
void sub3 ();
int sub4 (int subx);
int x;

void sub1 ( ) {
	//int x;
	x = 1;
	sub3 ( );
}

int sub2 ( ) {
	cout << x;
}


void sub3 ( ) {
	//int x;
	x = 3;
	sub4(sub2( ));
}

int sub4 (int subx) {
	//int x;
	x = 4;
	subx;
}

int main (int argc, char * const argv[]) {
	// insert code here...
	sub1( );
	return 0;
}



If i understand deep binding correctly, the javascript has x as being equal to 1, where shallow binding would be = 3. For my C++ example, I have 3. I know it's because of how sub2 is called in sub3, and sub3 is called in sub1, but shouldn't the value for deep binding be 1? I must be missing some key concept (save the void fun).

If this is not enough, perhaps I might be able to give a better explanation.

EDIT: Also, yes, i'm aware i'm doing int function calls without returning, I figure it's not relevant and doesn't cause compliation errors for this example :)

EDIT2: No, maybe not. If i'm not mistaken, shallow binding for this C++ example would be 4? If it's 3, it's going to the call of the subprogram in sub3. However, from my lecture notes it could also be "ad hoc binding". None the less, I -believe- this is an example of Deep Binding, any further tips on how to remove doubt? :)

This post has been edited by Shadowrunner: 04 December 2009 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C++ Deep Binding

Posted 04 December 2009 - 02:09 PM

Excellent first post! Certainly not outside any bounds at all.
--

In C++, unless you pass the variable as a parameter or it's global, the local copy will take precedence.

In your example, 'x' is global so whatever assignment is last before the print will remain in effect.

In the Javascript example, each function has its own copy of the var x, so when sub3 calls sub4 to print x its copy (x=3 copy) is destroyed.

You get 3 in your C++ example because (at least from my viewpoint) port correctly:

//DEEP BINDING EXAMPLE
int x; //our global
void sub3();
void sub2();

//same as java script called from main (which is sub1 equivalent
void sub3(){
	int x = 3; //local copy
	sub2();
}
//this will print out x, I could emulate the Javascript with a 
//function pointer, but that's unnecessary complicated
void sub2() { 
	cout << x << endl;// should be 1
}

int main(){
	//assignment of our global, this is the value we want it to print, eventually
	x = 1;
	sub3();
	return 0;
}



It prints out 1, which is what we want. Without a type specifier in front of x in your function it will invoke the global copy and overwrite its value. The local copy of 'x' is given the value of 3, independent of the global 'x'. when that function exits the local 'x' is destroyed.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#6 Shadowrunner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

Re: C++ Deep Binding

Posted 04 December 2009 - 02:30 PM

View PostKYA, on 4 Dec, 2009 - 01:09 PM, said:

Excellent first post! Certainly not outside any bounds at all.
--
...
--
Hope that helps.


It helps, and I feel I have it right, my only cravat is that it does ask to determine the referencing environment (via deep binding) for the subprogram that is passed as a parameter, in my example, sub2. I think my example still holds true, as he informed us that "ad hoc" binding is never used, and shallow binding would have been at the time of the call, which is in sub4. So.. I've submitted it as my answer, just looking for a second opinion :)

Your example does look a lot neater than mine however, as in my example it would seem that I don't even really need sub1. I was never the best at neat code (it'll be worked on...)

EDIT: I see your point. I actually did originally have x declared in each subprogram, i'll do a test or two to see if I can properly do it so that it does not adjust :)

I suppose it's just the fact that it's in the main program (and/or a global variable) and I feel that there might be some special case with declared variables, but if I add "x = 5;" into the main, my result is 5, which would mean that it's referencing all the way back to the original call of sub1 in the main program? If that's the case I'm not sure why it's not reporting the x = 1 from sub1 when i leave out a declaration in the main program (or also why it doesn't simply produce x as 0 since it is still a global variable)

This post has been edited by Shadowrunner: 04 December 2009 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 Shadowrunner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

Re: C++ Deep Binding

Posted 04 December 2009 - 03:08 PM

No :(, while I do understand your post and I think i have the deep binding down, I feel that I'm passing sub2 incorrectly as a parameter. I'll give it a second lookover (pretty sure I have to have a pointer or two in there somewhere) to see where i'm not properly setting sub2 as a parameter for sub4.

EDIT: I think i'm more afraid of being wrong than I am thinking straight. Your example pretty much proves that it should be right, and i don't see how I would be able to even compile the program if the subprogram was not set correctly as a parameter. (though it could be somehow registering the int as a boolean kind of expression? nah.. really don't think so). I think this is solid. Thanks! :)

This post has been edited by Shadowrunner: 04 December 2009 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C++ Deep Binding

Posted 04 December 2009 - 03:57 PM

When you pass a function, you're really passing what it returns which is different then a function pointer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1