Accessing array[0] gives memory access error

Pages: 123
Yes. I have in the header:
Graphic **_displaylist;

And in the other thing (the body-er? :P):
_displaylist[_dlistmax];
Oh.

_displaylist = new Graphic* [_dlistmax];
Hm... I still get an access violation for:
_displaylist[_dlistsize] = graphic;
Because _dlistsize is the capacity of the array, indices go from 0 to capacity-1

EDIT:
Misread that. Where is that line? Posting the surrounding code is useful. Is graphic of type Graphic*?
Last edited on
The whole function is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Renderer::AddGraphic(Graphic *graphic)
{
	if(_dlistsize + 1 < _dlistmax)
	{
		if(graphic->GetPriority() > _highestpriority)
		{
			_highestpriority = graphic->GetPriority();
		}
		_displaylist[_dlistsize] = graphic;
		_dlistsize++;
		Prioritize();
	}else if(MessageBoxA(NULL, "Failed to add graphic to display list. The display list is full. Would you like to continue execution?", "Error", MB_ICONERROR | MB_YESNO) == IDNO)
	{
		exit(EXIT_FAILURE);
	}
}


I've put message boxes immediately before and after that line. It's definitely causing the error.
Last edited on
Is it still the same error message? With the same location of 0? Do you know the value of _dlistsize when the error happens?
The error message is the same and _dlistsize equals 0.
Where have you added the line to properly initialize the array? The issue has to be there. If you could post the relevant functions of this class, that would be great.

EDIT:
Actually just need where the array is declared and where it's initialized.
Last edited on
The relevant main function code:
1
2
3
Renderer* MyRenderer = new Renderer();
Graphic MyGraphic("testimage.bmp", 0);
MyRenderer->AddGraphic(&MyGraphic);


The constructor:
1
2
3
4
5
6
7
8
9
10
11
12
Renderer::Renderer(void)
{
	_graphicssize = 0;
	_highestpriority = 0;
	_dlistsize = 0;
	_displaylist = new Graphic* [_dlistmax];
	ZeroMemory(&_displaylist, _dlistmax);
	_buffersize = 480 * 480;
	_frontbuffer = (Pixel*)malloc(480 * 480 * sizeof(Pixel));
	_backbuffer = (Pixel*)malloc(480 * 480 * sizeof(Pixel));
	InitBuffer();
}


You already have AddGraphic();

EDIT: I already pasted the declaration and initialization in this post, didn't I?.
http://www.cplusplus.com/forum/beginner/89331/2/#msg479856
Last edited on
Can I see the declaration of Graphic? The only other thing that could be wrong is in there. It may be accessing a bad Graphic pointer.
You mean the constructor? I'm using a bitmap class I found online to break down bitmaps into RGB:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Graphic::Graphic(char *filename, int priority)
{
	chdir("./resources");
	bitmap_image myBitmap(filename);
	_x = 0;
	_y = 0;
	_width = myBitmap.width();
	_height = myBitmap.height();
	_priority = priority;
	pixelData = (Pixel*)malloc(sizeof(Pixel));
	for(int y = 0; y >= _height; y++)
	{
		for(int x = 0; x >= _width; x++)
		{
			unsigned char red;
			unsigned char green;
			unsigned char blue;
			myBitmap.get_pixel(x, y, red, green, blue);
			pixelData[x + (y * _width)].SetRed(red);
			pixelData[x + (y * _width)].SetGreen(green);
			pixelData[x + (y * _width)].SetBlue(blue);
		}
	}
}
I'm running out of ideas. Where is _dlistmax initialized at? Are you sure of it's value when the array is created?
It's in the header. Actually, it's probably wrong. I sort of just kept changing things until it stopped giving me an error. (Seems professional, right? XD)
static const int _dlistmax = 10;
_ Learn to debug. Execute step by step, watch the variables, see the exact line where it crashes.
_ Don't abuse dynamic allocation. Your usage of new is not justified, may have leaks too.
_ Don't reinvent the wheel. Use std::vector as an dynamic size array. (¿do you want it dynamic?)

> I already pasted the declaration and iinitialization in this post, didn't I?.
No, you didn't

> ZeroMemory was giving me an error before, so I put the & there and it fixed it
> I changed it from &_displaylist to _displaylist[0].
> I commented out ZeroMemory because it was just causing problems.
RTFM
Which usage of new isn't justified?
I don't need the array to be dynamic at this stage, I'd prefer simplicity for now.
If I didn't post the initialization, then what would that look like?
I did read how ZeroMemory works. The first parameter is a pointer to the beginning of the section of memory to zero and second is its size. Using the ampersand seemed to be working and caused no problems that I noticed. I only had problems with it when I changed it according to the advice of others on the forum, and it's current back to what it was originally.
> Which usage of new isn't justified?
Renderer* MyRenderer = new Renderer();


> I don't need the array to be dynamic at this stage, I'd prefer simplicity for now.
Graphic *_displaylist[_dlistmax]; simple
1
2
3
std::vector<Graphic *>_displaylist;
//Construct as
Renderer::Renderer(): _displaylist(_dlistmax);

(¿are you sure that you want a container of pointers?)


> The first parameter is a pointer to the beginning of the section of memory to zero
ZeroMemory(&_displaylist, _dlistmax); is equivalent to _displaylist = NULL; (plus accessing memory outside that variable)

> and second is its size.
You need to follow Krzysztof Kawa's advice. Note that your items are pointers.
I didn't have new there at first, there was some reason that I added it but I have no idea what it was now. It's changed to Renderer MyRenderer = Renderer(); now.

I am sure that I want a container of pointers. The order of the items in _displaylist determines the render order, so I need to be able to move them around quickly and easily. Changing a pointer is a lot more efficient than moving arrays of RGB data around.

ZeroMemory is changed to ZeroMemory(_displaylist[0], _dlistmax * sizeof(Graphic*)); It gives me an access violation because it's accessing something in _displaylist and, for whatever reason, the program doesn't like that.
Last edited on
It gives me an access violation because it's accessing something in _displaylist and, for whatever reason, the program doesn't like that.


_displaylist[0] is a pointer that's pointing some random place in memory. Writing to that location results in undefined behavior.
I'm just trying to change what the pointers point to. What do I need to do to get it to work?
*Bump*
Pages: 123