Skip to main content
gamedev.net
🔒 Locked

Void pointers and arrays

Started by startreky498 Jan 14, 2005 at 4:24 PM 3 replies 900+ views
Original Post
startreky498
startreky498
Hi. I'm trying to typecast a member of an integer array to a void pointer for universal storage (my plan later being to convert the datatype to an image). Here's my code:

void* register()
{
	int x;

	int *pTextures;
	int pCounter = 0;
	
	pTextures = new int[1];

	pTextures[0] = 5;

	return (void*)pTextures[0];
}


This code has no compile errors, however at runtime I get the following error: Unhandled exception at 0x005b0fdc in 1.exe: 0xC0000005: Access violation reading location 0x00000005. And C++ brings up line 438 in iosfwd (which probably doesn't mean much to anyone here). What exactly am I doing wrong? How can I return a void pointer to an array member? Thank you.
Monder
Monder
pTextures[0] is an integer not a pointer, in that case you are casting the integer in pTextures[0] (which is 5) to a void* so when you try to access that pointer you get an access violation (note the location that is given).

What you want to do is return &pTextures[0], the & operator gives the address (i.e. a pointer to) of the first array element. Or you could simply return pTextures.

Oh and as the AP said, void pointers are bad there's probably another (safer and better) way to do what you want.
startreky498
startreky498
Quote:
Original post by Monder
Oh and as the AP said, void pointers are bad there's probably another (safer and better) way to do what you want.


I would have thought so, too. But I've found that I ought to keep the addresses of the values so that I can pass them to another engine without needing to know the datatype.

Thanks a bunch guys.
LessBread
LessBread
Something more to consider - "register" is also a keyword, so it's not a good name for a function. Maybe use RegisterTextures instead.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.