Accessing array[0] gives memory access error

Pages: 123
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.
Last edited on
What's exact error?
The error is:
Unhandled exception at x00cccb6e in Project.exe: 0xC0000005: Access violation writing location 0x00000000.
You need to show more code, but since this is a C++ program the following is not allowed:
1
2
_dlistmax = 10; //The maximum size of _displaylist
_displaylist[_dlistmax];

When declaring arrays the size must be a compile time constant.
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've changed _dlistmax to a constant, but I have no idea how to initialize it now.

What to initialize a constant you assign a value when you declare the const:
const int my_value = 100;
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);
Have you tried ZeroMemory(_displaylist, _dlistmax);, notice the lack of the ampersand?
Yes, that just gives me an access violation error in memset.asm.
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?
Access violation writing location 0x00000000

It sounds like _displaylist has not been initialized. Check if _displaylist == 0

Either that, or maybe _displaylist[0] == 0
Last edited on
_displaylist does not equal 0. _displaylist[0] gives me an access violation.
Access violation writing location 0x00000000

This usually means it hasn't been initialized. What type is _displaylist?
> 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?
Here's the code. I put it in the constructor after the initialization of the array. I commented out ZeroMemory because it was just causing problems.

EDIT: I changed ZeroMemory back to what it was originally (with the ampersand) and _displaylist did equal zero.

1
2
3
4
5
6
7
8
9
10
_dlistsize = 0;
_displaylist[_dlistmax];
//ZeroMemory(_displaylist[0], _dlistmax);
if(_displaylist == 0)
{
	MessageBoxA(NULL, "_displaylist equals 0", "Info", MB_ICONINFORMATION | MB_OK);
}else
{
	MessageBoxA(NULL, "_displaylist does not equal 0", "Info", MB_ICONINFORMATION | MB_OK);
}
Last edited on
_displaylist[_dlistmax];

This does nothing. I'm surprised you don't get an error. This is equivalent to:
arr[10];

Doesn't declare, initialize, or assign anything.
So could you tell me what to do instead?
Are you trying to declare the array?

type _displaylist[_dlistmax]; //Assuming _dlistmax is compile time constant
Pages: 123