Skip to main content
GameDev.net gamedev.net
🔒 Locked

Void Pointers question

Started by VprMatrix89 Apr 27, 2009 at 12:51 PM 16 replies 2.1k views
Original Post
VprMatrix89
VprMatrix89
I tried to make a function that returns a void pointer, and I don't see why the program will not allow it. Why can't it just be type casted later to assume its use? Can this error be fixed? :: error C2036: 'void *' : unknown size And the only reason I do this is to avoid a function for each type.. While also avoiding templates if I can.
Driv3MeFar
Driv3MeFar
Returning a void* is syntactically fine. Post your code.
Sneftel
Sneftel
It can, you're just using it wrong. You're doing pointer arithmetic on it before the cast, rather than after the cast. Post your code.

Also, post your reasons for avoiding templates.
smitty1276
smitty1276
You need to post some code, specifically around the area where the error occurs. (Make sure to use [ source ] tags--no spaces--when you do.)
rip-off
rip-off
Include with the code a brief overview of the high level goal you are trying to achieve.
Sneftel
Sneftel
And muffins.
smitty1276
smitty1276
Quote:
Original post by Sneftel
And muffins.
Absolutely.

VprMatrix89
VprMatrix89
I get the arithmetic now, it didn't know the size of bytes with no cast. Does the void function have to return a void pointer?, I'd much rather do arithmetic within the function. Otherwise I will also be writing to an address the amount to move the pointer.

The 'high level goal' is just a simple macro. It puts like things together while trying to optimize memory(less fragments) among other directives I can give it. It will do this and interface with a global buffer of types..
SiCrane
SiCrane
There's no such thing as a void function. Do you mean a function with a void return type? If so, then you can't return anything from it, not even a void pointer. Of course, you also failed to post code or bring muffins, so it's hard to help you.
iMalc
iMalc
Quote:
Original post by VprMatrix89And the only reason I do this is to avoid a function for each type.. While also avoiding templates if I can.
Avoid templates?! Meaning you're using C++ and yet...
Is that from lack of experience with them, or from executable size paranoia, or because you're trying to make the life of the next programmer to maintain the code hell, or ...?
No code posted yet.

Coming up next on things to avoid ... breathing
VprMatrix89
VprMatrix89
Quote:
Original post by SiCrane
There's no such thing as a void function. Do you mean a function with a void return type? If so, then you can't return anything from it, not even a void pointer. Of course, you also failed to post code or bring muffins, so it's hard to help you.


Yes it is void*. The code is just full or syntax bugs not worth showing. I'm starting to think this would be easier typecasting a char* pointer and using it as basic strider. If I don't I will have to write a cast for each type when I could just get a stride and move it in bytes.
Or templates, which I don't understand why they don't work in a header/source file. Just seems like a distraction.
Zahlman
Zahlman
...

...

... What is the function in question supposed to do?
rip-off
rip-off
Quote:

The 'high level goal' is just a simple macro. It puts like things together while trying to optimize memory(less fragments) among other directives I can give it. It will do this and interface with a global buffer of types..

Higher again. Until we have code or a description of what is (supposed to be) going on there is a limit to the amount of help we can offer.

Quote:

The code is just full or syntax bugs not worth showing.

We don't care. We will probably be able to grasp what you are trying to achieve from it.
Zakwayda
Zakwayda
Quote:
Or templates, which I don't understand why they don't work in a header/source file. Just seems like a distraction.
Are you talking about how (typically) the implementation of a class or function template must reside in the header file (either directly or indirectly)?

If so, to view this as a reason not to use templates is a mistake, IMO. Vast portions of the C++ standard library and almost all of Boost are based heavily on templates, so I think it's pretty safe to say that templates are a little more than 'a distraction' :)

I don't know that you've made it clear yet what you're actually trying to do, but maybe this would be a good opportunity to familiarize yourself with this particular feature of C++.
smitty1276
smitty1276
Hmm... whatever your intention, the answer you're looking for is "you can't do pointer arithmetic using void pointers".

The real question you should be asking is "How should I be solving this bigger problem?" Since you don't want to post source, we can't say for sure, but it likely involves a more generic (template-y) approach.

Quote:
Or templates, which I don't understand why they don't work in a header/source file. Just seems like a distraction.
Source files are compiled.
VprMatrix89
VprMatrix89
Templates did it. Thanks for help.

Topic Locked

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

Sign in to reply to this topic.