Vector has undefined value even though the values have been explicitley defined

StackOverflow https://stackoverflow.com/questions/22340295

  •  13-06-2023
  •  | 
  •  

Domanda

So I have a vector of test elements (teste) in my code so I can check the adding and removing of elements in one of my functions. It is defined as follows:

std::vector<GLuint> teste = {
        0, 1, 2,
        4, 3, 5
};

I then later on have a function print this and the element vector produced by my function

for(size_t i = 0; i < TestBuffer.Elements.size(); ++i){
    std::cout << "!" << i << std::endl;
    std::cout << TestBuffer.Elements[i] << std::endl;
    std::cout << teste[i] << std::endl;
}

It produces the following output

!0
0
0
!1
1
1
!2
2
2
!3
4
4
!4
3
4294967293
!5
5
4

How in the heck is this happening? NOTHING edits teste after it has been defined. Also the value for teste[4] doesn't change (NOTE: I don't think that value is GLuint's max value as a GLuint -1 has a value of 244294967295).

EDIT: I'm not going to include all the code because its over 1000 lines and no one wants to read that. Here are the functions that edit TestBuffer (a BufferData) and here is where teste is declared and used.

Also this is using OpenGL and so a GLuint is an OpenGL unsigned int.

È stato utile?

Soluzione

There appears to be a problem with the lines:

//Update the value of the vertices
for(uint i = VerticesEnd; i < Vertices.size(); ++i){
  VerticesPositions[i] -= ID.VerticesSize;
}

in BufferData::RemoveData. By looking at the rest of the code, I would venture to guess that it should rather be:

//Update the value of the vertices
for(uint i = VerticesEnd; i < VerticesPositions.size(); ++i){
  VerticesPositions[i] -= ID.VerticesSize;
}

Altri suggerimenti

You should post some more code, what is your GlueInt ? And what is Testbuffer.Elements?

If i do it this way it works like it should :

#include <iostream>
#include <vector>

using namespace std;
int main()
{
    std::vector<int> teste = {
        0, 1, 2,
        4, 3, 5
    };
    for (size_t i = 0; i < teste.size(); ++i){
        std::cout << "!" << i << std::endl;
        std::cout << teste[i] << std::endl;
        cin.get();
    }
    return 0;
}

So it must have to to with the rest of your code you didn't post, so if you want an answer post some more code.

You're seeing undefined behavior, probably caused by some other part of your program accessing an array or vector beyond its defined boundaries. It's impossible to diagnose without seeing more of your code. Given that posting your code would be impractical you're stuck finding it on your own.

In Microsoft's Visual Studio you can set a breakpoint whenever a particular memory address is modified. I've found this to be very useful when tracking down this kind of problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top