Jump to content
  • Advertisement
Sign in to follow this  
silvia_steven_2000

char* versus void* : a simple question

This topic is 4792 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a TGA image loader. I have a pointer for the image data defined as: void* m_imageData; let us assume that memory is allocated and data is loaded into memory. now I need to modify the data in memory by looping through the stored bytes. I can do something like : m_imageData = some value only if m_imageData is defined as char* m_imageData but if I leave it as void* m_imageData I get an error message : something like "can not determine size of void*" I think this problem has a simple solution that I can not figure it out right now. by the way, what do we achieve if we define it as void* why do not we define it as char* from the beginning before going one step further into pointer arithmetic. thanks allot

Share this post


Link to post
Share on other sites
Advertisement
Create a temporary pointer of type char* and have it point
to your memory region. Using that you should be able to index
the memory as bytes.

TT

Share this post


Link to post
Share on other sites
When you use the array operator "[]", you are stepping n bytes from the start of the allocated memory block.

n is determined by the size of each element multiplied by the number of elements.

a void pointer points to a block of memory of unknown size, and so cannot be used in this manner, unless it is cast to a known data-type first.

Share this post


Link to post
Share on other sites
*(m_imageData+i) is of type void, so no (you cannot assign to void); also, m_imageData+i will not work since you are adding to a void*, which doesn't work.

Share this post


Link to post
Share on other sites
I thought about using a temporary pointer of type char* to index the memory block as bytes. I did not try it yet but I think it would work
so the question that comes after that is : what do we achieve when we declare the pointer as void* ? why do not we declare it char* in the first place ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
void* is a construct I've seen often in C code and is useful to obtaining a pointer to a memory location where you are not sure what lives at that memory location. An example would be an implementation of a generic link list where the data could be any size. There are much better ways to accomplish this (in C++) but in general, the void* is used for times when you don't know or care about what the data the pointer is pointint to.

Share this post


Link to post
Share on other sites
Quote:
Original post by Episode6
would't looking it through like this help

*(m_imageData+i) = some value;
No, that is exactly what [] does internally anyway. void* is basically a way of having data that is of anonymous type. You can't see how to access it, only certain functions know how to access it. All they do is cast it to the type it should be. That's all you need to do - cast it to char *.

Share this post


Link to post
Share on other sites
Just to clarify a bit on the other posts (though they are all spot-on correct):

<code>void*</code> is a pointer to memory where the type is not known; it has nothing to do with size as earlier posts eluded (only that size is a property of type...).

<code>void*</code>'s are useful for storing addresses and that's it. They are not invocable without type-casting them. In many situations, an address needs to be stored but the code that is storing the address won't actually be using it (containers, like the linked-list example above, or blind-data passed into callbacks, for example). The storing code won't ever invoke it, and isn't involved in the eventual type-casting, so it's simplest to simply store a <code>void*</code>.

In general, I would recommend storing data with the strongest type guarantee that can be determined at compile time. This would be one of the great benefits of templates (and generic programming generally) is the ability to parameterize on type, thus eliminating a lot of scenarios where one would use <code>void*</code>'s in straight C.

So, in the case of an image reader, if you're going to be trolling through it byte-by-byte, then store your pointer as an <code>unsigned char*</code>. Why <code>unsigned</code>? Because some compilers, in particular VS.NET, assume <code>char</code> is signed (-128 to 127) by default, while most image formats deal with strictly unsigned bytes (0-255).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!