Home c++ Pointers and Links

Pointers and Links [duplicate]

Author

Date

Category

  • Why under conditions

    int * p;
    int t = 100;

  • expressions

    * p = 100;
    * p = & amp; t;
    

    are correct, and

    * p = t
    

    is invalid.

    1. Why are these difficulties at all?
    2. And if possible, as always, an example that reveals at least a piece of the deep (as I understand it) idea of ​​links and pointers.

    P.S.
    To clarify, after reading everything above, I understood somewhere like this:

    struct big {/ * some_very_large_struct * /};
    struct big baobab;
    void doSomething (struct big boom) {
    / * do something with boom * /
    };
    void doSomething1 (struct big * boom) {
    / * do something with boom * /
    };
    void doSomething2 (struct big & amp; boom) {
    / * do something with boom * /
    };
    int main {
    void doSomething (baobab); // this function takes a whole copy of the structure to work with
    void doSomething1 (& amp; baobab); // this function takes the cell number where to look for the structure to work
    void doSomething2 (baobab); // this function takes a live structure, but works with a reference to it
    return 0;
    };
    

    Answer 1, authority 100%

    Something you, apparently, read something wrong … Usually in books they explain it very clearly …

    So, on the fingers:


    Here you have a int a variable. This is such a letter, with the help of which the compiler is able to determine what you are telling him about. He knows that behind the letter a lies a value in memory located at some address (an address in memory is a number). Well, you understand that a variable is such a thing that it is convenient to refer to data written in a certain memory area by name.

    So, pointer is, stupidly, the same number, the same address. Those. & amp; a is exactly the memory address where the value is stored. To avoid obscure problems, the type “pointer to int (or to any other type) – int * ” is such a completely separate type.

    So there are two magic operators: * and & amp; . The second by this variable recognizes its address in memory. The first one at the given address (which, as we remember, is stored in a variable of type int *) returns the actual data located at this address.

    int a = 566;
    // Create a variable. The number 566 was written to memory at some address
    // & amp; a - the address at which the number was written
    int * p;
    // Variable of type "pointer to int"
    // Actually, it's clear that all pointers to all types are one raspberry. It's simple
    // 32-bit numbers. But C++ is a strongly typed language, so you can't directly
    // assign char * to int *
    // But you can assign int * to int *
    p = & amp; a; // Remember, & amp; a returns a memory address
    int b = * p;
    // Now the variable b contains the VALUE, which is located at the address to which
    // points to p (and as we know, p points to where the value of a lies)
    b == a;
    // It's right
    // At the moment we have the same number in variables a and b.
    int * p2 = & amp; b;
    // Now p points to a and p2 points to b.
    // a = 566, b = 566, * p = 566, * p2 = 566
    * p2 = 777;
    // At the address pointed to by p2, put the number 777. As we remember, p2
    // points to where the value of b lies. So now b = 777.
    p = & amp; b;
    // Now both pointers point to the same memory location.
    // And so on
    

    I’ll write about links a little later. Or someone else will write …


    Answer 2, authority 52%

    References, like pointers in C++, represent the addresses of objects in memory. Being able to reference a specific object is so important that some languages ​​(Java, for example, or the .NET family) claim that this is practically the only way to work with objects. At this level of abstraction, there is no difference between references and pointers.
    The difference appears one level below: a pointer is a value, an object of the first class, roughly speaking, the number of a memory cell. You can perform some arithmetic operations with them, compare as numbers, etc. In order, having a pointer (i.e., an address), to access an object at this address, the dereference (dereference) operation is applied:

    * p = 100; // pointer p is dereferenced, and in
         // the received memory cell is written the number 100
    

    Links, in turn, do not have the semantics of “object addresses”, but the semantics of the objects themselves. One might think that a reference is a pointer, which itself, automatically, applies dereference. This has several implications: for example, immutability of links. There is simply no syntax for assigning a reference in C++; any such assignment would be an assignment to the object it points to.

    Now about your examples: * p = & amp; t; is incorrect (an attempt to assign to a dereferenced pointer p a pointer to a number (specifically, to the number t), and not the number itself), but * p = t , exactly, absolutely correct (we refer to the address stored in the pointer and write the number t there).

    If we talk about deep thoughts, then, first of all, pointers came from C and remained a heavy legacy. I think incorrect memory addressing is the most common reason for crashing C++ programs. References cannot replace pointers (they are not objects, unlike pointers), but they greatly simplify life when you need to pass some structure weighing a couple of kilobytes as a parameter to a function, and copying will slow down program execution, but you don’t want to mess with pointers with your hands. Links have the same syntax of use as the objects to which they refer, and therefore are more or less interchangeable.

    I feel that I have already written a lot, but I could not articulate anything clear πŸ™
    I hope it became a little clearer


    Answer 3, authority 33%

    1. A link and a pointer are really no different, except that the link is initialized once and cannot be redefined + syntax differently. For example, if a reference is to a class, then a period is used to refer to members. instead of – & gt;
    2. How is * p = t not correct? It is quite correct to itself. If p points to some int variable, then nothing prevents you from doing this (write the value from t to the variable pointed to by p)
    3. What difficulties do you mean? Why pointers?
    4. As I understand it, the difficulty is that you don’t know why pointers are needed? Well, an example like this:

    struct hello {
      int value1;
      int value2;
    };
    struct hello instance;
    void myfunction (struct hello * p) {
      printf ("value1 is% d \ n", p- & gt; value1);
      p- & gt; value2 = 777;
    }
    int main (void) {
      instance.value1 = 1;
      instance.value2 = 2;
      myfunction (& amp; instance);
      return 0;
    }
    

    Answer 4, authority 33%

    speaking about the complexity, I’m talking about the fact of the existence of pointers and links. why are they needed at all? why can’t we just use variables?

    When I was learning C++, I myself had the exact same question. I then just continued reading the book until I came across the use of pointers in practice. I re-read this chapter, once again tried to understand, and it turned out to understand. Then, when I started working with graphics, I studied OpenGL and DirectX, I came across these pointers everywhere, and there were even pointers to pointers. It was then boring to me in my brain. Then I figured it out and got used to it. Now pointers seem to me to be something familiar and necessary.

    If there were no libraries that use pointers, I think we could do without them. But it would suffer performance, development convenience, and program comprehensibility.

    Pointers or links are used:

    1. To pass the address of a complex structure or object to avoid copying. Constant references are the best syntax for this.

    2. To return multiple values ​​from a function. A single return value, via return, from a function may not be enough. There are two ways to return several values: use a structure with several fields (which is very inconvenient if these values ​​are not related in meaning), or write to the address passed through a pointer. In the second method, you can use a reference, but syntactically it looks like passing a parameter, not a return. Therefore, the pointer in this case better shows the intentions of the programmer in the code.
      I will not even call the use of global variables in the third way. This is bad coding style (you will find out why when you get more experience), and it is better to never do it.

    3. Links are needed for copy constructors. You will know about them when you get to class.

    4. All variables are allocated on the stack. They have their own scope, upon exiting from which they are destroyed. To manage the life of a variable, you may need to allocate it from dynamic memory using new and store a pointer to that memory area so that you can access and delete it.

    5. Pointers are used in a data structure such as lists. You will learn about the lists after studying the classes. They should be in any normal C++ book.


    Answer 5, authority 19%

    A link differs from an index in the following way:

    1. The link syntax is simpler (p instead of * p or p.value instead of p- & gt; value)
    2. The link cannot be “null”, that is, it must be initialized with something.
    3. The link cannot be redefined, that is, it will always point to the same memory location.

    See See also an article about links in C++ on Wikipedia.

    In your second question, everything is really correct, as cy6erGn0m said πŸ™‚

    Why do we need links at all is a great question, in my opinion πŸ™‚

    In general, of course, code with links can always be rewritten to code with pointers. Links were introduced in C++ (they were not in C) precisely to simplify the syntax.

    Another advantage of links is that it is easier to modify the code. For example, you have a function that takes an object of some type A:

    void f (A a);
    

    At some point, it becomes clear that objects of type A are too large, and it is too expensive to copy them every time f () is called. In order not to rewrite the function and the places of its call to pointers, you can simply change the function declaration:

    void f (A & amp; a);
    

    or better yet:

    void f (const A & amp; a);
    
    

    Programmers, Start Your Engines!

    Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

    Recent questions