Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: void pointer reference  (Read 1270 times)

0 Members and 1 Guest are viewing this topic.
void pointer reference
« on: January 13, 2010, 04:41:59 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
So I've got this linked list class I wrote up real quick, but my function for retrieving data from it isn't working entirely.

Code: [Select]
void LinkedList::retrieve(void *buffer, int index)
{
node * current = firstNode;

if (current != 0)
{
for (int i = 0; i < indexCounter; i++)
{
if (i == index)
{
buffer = current->data;
return;
}
else
current = current->next;
}
}

}

And my call to it is...

Code: [Select]
HVSTGFX::CAnimation CSprite_State::getSprite(int index)
{
HVSTGFX::CAnimation * temp = 0;

sprites.retrieve((HVSTGFX::CAnimation*)&temp, index);
return *temp;
}

I've tried a bunch of different things, but temp remains null even after the function call.
Umm...wat.
Logged



i love big weenies and i cannot lie
Re: void pointer reference
« Reply #1 on: January 13, 2010, 09:26:51 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Well, I haven't worked with void* that much and personally I try to avoid using them in my code. Additionally pointers still give me a headache to begin with. I can't give you a definite solution, but somethings to look at.

1) "buffer = current->data", are you sure that 'data' is another pointer and not the data you need in itself. You might need to put & in front of current->data.

2) Are you sure the function returns because it has found the requested index and not because it came to the end of the list. (put in some strings to a debugger or the standard output)

3) Are you absolutely sure the * at index is a CAnimation*. You could have some type cast errors.

4) Maybe your fault is in the calling function. Does temp remain 0 after calling sprites.retrieve or do you get 0 after returning from getSprite.


It would be nice to know if an error or exception is generated or if the program continues running.
Logged
Re: void pointer reference
« Reply #2 on: January 13, 2010, 10:43:35 am »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
void pointers? Why not use templates? =P

void LinkedList::retrieve(void *buffer, int index)
HVSTGFX::CAnimation *temp = 0;
sprites.retrieve((HVSTGFX::CAnimation*)&temp, index);

Anyway, look at this carefully. You're getting the address of the CAnimation pointer (effectively, CAnimation**), and then casting that into a CAnimation* and then casting it into a void*.

But the real problem is you then pass a copy of this, not a reference, to the retrieve function. The copy gets assigned, then dropped off the stack.

If you want to pass a reference to the void* then have the parameter as void*& buffer. You probably won't be able to use void** because you can't then do *buffer = [whatever] because it will not know what *buffer is...

Code: [Select]
void LinkedList::retrieve(void*& buffer, int index)
{
node *current = firstNode;

if (current != 0)
{
for (int i = 0; i < indexCounter; i++)
{
if (i == index)
{
buffer = (void*)(current->data); // I assume here data is a pointer,
// if not, alter as needed.
return;
}
else
current = current->next;
}
}

}

HVSTGFX::CAnimation CSprite_State::getSprite(int index)
{
HVSTGFX::CAnimation *temp = 0;
sprites.retrieve((void*)temp, index);
return *temp;
}
Above code should work.

Alternatively just return the void* and cast it into a CAnimation* instead of using references and the like. Then you're getSprite function could just be one line

return *((CAnimation*)(sprites.Retrieve(index)));

Also calling retrieve a lot is going to be slow, but you're probably aware of that.

Though if you ask me, you really need something there in case of temp == 0...
« Last Edit: January 13, 2010, 04:47:14 pm by TheDarkJay »
Logged
Re: void pointer reference
« Reply #3 on: January 13, 2010, 08:15:47 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Changed it to return the data instead, which seems to be working.  However, I think the memory is being deleted somewhere else in the program after it's returned.  Got some poking around to do.

Quote
void pointers? Why not use templates? =P
Because I know I can do it with templates :P Now void pointers, thats something that'll challenge me and teach me something new.
Logged



i love big weenies and i cannot lie
Re: void pointer reference
« Reply #4 on: January 13, 2010, 09:21:59 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
And invite you to trip over yourself repeatedly ;D
Logged
Re: void pointer reference
« Reply #5 on: January 13, 2010, 10:57:57 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
That's how you learn, is it not?

EDIT: And on that note, my errors are fixed.
« Last Edit: January 13, 2010, 11:36:26 pm by MG-Zero »
Logged



i love big weenies and i cannot lie
Re: void pointer reference
« Reply #6 on: January 14, 2010, 11:16:15 am »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
True, my point was more void* invites you to run a marathon with your shoelaces untied if you aren't careful ^^
« Last Edit: January 14, 2010, 11:21:45 am by TheDarkJay »
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.039 seconds with 51 queries.