Dereferencing void* warnings on Xcode
I'm aware of this SO question and this SO question. The element of novelty in this one is in its focus on Xcode, and in its use of square brackets to dereference a pointer to void.
The following program compiles with no warning in Xcode 4.5.2, compiles with a warning on GCC 4.2 and, even though I don't have Visual Studio right now, I remember that it would consider this a compiler error, and MSDN and Internet agree.
#include <stdio.h>
int main(int argc, const char * argv[])
{
int x = 24;
void *xPtr = &x;
int *xPtr2 = (int *)&xPtr[1];
printf("%p %pn", xPtr, xPtr2);
}
If I change the third line of the body of main to:
int *xPtr2 = (int *)(xPtr + 1);
It compiles with no warnings on both GCC and Xcode.
I would like to know how can I turn this silence into warnings or errors, on GDB and especially Xcode/LLVM, including the fact that function main is int but does not explicitly return any value (By the way I think -Wall does the trick on GDB).
that isnt wrong at all...
the compiler doesnt know how big the pointer is ... a void[] ~~ void*
thats why char* used as strings need to be -terminated
you cannot turn on a warning for that as it isnt possible to determine a 'size of memory pointer to by a pointer' at compile time
void *v = nil;
*v[1] = 0 //invalid
void *v = malloc(sizeof(int)*2);
*v[1] = 0 //valid
*note typed inline on SO -- sorry for any non-working code
链接地址: http://www.djcxy.com/p/35382.html上一篇: 在编译angular 5项目时在打字稿中出现随机错误
下一篇: 在Xcode上解除引用void *警告