how calculate values from 1D array?

Apr 3, 2022 at 6:19pm
see these code:
1
2
3
4
5
6
void *BufferMemory;
int BufferSize = ConsoleWidth * ConsoleHeight * sizeof(unsigned int);

    if(BufferMemory) VirtualFree(BufferMemory, 0,MEM_RELEASE);
    BufferMemory = VirtualAlloc(0, BufferSize,MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    unsigned int *PixelColors = (unsigned int) BufferMemory;

now imagine that we have only have 'PixelColors'(like try use it on a function)... my question is:
can i calculate for get the 'ConsoleWidth' and 'ConsoleHeight'?
Apr 3, 2022 at 6:30pm
In C and C++, arrays don't know their own length, thus you can't determine BufferSize from PixelColors alone.

Even if you had BufferSize, you could not unambiguously compute ConsoleWidth and ConsoleHeight, since BufferSize is their product. If BufferSize == 48, how could you know which of these are the real dimensions?
ConsoleWidth = 2, ConsoleHeight = 6
ConsoleWidth = 3, ConsoleHeight = 4
ConsoleWidth = 4, ConsoleHeight = 3
ConsoleWidth = 6, ConsoleHeight = 2
The only way you could avoid ambiguity would be if BufferSize / sizeof(unsigned int) was the square of a prime:
BufferSize = 24964
BufferSize / 4 = 6241
sqrt(BufferSize / 4) = 79
ConsoleWidth = 79
ConsoleHeight = 79
Last edited on Apr 3, 2022 at 6:48pm
Apr 3, 2022 at 6:38pm
understand...
thank you so much for all... now i need create a new topic for avoid these problem ;)
Last edited on Apr 3, 2022 at 6:38pm
Topic archived. No new replies allowed.