Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How Allocation / Resize / Free memory with QT?

How Allocation / Resize / Free memory with QT?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 6.5k 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
    AlekseyB
    wrote on last edited by
    #1

    In the Qt-library I haven't found methods to work with memory like malloc, realloc and free() of standard library C++.

    Are there any analogues in Qt?

    mrjjM jsulmJ 2 Replies Last reply
    0
    • A AlekseyB

      In the Qt-library I haven't found methods to work with memory like malloc, realloc and free() of standard library C++.

      Are there any analogues in Qt?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @AlekseyB

      yes, its c++ so there is new and delete ?

      there also classes for use cases, like
      http://doc.qt.io/qt-5/qtcore-ipc-sharedmemory-example.html

      A 1 Reply Last reply
      1
      • A AlekseyB

        In the Qt-library I haven't found methods to work with memory like malloc, realloc and free() of standard library C++.

        Are there any analogues in Qt?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @AlekseyB malloc, realloc and free()are actually C not C++.
        In C++ one usually uses new and delete.
        Since Qt is a C++ framework you use C++ language - so use new/delete. There is no need for Qt to reinvent the wheel.

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

        mrjjM 1 Reply Last reply
        3
        • jsulmJ jsulm

          @AlekseyB malloc, realloc and free()are actually C not C++.
          In C++ one usually uses new and delete.
          Since Qt is a C++ framework you use C++ language - so use new/delete. There is no need for Qt to reinvent the wheel.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          To add to @jsulm
          Do not use malloc with class since the constructor is not called.

          1 Reply Last reply
          4
          • mrjjM mrjj

            @AlekseyB

            yes, its c++ so there is new and delete ?

            there also classes for use cases, like
            http://doc.qt.io/qt-5/qtcore-ipc-sharedmemory-example.html

            A Offline
            A Offline
            AlekseyB
            wrote on last edited by
            #5

            @mrjj

            As I understand operators new and delete are used to individual objects. But I need allocation memory for array of objects. For example, to allocate 1000 elements of type char. Is operators new and delete is applicable for this problem?

            mrjjM E 2 Replies Last reply
            0
            • A AlekseyB

              @mrjj

              As I understand operators new and delete are used to individual objects. But I need allocation memory for array of objects. For example, to allocate 1000 elements of type char. Is operators new and delete is applicable for this problem?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @AlekseyB
              Hi
              yes, they are.
              http://www.cplusplus.com/doc/tutorial/dynamic/

              int * foo;
              foo = new int [5]

              A 1 Reply Last reply
              4
              • A AlekseyB

                @mrjj

                As I understand operators new and delete are used to individual objects. But I need allocation memory for array of objects. For example, to allocate 1000 elements of type char. Is operators new and delete is applicable for this problem?

                E Offline
                E Offline
                Eeli K
                wrote on last edited by
                #7

                @AlekseyB said in How Allocation / Resize / Free memory with QT?:

                As I understand operators new and delete are used to individual objects. But I need allocation memory for array of objects. For example, to allocate 1000 elements of type char. Is operators new and delete is applicable for this problem?

                Usually it's nowadays recommended that manual low-level memory management is avoided in C++ as much as possible. You may want to use QVector instead: "reserve(size) explicitly preallocates memory for size items". Or QByteArray. See http://doc.qt.io/qt-5/containers.html. I don't know your background or real needs, but if you for example come from C and embedded systems background and are now trying desktop applications it's recommended to go higher level than you have used to, leaving []-arrays and char* behind. Ignore me if you already know what you need.

                1 Reply Last reply
                0
                • mrjjM mrjj

                  @AlekseyB
                  Hi
                  yes, they are.
                  http://www.cplusplus.com/doc/tutorial/dynamic/

                  int * foo;
                  foo = new int [5]

                  A Offline
                  A Offline
                  AlekseyB
                  wrote on last edited by
                  #8

                  @mrjj
                  In the above case, you cann't change the size of the allocated memory. The new and delete operators can only allocate and deallocate memory.

                  What's wrong with using malloc, realloc and free?

                  mrjjM 1 Reply Last reply
                  0
                  • A AlekseyB

                    @mrjj
                    In the above case, you cann't change the size of the allocated memory. The new and delete operators can only allocate and deallocate memory.

                    What's wrong with using malloc, realloc and free?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @AlekseyB
                    Hi
                    There is nothing wrong using malloc, realloc and free for NON classes but there are so many better alternatives that
                    there seems to be no reason why to use them.

                    std::vector<char> mylist
                    mylist.reserve(1000);
                    its the same as
                    char mylist[1000];

                    BUt , the vector is so much cooler
                    u can just do mylist.resize(2000);
                    it has a size() to tell how many etc.
                    and they are much easier to send as paramters. the list goes on :)

                    A 1 Reply Last reply
                    2
                    • mrjjM mrjj

                      @AlekseyB
                      Hi
                      There is nothing wrong using malloc, realloc and free for NON classes but there are so many better alternatives that
                      there seems to be no reason why to use them.

                      std::vector<char> mylist
                      mylist.reserve(1000);
                      its the same as
                      char mylist[1000];

                      BUt , the vector is so much cooler
                      u can just do mylist.resize(2000);
                      it has a size() to tell how many etc.
                      and they are much easier to send as paramters. the list goes on :)

                      A Offline
                      A Offline
                      AlekseyB
                      wrote on last edited by AlekseyB
                      #10

                      Thanks for all!

                      1 Reply Last reply
                      0

                      • Login

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