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. I am a little curious about delete in Qt
Qt 6.11 is out! See what's new in the release blog

I am a little curious about delete in Qt

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 3.3k 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.
  • H Offline
    H Offline
    henryxuv
    wrote on last edited by
    #1

    I write the code in Qt Creator like this.
    @
    int s;
    delete &s
    @
    When I execute the code,there isn't any error message appear to me, and the program run still normally. But I try this code in MSVC, the program crashed. I don't know why? Can anybody explain this to me ? In my opinion, code like this should be illeagle. Has Qt lib handled the error, or something else?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GrahamL
      wrote on last edited by
      #2

      I believe that the behaviour in this case is 'undefined' which means that the compiler can choose to do whatever it likes

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        This is nothing about Qt, it's only about C++ and the tool chain.

        I expect you tried it in QtCreator, right? with the default SDK, which means with minGW or on linux, it would be gcc.

        Delete just gets an address, what ever happens on that code, is undefined. When you say it crashed on MSVC, debug or release (which is a big difference, as debug builds in MSVC have a lot of checks).

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          bq. In my opinion, code like this should be illeagle [sic].

          Whether or not it's illegal, it's definitely a bad idea. :-)

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on last edited by
            #5

            Interesting code. I've never had the idea to do something crazy like that. Does anybody know what happens here in low-level detail? Aren't stack adresses in a different range than heap adresses such that the delete statement just doesn't do anything because it can't operate on stack? Or do the ranges overlap and the delete statement marks some corresponding memory block with the same adress on the heap as freed and crashes the application due to that?

            1 Reply Last reply
            0
            • H Offline
              H Offline
              henryxuv
              wrote on last edited by
              #6

              The code I wrote is just for me to test. In fact, this code could be very easyly happened in Qt.Such as when your pass a QTableWidgetItem to setItem.
              @
              QTableWidgetItem tableItem;
              tableWidget.setItem(row,column,&tableItem);
              @
              In the document. Qt Helper doesn't forbid this usage with declaration. Indeed , There are no difference between them.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on last edited by
                #7

                The documentation mentions that you should not allocate QObjects on the stack several times. I don't see what else we can do, since syntactically this is legal.

                If a programmer tries to rely on undefined behavior, then he/she is going to get bitten sooner or later.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DerManu
                  wrote on last edited by
                  #8

                  [quote author="henryxuv" date="1346549695"]In fact, this code could be very easyly happened in Qt.Such as when your pass a QTableWidgetItem to setItem[/quote]

                  Passing stack pointers to foreign functions should generally sound some alarm bells.

                  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