I was trying to read an integer stored in a file as 4 bytes.
To read it, I implemented the function readInt()
which will read the next four bytes of a char array and transform it to an integer.
//The whole thing is in a class
char *content;
uint32_t index = 0;
uint32_t nextInt() {
const uint32_t i = index;
index += 4;
return ((u_char) content[i]) << 24 | ((u_char) content[i + 1]) << 16 | ((u_char) content[i + 2]) << 8 |
((u_char) content[i + 3]);
}
I used this function lots of time without any issues until today: I found a bug: When I use the function like below, it works perfectly like I want to:
uint32_t count = nextInt();
geometry.setup(count, nextInt());
But if I change the code a little bit to make it looking like below, it just stop working.
geometry.setup(nextInt(), nextInt());
I get an out of memory
exception. This exception is caused because I create an array but I get a wrong argument.
Things will be better if I show you how geometry.setup()
works:
void Geometry::setup(uint32_t newVertexCount, uint32_t newIndexCount) {
std::cout << newVertexCount << std::endl;
std::cout << newIndexCount << std::endl;
vertexCount = newVertexCount;
indexCount = newIndexCount;
vertices = new Vertex[vertexCount];//out-of-memory exception here. vertexCount=1065353216
indices = new uint16_t[indexCount];
}
In the first case, newVertexCount
is 36
and newIndexCount
is 24
(which are the expected results).
But in the second case, the values are 1065353216
and 1052770304
. Why?
Note that count
is not used anywhere else.
Any idea?