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?
Forum Updated to NodeBB v4.3 + New Features

How Allocation / Resize / Free memory with QT?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 6.6k Views 1 Watching
  • 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 8 May 2017, 11:12 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?

    M J 2 Replies Last reply 8 May 2017, 11:17
    0
    • A AlekseyB
      8 May 2017, 11:12

      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?

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 8 May 2017, 11:17 last edited by mrjj 5 Aug 2017, 11:18
      #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 8 May 2017, 12:12
      1
      • A AlekseyB
        8 May 2017, 11:12

        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?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 8 May 2017, 11:26 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

        M 1 Reply Last reply 8 May 2017, 11:28
        3
        • J jsulm
          8 May 2017, 11:26

          @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.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 8 May 2017, 11:28 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
          • M mrjj
            8 May 2017, 11:17

            @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 8 May 2017, 12:12 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?

            M E 2 Replies Last reply 8 May 2017, 12:16
            0
            • A AlekseyB
              8 May 2017, 12:12

              @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?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 8 May 2017, 12:16 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 8 May 2017, 12:34
              4
              • A AlekseyB
                8 May 2017, 12:12

                @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 8 May 2017, 12:34 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
                • M mrjj
                  8 May 2017, 12:16

                  @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 8 May 2017, 12:34 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?

                  M 1 Reply Last reply 8 May 2017, 12:41
                  0
                  • A AlekseyB
                    8 May 2017, 12:34

                    @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?

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 8 May 2017, 12:41 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 8 May 2017, 12:43
                    2
                    • M mrjj
                      8 May 2017, 12:41

                      @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 8 May 2017, 12:43 last edited by AlekseyB 5 Aug 2017, 12:43
                      #10

                      Thanks for all!

                      1 Reply Last reply
                      0

                      1/10

                      8 May 2017, 11:12

                      • Login

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