Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Pointers and object initialization

Pointers and object initialization

Scheduled Pinned Locked Moved Solved C++ Gurus
13 Posts 6 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aymeric_Qt
    wrote on 22 May 2020, 17:05 last edited by
    #3

    Hi @SGaist,

    "First thing: turning everything to pointers is not a goal of good C++ programming."
    Too bad I was almost there... :)

    So how do I know what need to be pointers and doesn't need to be ?

    M 1 Reply Last reply 23 May 2020, 00:07
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 May 2020, 17:58 last edited by
      #4

      It will all depend on the data you are going to use, how you are going to use it and depending on the types you are using how you are going to pass them around.

      There's not one final answer because, well, as I wrote above, it depends.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • A Aymeric_Qt
        22 May 2020, 17:05

        Hi @SGaist,

        "First thing: turning everything to pointers is not a goal of good C++ programming."
        Too bad I was almost there... :)

        So how do I know what need to be pointers and doesn't need to be ?

        M Offline
        M Offline
        mpergand
        wrote on 23 May 2020, 00:07 last edited by mpergand
        #5

        @Aymeric_Qt said in Pointers and object initialization:

        Hi @SGaist,

        "First thing: turning everything to pointers is not a goal of good C++ programming."
        Too bad I was almost there... :)

        So how do I know what need to be pointers and doesn't need to be ?

        So you are a beginner, I have a strong advice for you:
        Stay away from pointers as much as possible.
        In 99% of cases they are useless.

        You need to allocate with new for all the QWidgets in Qt, but If you follow carefully the Qt object memory management (parent/child model) you only need to delete the top level objects (windows) and Qt delete all the children in the object tree for you automatically.

        You need to use pointers when you have to allocate dynamically a block of memory, for example, to load raw data from a file.
        Even in this case, Qt provide QByteArray, so you don't need to allocate memory by yourself.

        And for functions parameters, avoid pointers too, use references instead.

        Have fun with Qt :)

        1 Reply Last reply
        2
        • A Offline
          A Offline
          Aymeric_Qt
          wrote on 23 May 2020, 13:15 last edited by
          #6

          @SGaist said in Pointers and object initialization:

          It will all depend on the data you are going to use, how you are going to use it and depending on the types you are using how you are going to pass them around.

          There's not one final answer because, well, as I wrote above, it depends.

          Ok but there must be some rules (so now I need to know how/where do I learn that, I'll continue my resarch).

          @mpergand said in Pointers and object initialization:

          So you are a beginner

          Yes I am! :)

          @mpergand said in Pointers and object initialization:

          @Aymeric_Qt said in Pointers and object initialization:
          You need to allocate with new for all the QWidgets in Qt, but If you follow carefully the Qt object memory management (parent/child model) you only need to delete the top level objects (windows) and Qt delete all the children in the object tree for you automatically.

          What about widgets (like button, lie edit etc..) added in the Qt Creator Design mode, are they automatically parented ?
          Add what about other classes. When I create a class (a service for example) by default, as I understand, it have no parent givent that the constructor, in the header file, look like this:

          explicit httpService(QObject *parent = nullptr);
          

          So will it be destroyed too when the application get closed ?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on 23 May 2020, 16:54 last edited by mpergand
            #7

            Have a look at:
            https://doc.qt.io/qt-5/objecttrees.html

            So will it be destroyed too when the application get closed ?

            In modern computer, each process runs in its own memory space. This space returns back to the system when the process terminates.

            explicit httpService(QObject *parent = nullptr);
            

            In this case you are responsible for deleting the object when you no longer need it. Actually it's what we call "top level widget" like QMainWindow or QDialog, and for them you can set Qt::WA_DeleteOnClose to true for this widgets to be deleted when closed.

            One strongly recommend to use QScopedPointer, smartPointer etc for safety. It's up to you to use them.

            1 Reply Last reply
            2
            • K Offline
              K Offline
              Kent-Dorfman
              wrote on 24 May 2020, 03:26 last edited by
              #8

              Oooohhhh...this could be a fun hornet nest to poke.

              Pointers are not evil...there are just too many people who thought software engineering would be an easy field such that too many practicing SEs are afraid of them so they attempt to dumb down the whole discipline with sometimes ridiculous safety rules.

              1 Reply Last reply
              0
              • A Aymeric_Qt
                21 May 2020, 17:24

                Hello C++ Gurus,
                As a very beginner C++ hobby developer (might not make any sense but I hope you get the idea), I have questions about pointers and hwo to use them when an object is initialzed.

                As far I understand, pointers are used to avoid memory leaks, so they have to used as often as possible.
                So here is the code sample I'm working on:

                QJsonDocument* httpService::getAll(QString *url)
                {
                    int pageNumber = 0;
                    bool isLast = false;
                    QJsonArray *allPagesArray = new QJsonArray();
                    QJsonObject *infos = new QJsonObject();
                
                    do {
                        QString *requestUrl = new QString(url->append(QString::number(pageNumber)));
                        QJsonDocument *objectList = new QJsonDocument(get(*requestUrl));
                
                        if(objectList->object()["content"].isNull()) {
                            break;
                        }
                
                        allPagesArray->append(objectList->object()["content"]);
                
                        isLast = objectList->object()["last"].toBool();
                        pageNumber++;
                
                        if(isLast == true)
                        {
                            infos->insert("totalPages", objectList->object().value("totalPages"));
                            infos->insert("totalElements", objectList->object().value("totalElements"));
                            infos->insert("size", objectList->object().value("size"));
                            allPagesArray->append(*infos);
                        }
                
                    } while(!isLast);
                
                    QJsonDocument *allPagesJsonDoc =  new QJsonDocument(*allPagesArray);
                
                    return allPagesJsonDoc;
                }
                

                Firstly, I don't know how to get 'pageNumber' and 'isLast' (which are primitives), I don't even know if it is necessary.

                Secondly, I've manage to turn every other variables into pointers but I quite sure that they are not destroyed automatically (when they go out of scope for example). So how can I manage that ?

                I know that the pointers subject is very large and complex so if you have some documentation or video or whathever recommandation that I could get, feel free to share (I've already read and take some C++ course/documentation but I still have difficulties to fully understand pointers).

                Thanks for reading.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 25 May 2020, 05:54 last edited by
                #9

                @Aymeric_Qt said in Pointers and object initialization:

                pointers are used to avoid memory leaks, so they have to used as often as possible

                Actually pointers are a source for memory leaks :-) To be more precise: heap allocation is source for memory leaks.
                You should use stack allocation as much as possible and only use pointers (heap allocation) when needed.
                Stack allocation:

                void someFunction()
                {
                    SomeObject obj;
                    obj.doSomething();
                }
                

                obj is automatically deleted as soon as someFunction() finishes!

                Heap allocation with pointer:

                void someFunction()
                {
                    SomeObject *obj = new SomeObject();
                    obj->doSomething();
                }
                

                obj is NOT deleted when someFunction() finishes - you have to explicitly delete it using "delete"!
                You see - using stack allocation in this example you can't create a memory leak, but you can when using heap allocation.

                Also stack allocation is way faster than heap allocation.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply 25 May 2020, 16:14
                3
                • J jsulm
                  25 May 2020, 05:54

                  @Aymeric_Qt said in Pointers and object initialization:

                  pointers are used to avoid memory leaks, so they have to used as often as possible

                  Actually pointers are a source for memory leaks :-) To be more precise: heap allocation is source for memory leaks.
                  You should use stack allocation as much as possible and only use pointers (heap allocation) when needed.
                  Stack allocation:

                  void someFunction()
                  {
                      SomeObject obj;
                      obj.doSomething();
                  }
                  

                  obj is automatically deleted as soon as someFunction() finishes!

                  Heap allocation with pointer:

                  void someFunction()
                  {
                      SomeObject *obj = new SomeObject();
                      obj->doSomething();
                  }
                  

                  obj is NOT deleted when someFunction() finishes - you have to explicitly delete it using "delete"!
                  You see - using stack allocation in this example you can't create a memory leak, but you can when using heap allocation.

                  Also stack allocation is way faster than heap allocation.

                  A Offline
                  A Offline
                  Aymeric_Qt
                  wrote on 25 May 2020, 16:14 last edited by
                  #10

                  Hi @jsulm,

                  @jsulm said in Pointers and object initialization:

                  Actually pointers are a source for memory leaks :-) To be more precise: heap allocation is source for memory leaks.
                  (...)
                  Also stack allocation is way faster than heap allocation.

                  Once again I realise that I got it all wrong :D. I really thought that pointers were used for performances because it allows to avoid to copy object (so it was faster).

                  And what about the infamous stack overflow? Is it still a chance this can happen (without an obvious error like allocate a thousand of objects on the stack at the same time) ?
                  Because I have the feeling that it can goes quite fast when you have a more complex application, if you take in account all the interface widgets (buttons, line edit etc..) plus a few other classes like services etc...

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    Kent-Dorfman
                    wrote on 25 May 2020, 17:19 last edited by Kent-Dorfman
                    #11

                    @Aymeric_Qt said in Pointers and object initialization:

                    Once again I realise that I got it all wrong :D. I really thought that pointers were used for performances because it allows to avoid to copy object (so it was faster).

                    No, you got it right. pointer access to memory can be substantially faster than other methods like array indexing.

                    *(ptr++) is generally faster than v=array[n];
                    // where simply iterating through memory; yes some
                    // std::algorithms can duplicate this behaviour, but they will
                    // invariably use the pointer aproach themselves

                    And what about the infamous stack overflow? Is it still a chance this can happen (without an obvious error like allocate a thousand of objects on the stack at the same time) ?

                    Yes...stack overflow should be considered. there is no one size fits all with regard to stack vs heap storage. each has appropriate use and misuse.

                    Because I have the feeling that it can goes quite fast when you have a more complex application, if you take in account all the interface widgets (buttons, line edit etc..) plus a few other classes like services etc...

                    and don't confuse heap allocation and pointers. they are NOT the same thing. in heap allocation you reference data through a pointer, but a pointer can point to ANY ADDRESS or piece of data. So, pointer is a lower level method of accessing memory than a variable is.

                    int local() {
                    int myVar{ 34 };
                    int* varPtr{ &myVar };    // points to stack location of myVar
                    int* heapVar = new int[50]; 
                    // heapVar is pointer to first elem of array on the heap
                    }
                    
                    K 1 Reply Last reply 26 May 2020, 18:52
                    1
                    • K Kent-Dorfman
                      25 May 2020, 17:19

                      @Aymeric_Qt said in Pointers and object initialization:

                      Once again I realise that I got it all wrong :D. I really thought that pointers were used for performances because it allows to avoid to copy object (so it was faster).

                      No, you got it right. pointer access to memory can be substantially faster than other methods like array indexing.

                      *(ptr++) is generally faster than v=array[n];
                      // where simply iterating through memory; yes some
                      // std::algorithms can duplicate this behaviour, but they will
                      // invariably use the pointer aproach themselves

                      And what about the infamous stack overflow? Is it still a chance this can happen (without an obvious error like allocate a thousand of objects on the stack at the same time) ?

                      Yes...stack overflow should be considered. there is no one size fits all with regard to stack vs heap storage. each has appropriate use and misuse.

                      Because I have the feeling that it can goes quite fast when you have a more complex application, if you take in account all the interface widgets (buttons, line edit etc..) plus a few other classes like services etc...

                      and don't confuse heap allocation and pointers. they are NOT the same thing. in heap allocation you reference data through a pointer, but a pointer can point to ANY ADDRESS or piece of data. So, pointer is a lower level method of accessing memory than a variable is.

                      int local() {
                      int myVar{ 34 };
                      int* varPtr{ &myVar };    // points to stack location of myVar
                      int* heapVar = new int[50]; 
                      // heapVar is pointer to first elem of array on the heap
                      }
                      
                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 26 May 2020, 18:52 last edited by kshegunov
                      #12

                      @Kent-Dorfman said in Pointers and object initialization:

                      No, you got it right. pointer access to memory can be substantially faster than other methods like array indexing.

                      *(ptr++) is generally faster than v=array[n];

                      Eh, that one's hard to prove. Consider this simple snippet, it's not easy to judge which is faster. For one, the array indexed piece is SIMD optimized.

                      and don't confuse heap allocation and pointers. they are NOT the same thing. in heap allocation you reference data through a pointer, but a pointer can point to ANY ADDRESS or piece of data. So, pointer is a lower level method of accessing memory than a variable is.

                      Correct, and as such is an indirection by merit of its construction, which more often than not also means a cache miss if locality isn't good. The point is that pointers ain't all moonlight and roses, as you make it sound. I'd rather agree with your statement that it depends on the use case!

                      A decent enough hornets' nest for your taste? :)

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1
                      • A Offline
                        A Offline
                        Aymeric_Qt
                        wrote on 28 May 2020, 15:30 last edited by
                        #13

                        Hey,
                        Sorry for the delay, I didn't had/take enough time for Qt these days.

                        I think this is going a little bit far/too complex for me at the moment. I need to learn and pratice more on pointers.
                        Also my question was too 'general' so it could not be really answered (except with a all disseration and that was not the goal). But I've learn some really usefull informations and that's already something!

                        So thank you all for your replies.

                        Have a nice day.

                        1 Reply Last reply
                        1

                        12/13

                        26 May 2020, 18:52

                        • Login

                        • Login or register to search.
                        12 out of 13
                        • First post
                          12/13
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved