I'm writing a simple class for rendering 2D graphics. The relevant code in the initialization for the class is:
1 2 3 4
_dlistsize = 0; //The number of occupied spaces in _displaylist
_dlistmax = 10; //The maximum size of _displaylist
_displaylist[_dlistmax];
ZeroMemory(&_displaylist, _dlistmax);
When I try to run it, I get a memory access error on this line: _displaylist[_dlistsize] = graphic;
I've checked the value of _dlistsize and it is still 0 when the line runs. I get no errors until I run it.
The problem is probably somewhere else, but you've got weird usage of ZeroMemory.
The & is irrelevant in this case, and because ZeroMemory operates on bytes(size 1) I think this should be something like:
ZeroMemory(_displaylist, _dlistmax * sizeof(/*whatever the type of items in _displaylist is*/));
I've changed _dlistmax to a constant, but I have no idea how to initialize it now.
Also, Krzysztof, ZeroMemory was giving me an error before, so I put the & there and it fixed it. I just thought about it though and changed it to _displaylist[0].
I just tried to run it again and ZeroMemory is giving me the same error now. I changed it from &_displaylist to _displaylist[0]. ZeroMemory(_displaylist[0], _dlistmax);
Most likely, there's some problem accessing _displaylist. Would making it an array of pointers cause problems, by any chance? The declaration for it is: Graphic** _displaylist;
I want to be able to move things around quickly in the array by just changing what the pointers point to. Have I broken something by doing that?
> When declaring arrays the size must be a compile time constant. _displaylist[_dlistmax]; is not a declaration... (probably that statement has no effect)
_displaylist does not equal 0. _displaylist[0] gives me an access violation.
Indeed, you said it gives you Access violation writing location 0x00000000, which suggests that it is equal to zero. So, how are you making sure that it is not?