Understanding C ++ Pointers in ActionScript-Javascript

Learning the basicsof C ++ and trying to wrap your head around pointers and when will you use them. Coming from a Javascript-Actionscript background, I can't think of anything like this.

Can anyone suggest any equivalents to pointers in Javascript or Actionscript 3 and also when you can use them?

+3


source to share


7 replies


Quick and easy answer: There is nothing like pointers in javascript or actionscript, but I could draw some weak comparisons for you.

All languages ​​implement the functions given to you by pointers, but most modern languages ​​hide them from the programmer. C / C ++ gives you a lot of energy by exposing them and asking you to use them; most other languages ​​do not allow programmers to harm themselves by hiding them. Taking responsibility for managing your own memory can be dangerous. Also, remember that C and C ++ are earlier languages ​​than JavaScript, ActionScript, or even the Java or C # language pointer syntax required for these "primitive" times. (C is very close to assembler in memory management, and C ++ is directly from C.)

But every programming language must still manage memory - they must allow you to create (new) variables, access those variables, and store references to them. The same is true for functions.



Look at some of the things you do and think about them with a pointer mindset.

  • When you set a variable equal to a named function, your virtual machine finds the function in memory and sets the variable to point to that memory. (In C, this is called a function pointer. C ++ also uses this.)
  • When you set a variable to be equal to an anonymous function, your virtual machine creates a function somewhere in memory, sets a variable to point to that area in its memory, and lets you reference it.
  • When you call new, you create a new link. Again, the virtual machine creates a slick in memory and gives you access to it.
  • When you pass an object to a function, you are not passing the entire object, you are passing a reference to it. The VM uses your variable to tell the function which part of the memory to play with and how it should behave.

If you start programming a lot in C / C ++, you will find yourself using just about everything you do, pointers (and references, which are a related concept). Once you get used to it, you will find that you have used similar concepts all the time since then - you just didn't need to know exactly what you were doing. You will be much more aware of the fact that every programming language has to do with its memory - implicitly or explicitly.

+4


source


In javascript or...

C ++ has overtaken the concept of a pointer from the C language. Basically, C / C ++ pointers contain specific memory addresses that point to instances of types, but C ++ added polymorphic resolution for class types.

The preferred mechanism for passing (as functional parameters) or storing (as variables, class attributes) references to type instances in C ++ is (uummm) references or values. This may not be possible or required in all situations.



C ++ references (see type specifier &

) are basically the same, but must be initialized to a valid memory address referencing a valid instance of the type. Pointers, by contrast, can also hold a value NULL

, indicating that there is no valid instance of the type.

He further discouraged the use of raw pointer types (C like) in C ++. The Standard Library offers several pointers such as surrogates (types of smart pointers) for specific use cases of semantic use of instance references designed for aso lifecycle management (recall that C ++ has no built-in garbage collection mechanism).

There are many additional resources on this topic on Google and SO.

+1


source


There is no good 1-to-1 mapping. The pointer is hidden in other languages ​​because, although they are very powerful, they are also prone to application crashes.

Typically, you use pointers when the lifetime of the object you are working with allows it to escape your creation context. Even then, it is best to wrap the pointer in another object, which will automatically delete the pointer if needed.

If you put some research into a C ++ reference and when C ++ is silently copying an object (copies of constructors, etc.), the usefulness of references becomes apparent. Then, when you realize that links have some usage restrictions, you are left with pointers. :)

Hope you get started!

+1


source


Considering a pointer (or reference), the value holds the address of the actual object ;
In AS3, any reference to a subclass of the Object class is a pointer:

var a:Point = new Point(0, 0);
var pointer:Point;
pointer = a;

      

Any modification of the "pointer" object actually changes "a", and therefore "pointer" is a pointer / reference (contains the address) to "a", even when passed to the function as arguments: the parameter is a pointer (passed by reference, not by value ).
However, primitive types that are not subclasses of Object, such as Point, are always passed by value (copied) into a new variable or parameter; Primitive types include int, uint, Boolean, Number, String.
By the way, C ++ Pointers are a special kind of "reference" implementation.

+1


source


Consider the following example:

 a = {a:1, b:2};
 b = a;
 b.a = 4;
 console.log(a.a); // outputs 4

      

Something like pointers works with JavaScript objects.

0


source


I like to conceptually think of pointers like a giant table with two columns and an infinite number of rows. In the first column, we have the addresses in memory, and in the second, the values ​​stored at that address, which might look something like this:

char *catString = "cat"; //C picked free space at address 3 to hold this string

 Address     Value
    0         23
    1         'r'
    2         12.2
    3         'c'
    4         'a'
    5         't'
    6         '/0'
   ...        ...

      

Where catString actually contains the value 3, not "cat". The string "cat" is in memory, while the catString pointer just knows where the string is and points to the first letter in the string at that memory address. This is why strings need a null connection, notify address 6, so when printing, the program knows when to stop printing characters. char *str;

is more of a C style that reads, an array of characters, it's equivalent string str;

in C ++, which is much more string friendly!

0


source


Here's another to take over.

Think of a pointer as being hand in hand with a piece of paper that says you are looking at a specific drawer in the kitchen. You can give this piece of paper to everyone who lives in the house, and if they follow the directions on this piece of paper, they all open the same drawer.

If person A places something in a drawer, then gives a piece of paper to person B at some later point, person B can still find what is where person A.

Etc.

0


source







All Articles