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. Uninitialized Variable and qDebug Statement

Uninitialized Variable and qDebug Statement

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 211 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.
  • J Offline
    J Offline
    joejack77
    wrote on 15 Aug 2024, 13:31 last edited by
    #1

    So, I've got this bit of code and i would love if someone could explain me what is happening.

    Here is the code:

    void Buffer::alignChunk(Chunk &chunk, BDSH lastType)
    {
        VkDeviceSize totalChunkSizeNoAlignment;
        qDebug() << "totalChunkSizeNoAlignment" << totalChunkSizeNoAlignment;
        for(auto& dataPiece : chunk.chunkPiecesSize)
            totalChunkSizeNoAlignment += dataPiece;
    
        // qDebug() << "comment";
    

    Here's the part i dont get:
    When that last qDebug line is commented out (like it is now), totalChunkSizeNoAlignment starts off as some big random number. But if I uncomment it:

    qDebug() << "comment";
    

    totalChunkSizeNoAlignment is zero when it prints.
    But this first qdebug:

    qDebug() << "totalChunkSizeNoAlignment" << totalChunkSizeNoAlignment;
    

    doesn't change anything when its commented, and uncommented.

    I'd like to know what's happening behind the scenes, and why the first debug statement doesn't change the initialized number at all, but the second one does. Thank you.

    J 1 Reply Last reply 15 Aug 2024, 13:53
    0
    • J joejack77
      15 Aug 2024, 13:31

      So, I've got this bit of code and i would love if someone could explain me what is happening.

      Here is the code:

      void Buffer::alignChunk(Chunk &chunk, BDSH lastType)
      {
          VkDeviceSize totalChunkSizeNoAlignment;
          qDebug() << "totalChunkSizeNoAlignment" << totalChunkSizeNoAlignment;
          for(auto& dataPiece : chunk.chunkPiecesSize)
              totalChunkSizeNoAlignment += dataPiece;
      
          // qDebug() << "comment";
      

      Here's the part i dont get:
      When that last qDebug line is commented out (like it is now), totalChunkSizeNoAlignment starts off as some big random number. But if I uncomment it:

      qDebug() << "comment";
      

      totalChunkSizeNoAlignment is zero when it prints.
      But this first qdebug:

      qDebug() << "totalChunkSizeNoAlignment" << totalChunkSizeNoAlignment;
      

      doesn't change anything when its commented, and uncommented.

      I'd like to know what's happening behind the scenes, and why the first debug statement doesn't change the initialized number at all, but the second one does. Thank you.

      J Online
      J Online
      JonB
      wrote on 15 Aug 2024, 13:53 last edited by JonB
      #2

      @joejack77
      qDebug() statements, later on or anywhere, in themselves make no difference.

      VkDeviceSize totalChunkSizeNoAlignment;

      If this (the VkDeviceSize constructor) does not initialize totalChunkSizeNoAlignment to any particular value then the value of that variable (until you assign to it) is undefined. That means it could have any value. In practice that is probably a "random" value in an area of memory on the stack. Putting in some seemingly unrelated piece of code, before or after, can change what random value is picked up --- because it can alter where the code is and what happens to be in a random place on the stack.

      This sounds like the most likely answer to your question. You might try putting all sorts of quite different, random statements, instead of the qDebug(), all over the function and see whether some of them also alter the output "by coincidence".

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 15 Aug 2024, 14:01 last edited by
        #3

        From the C++ specification standpoint what is happening is undefined behavior. You're using an uninitialized variable so it can have any value and that value can depend on anything. When UB is invoked you can't reason about what's happening in the language boundaries.

        As for what's happening underneath, leaving the language rules aside - any number of things can happen. Code generation and optimizer work on whole chunks of code, not just individual lines or variables, so unrelated (from the language point of view) instructions can have a dependency in the generated assembly. Instructions are often reordered, registers get assigned differently from the available pool, so a random change like that just reorders the resulting assembly in a way that lands totalChunkSizeNoAlignment in a different place, that happens to be 0.

        1 Reply Last reply
        3
        • J Offline
          J Offline
          joejack77
          wrote on 16 Aug 2024, 14:19 last edited by
          #4

          Thanks, appreciate it. Makes sense now

          1 Reply Last reply
          0

          1/4

          15 Aug 2024, 13:31

          • Login

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