views:

167

answers:

3
Error    1    error C2036: 'const void *' : unknown size    file.cpp     111

I don't follow. GCC never complains about void * pointer arithmetic, even on -ansi -pedantic -Wall. What's the problem?

Here's the code-

struct MyStruct {

    const void *buf;    // Pointer to buffer  
    const void *bufpos; // Pointer to current position in buffer

};

...

size_t    someSize_t, anotherSize_t;
MyStruct *myStruct = (MyStruct *) userdata;
...
  if ( (myStruct->bufpos + someSize_t) > 
       (myStruct->buf + anotherSize_t) ) { // Error on this line
     ...
+9  A: 

You can't do pointer math on a void * pointer. Cast oData->bufpos and oData->anotherConstVoidPtr to something the compiler knows how to deal with. Since you seem to be looking for sizes, which are presumably in bytes, casting to char * should work:

if (((char *)oData->bufpos + someSize_t) ...
Carl Norum
Or given that `bufpos` points to a series of bytes, change it to type `char*`. `void*` is best used for opaque object handles where you don't even want to allow byte-wise access.
Potatoswatter
+4  A: 

On the line:

if ( oData->bufpos ...

The type of bufpos is still void*. The compiler doesn't know what that pointer points to, so it gives you that error.

For pointer arithmetic, void* has no size, so taking an offset, or doing other pointer arithmetic doesn't make sense. Cast it to char* if you want to offset it by a number of bytes:

if(((char*)oData->bufpos) + offset ...

Edited after more code/context was given

If you can help it, try to use char* instead of void*. People in C-land will know what you are talking about, because chars are bytes, and you'll save yourself the headache of casting.

Merlyn Morgan-Graham
Why does Visual C++ break on this when rational C compilers don't? Why break the standard?
A Student at a University
@A Student at a University: I can only speculate, however I think your questions are loaded. It may not be standard (I haven't looked at the standard, but it seems accepted knowledge that you can't take an offset from a void pointer, because the size of the target is unknown). If it isn't the standard, than are other compilers actually that rational? If it is the standard, and VC++ doesn't conform, I can still see why they would make that mistake, because void pointers aren't generally used that way by a good portion of C users - char pointers are.
Merlyn Morgan-Graham
@A Student at a University: Besides that, are you compiling in MSVC in C++ or C? The extension of the file makes a huge difference in how the compiler works. For example, C++ is not a superset of C, though it tries to conform where it can. And MSVC does not implement C99 standards in its C compiler, only an earlier standard of C.
Merlyn Morgan-Graham
+1  A: 

$3.9.1/9- The void type has an empty set of values. The void type is an incomplete type that cannot be completed. It is used as the return type for functions that do not return a value. Any expression can be explicitly converted to type cv void (5.4). An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.18), as a second or third operand of ?: (5.16), as the operand of typeid, or as the expression in a return statement (6.6.3) for a function with the return type void.

I suspect an improper use of 'void' beyond what is allowed by the Standard.

Chubsdad
Please explain what the standard means in relation to this code, and please explain why this works on gcc but not MSVC.
A Student at a University
With reference to the OP, it means that void * cannot participate in arithmetic expressions
Chubsdad
Why does it work everywhere else but not on MSVC?
A Student at a University