Domanda

I'm trying to store a couple of ints in memory using void* & then retrieve them but it keeps throwing "pointer of type ‘void *’ used in arithmetic" warning.

void *a = new char[4];
memset(a, 0 , 4);


unsigned short d = 7;
memcpy(a, (void *)&d, 2);

d=8;
memcpy(a+2, (void *)&d, 2); //pointer of type ‘void *’ used in arithmetic 


/*Retrieving*/
unsigned int *data = new unsigned int();
memcpy(data, a, 2);
cout << (unsigned int)(*data);

memcpy(data, a+2, 2);  //pointer of type ‘void *’ used in arithmetic 
cout << (unsigned int)(*data);

The results are as per expectation but I fear that these warnings might turn into errors on some compiler. Is there another way to do this that I'm not aware of?

I know this is perhaps a bad practice in normal scenario but the problem statement requires that unsigned integers be stored and sent in 2 byte packets. Please correct me if I'm wrong but as per my understanding, using a char* instead of a void* would have taken up 3 bytes for 3-digit numbers.

È stato utile?

Soluzione 2

The problem is that the compiler doesn't know what to do with

a+2

This instruction means "Move pointer 'a' forward by 2 * (sizeof-what-is-pointed-to-by-'a')".

If a is void *, the compiler doesn't know the size of the target object (there isn't one!), so it gives an error.

You need to do:

memcpy(data, ((char *)a)+2, 2); 

This way, the compiler knows how to add 2 - it knows the sizeof(char).

Altri suggerimenti

a+2, with a being a pointer, means that the pointer is increased to allow space for two items of the pointer type. V.g., if a was int32 *, a + 2 would mean "a position plus 8 bytes".

Since void * has no type, it can only try to guess what do you mean by a + 2, since it does not know the size of the type being referred.

Please correct me if I'm wrong but as per my understanding, using a char* instead of a void* would have taken up 3 bytes for 3-digit numbers.

Yes, you are wrong, that would be the case if you were transmitting the numbers as chars. 'char*' is just a convenient way of referring to 8-bit values - and since you are receiving pairs of bytes, you could treat the destination memory are char's to do simple math. But it is fairly common for people to use 'char' arrays for network data streams.

I prefer to use something like BYTE or uint8_t to indicate clearly 'I'm working with bytes' as opposed to char or other values.

void* is a pointer to an unknown, more importantly, 0 sized type (void). Because the size is zero, offset math is going to result in zeros, so the compiler tells you it's invalid.

It is possible that your solution could be as simple as to receive the bytes from the network into a byte-based array. An int is 32 bits, 4 bytes. They're not "char" values, but quads of a 4-byte integer.

#include <cstdint>

uint8_t buffer[4];

Once you know you've filled the buffer, you can simply say

uint32_t integer = *(static_cast<uint32*>(buffer));

Whether this is correct will depend on whether the bytes are in network or host order. I'm guessing you'll probably need:

uint32_t integer = ntohl(*(static_cast<uint32*>(buffer)));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top