Jump to content


Check out our Community Blogs

RhetoricalRuvim

Member Since 16 Sep 2010
Offline Last Active Oct 25 2017 03:39 PM
-----

Posts I've Made

In Topic: Javascript Closures Help. Unable to save copy of "count' within an II...

25 October 2017 - 03:36 PM

What you want is to grab a copy of the "count" right away, not delaying it till after "count" has changed. What you are doing now is: 

  • Define myFuncs[0] as a function that uses "count". 
  • Change "count". 
  • Call myFuncs[0], which saves a copy of "count", but "count" has changed by the time you call this function, so therefore the count is always 4. 

You are close to the answer. Instead of calling the inside anonymous function right away, you need to wrap the myFuncs[0] = ... inside an outside anonymous function, and call this outside anonymous function right away, before you change "count". 

 

Consider the following example: 

var fs = []; 
for (var i = 0; i < 4; i++) { 
	(function () { 
		var k = i; 
		fs.push (function () { 
			console.log (k); 
                }); 
        }) (); 
} 
for (var j = 0; j < 4; j++) fs[j] (); 

Or, simpler yet, 

(function () { 
	var k = i; 
	fs.push (function () { 
		console.log (k); 
        }); 
}) (); 

There is an outside wrapper anonymous function that gets called right away, and that is the function that saves a copy of "i". Remember, it needs to save a copy before "i" changes, so you need to call it right away, before you change "i" (or "count" in your case). 

 

On the other hand, the inside of this outer function adds a function to the "fs" array. This inside function is just a normal function that uses the saved variable "k". 


In Topic: Intro To Win32 Assembly, Using NASM, Part 3

25 October 2017 - 03:14 PM

Sorry to bump this aged thread, but I can't seem to find an "edit" button on my original post. 

 

There is an important thing I realized recently: put the "use32" at the VERY TOP of the ASM file, or else ALINK will give a reloc warning, as described here

 

The reason for that is that (not sure why I wasn't getting this error back in 2011 when I posted this tutorial) NASM will interpret all code, INCLUDING the "extern" and "import" statements, as 16-bit. So when NASM creates a table where all the addresses of API functions are to be saved, it creates the table with all 16-bit entries. 

 

In summary, just put "use32" at the beginning of the .asm file, before any "import" statements, and it should assemble and link correctly. 


In Topic: basic c++ question, difference between a and a&

25 October 2017 - 02:53 PM

Hello, and welcome to the world of C++! 

 

The ampersand (&) in C++ does one of two things to variables: designates variables as references, or gives the memory address of the variable. Do you know memory addresses already? 

 

First of all, let me start with the latter case, where & tells C++ to use an address rather than the object itself. The variable with the address is called a pointer. Compare this code: 

void myFunc (int number) { 
    printf ("Number: %d", number); 
} 

int main (int argc, char * argv []) { 
    int x = 4; 
    myFunc (x); 
    return 0; 
} 

To this code: 

void myFunc (int * number) { 
    printf ("Number: %d", *number); 
} 

int main (int argc, char * argv []) { 
    int x = 4; 
    myFunc (&x); 
    return 0; 
} 

In the latter case, I changed the function parameter type from "int" to "int *" (a memory address of an 'int' rather than the 'int' itself). The "&" is now required when calling myFunc () because C++ needs an address (& tells it to use the address) rather than a value. 

 

However, pointers are not recommended, so you should avoid them when possible (see this SO answer). Luckily, C++ has references. Under the hood, references are just pointers, but programming them is usually better than programming pointers. Changing the code above to use references: 

void myFunc (int & number) { 
    printf ("Number: %d", number); 
} 

int main (int argc, char * argv []) { 
    int x = 4; 
    myFunc (x); 
    return 0; 
} 

Notice that only one part changed: an "&" for the one argument of myFunc (). So the parameter type is "int &" rather than "int". The myFunc () gets a reference to "x" rather than a value, so myFunc () could change our "x" if it wanted to. Like so: 

void changeMe (int & number) { 
    number = 10; 
} 

int main (int argc, char * argv []) { 
    int x = 1; 
    printf ("%d; ", x); 
    changeMe (x); 
    printf ("%d; ", x); 
    return 0; 
} 

The changeMe () function has full access to the integer, as if it had a pointer to it, so the second printf () statement should print "10;" 

 

You can use references yourself too, not just as function parameters. Take this: 

int main (int argc, char * argv []) { 
    int x = 10; 
    int & y = x; 
    y++; 
    printf ("%d", x); 
    return 0; 
} 

The printf () statement should print "11" because y was a reference to x, and we incremented it. 

 

There is also a binary operation with "&", which is not for declaring variables. Therefore, I won't go into it in my answer, but you can read about it in the Wikipedia article

 

Summary: 

  • It can be a way to tell C++ to use a variable's memory address rather than its value. 
  • It tells C++ to grab a reference (under the hood: pointer) to the object rather than its value.