如何以单字节精度可移植地执行指针运算?
请记住:
char 在所有平台上都不是1个字节sizeof(void) == 1 仅作为GCC的扩展名提供c compiler-construction portability void-pointers pointer-arithmetic
我是qsort()列表中的一些内存指针,以允许稍后在函数中对它们进行bsearch.我的问题是,我是否需要键入将这些值转换为const void*之外的其他值来在C中进行合法比较?我可以做转换并让编译器告诉我,但我感觉这可能是编译器依赖的.
我(尝试)在ObjC项目中编写一些(工作)C++ :-) C++库(Box2D)为我提供了一个b2Fixture类,它具有"用户数据"属性,供编码人员存储与他们相关的内容.
在我的例子中,它只需要存储一个整数.从我在ObjC中的主程序,一个是将整数转换为void*:
headFixture->SetUserData( (void*) 10 );
Run Code Online (Sandbox Code Playgroud)
在程序的C++方面的实用程序方法中,我想将用户数据与给定的整数进行比较(即它们是常数,10 =实地,11 =平台等).
第一次比较使用(void*)拒绝编译.在SO上找到了一种不同的方法,如第二次比较所示,它使用*((intptr_t*)...).那个编译,但它发送EXC_BAD_ACCESS:
bool AbstractContactListener::contactContainsType(JRContact contact, int type){
if (( type == ( (void *) contact.fixtureA->GetUserData() )) ||
( type == *( (intptr_t *) contact.fixtureB->GetUserData() ))
) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我已经没有想法来解决这个问题了.请帮忙:-)谢谢!J.
编辑/解决方案:
bool AbstractContactListener::contactContainsType(JRContact contact, int type){
if (( type == (intptr_t) contact.fixtureA->GetUserData() ) ||
( type == (intptr_t) contact.fixtureB->GetUserData() )
) {
return true;
};
return false;
}
Run Code Online (Sandbox Code Playgroud)
这个对我有用!
如果文件是.c MSVC将编译.
如果它是.cpp它 "cannot convert from 'void *' to 'unsigned char *'"
我使用MEMORY_BASIC_INFORMATION::BaseAddress的windows.h是void*,将其分配给一个char*让我看见你知道实际的地址?
当我把它转换为char*,然后回到void*(在windows函数中使用它)时,它就像错误并丢失数据或其他东西
该怎么办?
host.cpp有:
int main (void)
{
void * th = dlopen("./p1.so", RTLD_LAZY);
void * fu = dlsym(th, "fu");
((void(*)(int, const char*)) fu)(2, "rofl");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并且p1.cpp具有:
#include <iostream>
extern "C" bool fu (float * lol)
{
std::cout << "fuuuuuuuu!!!\n";
return true;
}
Run Code Online (Sandbox Code Playgroud)
(我故意留下错误检查出来)
当执行主机时,"fuuuuuuuu !!!"被正确打印,即使我将带有完全不同功能签名的符号的void指针类型化.
为什么会发生这种情况,这种行为在不同的编译器之间是否一致
可能重复:
C++从函数返回数组
我试图声明一个返回void指针数组的函数.我有以下代码:
void *[] get_functions();
Run Code Online (Sandbox Code Playgroud)
但是我得到了编译错误: expected unqualified-id before '[' token
我正在尝试做什么有效,如果是这样,我的语法错误是什么?
编辑
在回复一些注释时,我试图返回一个函数的数组(现在可能是一个向量),然后我可以随机选择一个并调用它.你会建议什么而不是void *?
编辑2
返回的函数类型将有一个固定的签名(尚未确定),让我们为参数说明签名将是int f(int i,int j)我的get_functions函数返回的样子是什么,或者vector<void*>仍然是适当?
可能重复:
取消引用void指针
我有这样一个函数调用:
void foo(void *context) //function prototype
..
..
..
main()
{
.
.
foo(&(ptr->block)); //where ptr->block is of type integer.
.
.
.
void foo(void *context)
{
Here I try to use the ptr->block but am having problems. I have tried
if((int *)context ==1)
..
..
}
Run Code Online (Sandbox Code Playgroud)
我在函数中将它转换回int以使用它.我在foo()函数中取消引用它是错误的吗?
我试图通过ctypes使用python来控制cuda.在这里,为了说明我的问题,我使用python将指针传递给c函数,这些函数分配cuda内存,将numpy数组复制到cuda mempory,并将cuda内存复制回新的numpy数组.但它似乎没有用,尽管我的基本ctypes设置工作.我认为问题在于从cudaMalloc函数返回到python的内容.
这是python代码
pycu_alloc = dll.alloc_gpu_mem
pycu_alloc.argtypes = [c_size_t]
pycu_alloc.restypes = [c_void_p]
host2gpu = dll.host2gpu
host2gpu.argtypes = [c_void_p, c_void_p, c_size_t]
gpu2host = dll.gpu2host
gpu2host.argtypes = [c_void_p, c_void_p, c_size_t]
a = np.random.randn(1024).astype('float32')
c = np.zeros(1024).astype('float32')
c_a = c_void_p(a.ctypes.data)
c_c = c_void_p(c.ctypes.data)
da = pycu_alloc(1024)
c_da = c_void_p(da)
host2gpu(c_a, c_da, 1024)
gpu2host(c_c, c_da, 1024)
print a
print c
Run Code Online (Sandbox Code Playgroud)
和C:
extern "C" {
float * alloc_gpu_mem( size_t N)
{
float *d;
int size = N *sizeof(float);
int err;
err = cudaMalloc(&d, size);
printf("cuda …Run Code Online (Sandbox Code Playgroud) 她是我的简化代码:
void main(){
void* ptr;
char* args[3];
args[0]="Arg1";
args[1]="Arg2";
args[2]="Arg3";
ptr = &args;
myMethod(ptr);
}
static void myMethod(void* args){
}
Run Code Online (Sandbox Code Playgroud)
我该如何转换void* args成char*[]?在myMethod(void*)?
我遇到了这段代码,并想知道为什么它void *在释放之前已经被转换了?
free((void *)array1[i]);
Run Code Online (Sandbox Code Playgroud)
我free在一些文章中看到过没有使用过的东西,所以我想知道你为什么要void *在释放内存时施展?
以下是他们分配和释放内存的方式:
#include <stdlib.h>
int **array1 = malloc(nrows * sizeof(int *));
for(i = 0; i < nrows; i++)
array1[i] = malloc(ncolumns * sizeof(int));
for(i = 0; i < nrows; i++)
free((void *)array1[i]);
free((void *)array1);
Run Code Online (Sandbox Code Playgroud)
参考这个,一些gcc版本可能会发出警告,但总的来说这是不必要的.
void-pointers ×10
c ×7
c++ ×4
casting ×3
pointers ×2
char ×1
ctypes ×1
cuda ×1
dlsym ×1
free ×1
int ×1
objective-c ×1
portability ×1
python ×1
windows ×1