So I have always developed in a 32 bit environment, but now have the opportunity to step into 64 bit development. What I am working on requires a lot of pointer aritmatic, and I am a little confused how primitive data type sizes vary from 32 to a 64bit platform. So what I know is pointers in a 32bit environment are 4 bytes and 8 bytes in a 64bit environment, but is their anything else I need to look out for? And how would you solve void pointer arithmatic in a 64 bit environment? Just divide the size(8) by 2 to get back to 4?

For example this is what I can do in a 32 bit environment:

void CopyData(void* data, int dataTypeCount, int dataTypeSize)
{
   void** dataCopy = new void*[dataTypeCount * dataTypeSize];

   memcpy(dataCopy, data, dataTypeCount * dataTypeSize);

    //Go through the copied data to cast to a known data type
    for(int i = 0; i < dataTypeCount; i++)
    {
       KnownType values;

       //12 is how ever many bytes you want from the unknown data type structure
       //i * (dataTypeSize / sizeof(void*)) will step forward through each element of the void pointer list
       memcpy(&values, dataCopy + (i * (dataTypeSize / sizeof(void*))), 12) 
   }        
}

If you're writing code that depends on an exact data type size (and often on a data type size in general), you're probably doing something wrong.

If your code relies on the size of pointers, you're almost certainly doing it wrong. The whole purpose of pointer arithmetic is that it removes from you the need to know how big an object is, and to let the compiler handle it. Additionally, if you're using a void pointer, you're almost certainly (but not quite always) doing it wrong.

If you want to work with a single byte at a time using a pointer to that data, consider a char pointer. In C++, a char is guaranteed to be a single byte.

And how would you solve void pointer arithmatic in a 64 bit environment?

You cannot conduct arithmetic on a void pointer in C++. It's forbidden. What you're doing above does not look like pointer arithmetic on a void pointer; it appears to be arithmetic on a known pointer type, using a void pointer in some convoluted way that doesn't make a lot of sense.

Edited 3 Years Ago by Moschops

ok, using a char* makes more sense. That was the blunt response I needed to clear things up. /thread

This article has been dead for over six months. Start a new discussion instead.