Uninitialized Variable and qDebug Statement
-
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.
-
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.
@joejack77
qDebug()
statements, later on or anywhere, in themselves make no difference.VkDeviceSize totalChunkSizeNoAlignment;
If this (the
VkDeviceSize
constructor) does not initializetotalChunkSizeNoAlignment
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". -
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.