Viewing 20 posts - 61 through 80 (of 134 total)
  • Author
    Posts
  • #27548
    Wheels-Of-FireWheels-Of-Fire
    Participant
      @grahamdearsley
      Forumite Points: 4

      They actually did a fair job with arrays and pointers. First they did data types for variables and THEN pointers and THEN pointers into arrays. Even there though they tended to assume that I am already handy with Visual Studio (I am not?).

      I originally taught myself C out of a book on my Atari ST and I dont remember having to read ahead just to understand the topic being talked about, refer back plenty but not forward. I think it may have something to do Hyperlink style of writing used on the web these days. This style just does not translate well into a book or video clip.

      #27648
      Wheels-Of-FireWheels-Of-Fire
      Participant
        @grahamdearsley
        Forumite Points: 4

        I am just about to print out the following and add it to my C++ Course folder as a memory aid. If anyone can spot any errors please let me know.

        #include “pch.h”

        #include <iostream>

        using namespace std;

        int main()

        {

        // Another way to assign a variable

        int Avariable{ 3 };

        cout << Avariable << endl;

        // A pointer to a memory address

        int *Pointer = &Avariable;

        cout << Pointer << endl;

        // DeReff a Pointer

        cout << *Pointer << endl;

        // Reference or alias a variable

        int &Ref = Avariable;

        cout << Ref << endl;

        Ref = Ref++;

        cout << Avariable << endl;

         

        return (0);

        }

        The output is 3, memory address, 3, 4.

         

        #27656
        Wheels-Of-FireWheels-Of-Fire
        Participant
          @grahamdearsley
          Forumite Points: 4

          One error is that the output is 3, memory address, 3,3,4.

          I could also have just used Ref++ instead of Ref=Ref++, a hangover from my BASIC ways ?

          #27659
          Ed PEd P
          Participant
            @edps
            Forumite Points: 39

            int *Pointer = &Avariable

            This is wrong on multiple counts:

            a) you are assigning the address of a value to an integer deferenced pointer. You are trying to do too many things at once

            Pointer = &Apointer

            Anothervariable = *Pointer

            cout << *Anothervariable

            b) If the compiler does not throw an error you are also assigning an address to an int. Typically you need a long or double word to avoid address truncation.

            #27660
            Wheels-Of-FireWheels-Of-Fire
            Participant
              @grahamdearsley
              Forumite Points: 4

              You would think so but it works in Visual Studio and it is the way the Microsoft course told me to do it. If I remove the * dereference from Pointer then I get a type mismatch error. The address in Pointer is indeed a four byte value. They give some reason about why this works but its in a video clip and I have played it back several times without being any the wiser.

              I wrote the code myself in Visual Studio and pasted it here so you can paste it back and try it if you like ?

              #27663
              Wheels-Of-FireWheels-Of-Fire
              Participant
                @grahamdearsley
                Forumite Points: 4

                double Pointer=&Avariable won’t work either, another type mismatch error.

                #27664
                Ed PEd P
                Participant
                  @edps
                  Forumite Points: 39

                  The address in *pointer (your example) is undefined, so the behaviour is also undefined. The value in APointer is being assigned to an undefined random address.

                  If the Visual Studio compiler allows this behaviour by default then I think it stinks. At the very least ^pointer needs to be assigned to a defined and initialised variable at the start of the code block. e.g *pointer = 20

                  #27665
                  Wheels-Of-FireWheels-Of-Fire
                  Participant
                    @grahamdearsley
                    Forumite Points: 4

                    Another thing that was shown in a video clip, but not actually mentioned, relates to splitting your function definitions  into a separate cpp ( only just realised that cpp stands for C plus plus ?) file. They say you should create a new header file with the function declaration in it, fine. They say that your new cpp file with the function definition in it and your main cpp must include your header file, fine. They do not say that EVERY cpp file must include the “Pre Compiled Header”.  Bloody thing would not build ?

                    #27666
                    Wheels-Of-FireWheels-Of-Fire
                    Participant
                      @grahamdearsley
                      Forumite Points: 4

                      Avariable is declared and initialised with a value of 3 at the beginning of the program so it has an address in memory. Int* Pointer=&Avariable really does put the memory address of Avariable into Pointer even though it is 4 bytes long. It does not matter if I put the space before or after the * either.

                      #27667
                      Wheels-Of-FireWheels-Of-Fire
                      Participant
                        @grahamdearsley
                        Forumite Points: 4

                        Ah. Reading ahead again they have their explination in print. We are “overloading” the use of the * operator, we are using it both to declare the use of a pointer (int *Pointer=&Avariable) and to dereference a variable (cout << *Pointer in my example). If I just want to reserve some memory for a pointer of type int then apparently I must use the NEW key word.

                        Int *Pointer = new int

                        This is all in the memory management chapter, about 3 ahead of where I am !

                        #27668
                        Wheels-Of-FireWheels-Of-Fire
                        Participant
                          @grahamdearsley
                          Forumite Points: 4

                          They also explain the gobbledy gook in the video too.

                          Even though Pointer will contain the memory address of Avariable when when I use int* pointer=&Avariable, Pointer must still be the same data type as Avariable because it must be capable of holding its value as well. Pick the bones out of that if you will ?

                          #27669
                          Wheels-Of-FireWheels-Of-Fire
                          Participant
                            @grahamdearsley
                            Forumite Points: 4

                            Oh yeh, we are also “overloading” the & operator. Int &Ref = Avariable gives us a reference to Avariable called Ref as you would expect but int *pointer = &Avariable gives us a pointer to the memory address of Avariable. And of course * and & are already used as multiply and a logical operator so it all depends on where they appear in your code. Did they have to do that ?

                            #27670
                            Wheels-Of-FireWheels-Of-Fire
                            Participant
                              @grahamdearsley
                              Forumite Points: 4

                              I found the link below when I was looking up the C++ standard.

                              https://isocpp.org/get-started

                              I will look into some of these books later.

                              #27684
                              Ed PEd P
                              Participant
                                @edps
                                Forumite Points: 39

                                OK Now I now what happened. It just goes to show that overloading can be easily abused. Personally I think this is a very dangerous area to abuse, and as far as I can see gains little in this usage except a small bit of typing. It is far more likely to result in subtle bugs and malign code.

                                #27686
                                Wheels-Of-FireWheels-Of-Fire
                                Participant
                                  @grahamdearsley
                                  Forumite Points: 4

                                  I have put a note on my memory aid sheet ?

                                  As im now looking at the memory management module I have noticed something else. If I use “int *Pointer = new int”  to reserve some memory then Pointer is actually an object. The memory for Pointer will be allocated from the heap instead of the stack and it will remain in use for the life of the program or until I remove it with “delete Pointer”. Don’t quote me on this though because the module on objects is between where I was and memory management and I haven’t read it yet ! They do say that creating objects and failing to delete them after use is a cause of memory leaks though.

                                  #27687
                                  Ed PEd P
                                  Participant
                                    @edps
                                    Forumite Points: 39

                                    Back to Python and away from the modern insanities of C++!

                                    Re Tippons original post, while browsing around WH Smith I came across a small bookazine “Python The complete Manual” published by Future Press £7.99. This manual covers all the Tippon requeste:

                                    i.e. A Python intro.

                                    b) Building an Android app

                                    3) Making Web apps

                                    4) Scraping (Wikipedia)

                                    5) Games programming

                                    It also covers my hobby oprogramming on the Pi and multi-tasking. There is also on-line access to the included code.

                                    Looks like a very good eight quids worth to me.

                                    (Wheels I suggest you start a new thread on C++ as the two languages are very different)

                                    #27691
                                    TipponTippon
                                    Participant
                                      @tippon
                                      Forumite Points: 0

                                      Re Tippons original post, while browsing around WH Smith I came across a small bookazine “Python The complete Manual” published by Future Press £7.99. This manual covers all the Tippon requeste:

                                      Thanks Ed, it sounds great 🙂

                                      I’ve just found what looks like it on the Future Publishing site for £4.99, or the first edition for free through archive.org. Is this the right book?

                                      https://www.myfavouritemagazines.co.uk/tech-and-gadgets-guides-and-specials/python-the-complete-manual-5th-edition/

                                      https://archive.org/details/Python_The_Complete_Manual_First_ED_2016_UK

                                      If it is, I’ll buy the 5th edition, and put the free first edition on my phone. I usually get about an hour of free time when I drop Alice off to nursery, so having it on my phone means I’m more likely to read it regularly 🙂

                                      #27692
                                      Ed PEd P
                                      Participant
                                        @edps
                                        Forumite Points: 39

                                        The £4.99 version appears to be identical (the one in Smiths is the 6th edition, but I cannot see any differences at first glance). Numpy and Beautiful Soup have changed over time, but you should be able to work around the changes (if any). In any case finding out why something does not work is often the best way of learning.

                                        [edit] Just one caution, I’m not sure which Python the book uses. There are two versions currently in side by side use. Version 2.7 and version 3.xx. There are unfortunately some irritating and senseless differences between the two versions such that code from one has to be transliterated into the other. Whichever one the book uses (probably 2.7), stick to it until you are forced to use the other version.

                                        #27696
                                        Wheels-Of-FireWheels-Of-Fire
                                        Participant
                                          @grahamdearsley
                                          Forumite Points: 4

                                          I have submitted a new thread called C++ programming. If and when it appears I have a few things relating to classes and constructors to put in it. I fear I may be on my own here though ?

                                          #27697
                                          Ed PEd P
                                          Participant
                                            @edps
                                            Forumite Points: 39

                                            There were one or two on MM who liked C++.. I have to confess that I prefer C as I find c++ bloated, and less easy to use on a Pi.

                                          Viewing 20 posts - 61 through 80 (of 134 total)
                                          • You must be logged in to reply to this topic.