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. Weird array properties
Forum Update on Monday, May 27th 2025

Weird array properties

Scheduled Pinned Locked Moved Unsolved C++ Gurus
3 Posts 3 Posters 356 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.
  • I Offline
    I Offline
    ItsRhysNotRhys
    wrote on 12 Mar 2024, 21:31 last edited by
    #1

    I am currently trying to manipulate an array being used as a queue.

    int Elevator::move(){
        if (queueSize > 0) {
            //qInfo("Trying to change to floor %d", queue[0]);
    
            setFloor(queue[0]);
    
            //qInfo("Current floor: %d", getFloor());
    
            printQueue();
    
            for(int i = 0; i < 20-1; ++i) {
                queue[i] = queue[i + 1];
            }
    
            printQueue();
    
            //qInfo("Next floor: %d", queue[0]);
    
            queueSize--;
    
            return queueSize;
        } else {
            qInfo("No floors in queue.");
            return -1;
        }
    }
    

    I filled the queue with 3 floors. After moving to the first floor, the array gets shifted perfectly fine. The issue is when the queue gets accessed the next time, the index 0 of the array will always get changed back to the first floor request.

    Here is the console output (The first print of each cycle is the queue before changing floors, the second one is the queue after changing)

    Printing Queue:
    3
    4
    5
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Printing Queue:
    4
    5
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    2 left in queue
    inside moveElevator1
    Printing Queue:
    3
    5
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Printing Queue:
    5
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1 left in queue
    inside moveElevator1
    Printing Queue:
    3
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Printing Queue:
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0 left in queue
    

    The array I'm using is simply "int queue[20] ;"

    Please let me know if you can see a problem here. I can provide more class declarations if needed.

    J 1 Reply Last reply 14 Mar 2024, 18:50
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Mar 2024, 18:25 last edited by
      #2

      Hi,

      The printQueue code would be nice to have as well.

      You should also check for any code setting values in your queue.

      Finally, why not use a queue class such as QQueue ?

      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
      0
      • I ItsRhysNotRhys
        12 Mar 2024, 21:31

        I am currently trying to manipulate an array being used as a queue.

        int Elevator::move(){
            if (queueSize > 0) {
                //qInfo("Trying to change to floor %d", queue[0]);
        
                setFloor(queue[0]);
        
                //qInfo("Current floor: %d", getFloor());
        
                printQueue();
        
                for(int i = 0; i < 20-1; ++i) {
                    queue[i] = queue[i + 1];
                }
        
                printQueue();
        
                //qInfo("Next floor: %d", queue[0]);
        
                queueSize--;
        
                return queueSize;
            } else {
                qInfo("No floors in queue.");
                return -1;
            }
        }
        

        I filled the queue with 3 floors. After moving to the first floor, the array gets shifted perfectly fine. The issue is when the queue gets accessed the next time, the index 0 of the array will always get changed back to the first floor request.

        Here is the console output (The first print of each cycle is the queue before changing floors, the second one is the queue after changing)

        Printing Queue:
        3
        4
        5
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        Printing Queue:
        4
        5
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        2 left in queue
        inside moveElevator1
        Printing Queue:
        3
        5
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        Printing Queue:
        5
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        1 left in queue
        inside moveElevator1
        Printing Queue:
        3
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        Printing Queue:
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0 left in queue
        

        The array I'm using is simply "int queue[20] ;"

        Please let me know if you can see a problem here. I can provide more class declarations if needed.

        J Offline
        J Offline
        JonB
        wrote on 14 Mar 2024, 18:50 last edited by JonB
        #3

        @ItsRhysNotRhys
        In order to get from

        Printing Queue:
        4
        5
        0
        

        to

        2 left in queue
        inside moveElevator1
        Printing Queue:
        3
        5
        0
        

        something outside the code you are showing us (maybe in whatever issues two of those message lines) is writing 3 to queue[0]. Either directly, or just possibly through buffer over/underflow writing to another member variable adjacent to int queue[20] .

        1 Reply Last reply
        0

        2/3

        14 Mar 2024, 18:25

        • Login

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