使用intptr_t作为通用存储(保存指针和整数值)而不是void*?(如下所示:http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html)
对于我已经读过的内容:
int- > void*- > int往返不保证保持原值; 我猜int- > intptr_t- > int会的void*和intptr_t要求铸件,所以没有在这里得到好处void*表示存储指针时的显式转换次数较少,intptr_t表示存储整数值时转换次数较少intptr_t 需要C99我还应该考虑什么呢?
我正在玩SFINAE并发现我无法解释的行为.
这编译正常:
template<typename Integer,
std::enable_if_t<std::is_integral<Integer>::value>* = nullptr>
void foo(Integer) {}
template<typename Floating,
std::enable_if_t<std::is_floating_point<Floating>::value>* = nullptr>
void foo(Floating) {}
Run Code Online (Sandbox Code Playgroud)
虽然这个(nullptr替换为0):
template<typename Integer,
std::enable_if_t<std::is_integral<Integer>::value>* = 0>
void foo(Integer) {}
template<typename Floating,
std::enable_if_t<std::is_floating_point<Floating>::value>* = 0>
void foo(Floating) {}
Run Code Online (Sandbox Code Playgroud)
prog.cpp: In function ‘int main()’: prog.cpp:13:10: error: no matching function for call to ‘foo(int)’
foo(3);
^ prog.cpp:5:6: note: candidate: template<class Integer, std::enable_if_t<std::is_integral<_Tp>::value>* <anonymous> > void foo(Integer) void foo(Integer) {}
^~~ prog.cpp:5:6: note: template argument deduction/substitution failed: prog.cpp:4:64: …Run Code Online (Sandbox Code Playgroud) 在使用C语言中的Threads时,我正面临警告
"警告:从不同大小的整数转换为指针"
代码如下
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<pthread.h>
void *print(void *id)
{
int a=10;
printf("My thread id is %ld\n",pthread_self());
printf("Thread %d is executing\n",id);
return (void *) 42;
}
int main()
{
pthread_t th[5];
int t;
int i;
int status;
void *ret;
for(i=0;i<5;i++)
{
status=pthread_create(&th[i],NULL,print,(void *)i); //Getting warning at this line
if(status)
{
printf("Error creating threads\n");
exit(0);
}
pthread_join(th[i],&ret);
printf("--->%d\n",(int *)ret);
}
pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释如何将一个整数传递给接收(void*)作为参数的函数吗?
我试图使用双void指针,但我对使用有点困惑.我有一个struct包含void **数组.
struct Thing{
void ** array;
};
struct Thing * c = malloc (sizeof(struct Thing));
c->array = malloc( 10 * sizeof(void *) );
Run Code Online (Sandbox Code Playgroud)
所以如果我想为每个指针分配一个不同的对象并尝试检索该值
// Option 1
*(c->array + index) = (void *) some_object_ptr;
// Option 2
c->array[index] = (void *) some_object_ptr;
Run Code Online (Sandbox Code Playgroud)
然后,我有另一个功能,给(void *) item每个单元格,而不是some_object_ptr.
如果我想检索指向的值some_object_ptr,
我应该这样做
function return type is 'void *' and takes argument 'void *'
// Option 3
return (void**) item
// Option 4 …Run Code Online (Sandbox Code Playgroud) 我最近遇到了严格的别名规则,但是我无法理解如何在void *不违反规则的情况下执行类型惩罚.
我知道这违反了规则:
int x = 0xDEADBEEF;
short *y = (short *)&x;
*y = 42;
int z = x;
Run Code Online (Sandbox Code Playgroud)
而且我知道我可以安全地使用C99中的联合进行类型惩罚:
union{
int x;
short y;
} data;
data.x = 0xDEADBEEF;
data.y = 42;
int z = data.x;
Run Code Online (Sandbox Code Playgroud)
但是如何void *在C99中安全地执行打字?以下是正确的:
int x = 0xDEADBEEF;
void * helper = (void *)&x;
short *y = (short *)helper;
*y = 42;
int z = x;
Run Code Online (Sandbox Code Playgroud)
我怀疑代码仍然会破坏严格的别名规则,因为变量x地址的内存可以由两者修改x并且可以解除引用y.
如果未定义类型 - 惩罚void *,那么void * …
注意:我是一位经验丰富的C++程序员,所以我不需要任何指针基础知识.只是因为我从未与之合作void**并且很难将我的心智模型调整为void*与void**.我希望有人能够以一种好的方式解释这一点,这样我就能更容易地记住语义.
请考虑以下代码:(使用例如VC++ 2005编译)
int main() {
int obj = 42;
void* ptr_to_obj = &obj;
void* addr_of_ptr_to_obj = &ptr_to_obj;
void** ptr_to_ptr_to_obj = &ptr_to_obj;
void* another_addr = ptr_to_ptr_to_obj[0];
// another_addr+1; // not allowed : 'void*' unknown size
ptr_to_ptr_to_obj+1; // allowed
}
Run Code Online (Sandbox Code Playgroud) 我试图理解C和C++之间关于void指针的区别.以下编译用C而不是C++编译(所有编译都用gcc/g ++ -ansi -pedantic -Wall完成):
int* p = malloc(sizeof(int));
Run Code Online (Sandbox Code Playgroud)
因为malloc返回void*,C++不允许分配,int*而C允许.
但是,这里:
void foo(void* vptr)
{
}
int main()
{
int* p = (int*) malloc(sizeof(int));
foo(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C++和C都编译它没有抱怨.为什么?
K&R2说:
任何指向对象的指针都可以转换为类型
void *而不会丢失信息.如果结果转换回原始指针类型,则恢复原始指针.
这很好地总结void*了C中的转换.C++标准规定了什么?
我有一个C++程序:
struct arguments
{
int a, b, c;
arguments(): a(3), b(6), c(9) {}
};
class test_class{
public:
void *member_func(void *args){
arguments vars = (arguments *) (*args); //error: void is not a
//pointer-to-object type
std::cout << "\n" << vars.a << "\t" << vars.b << "\t" << vars.c << "\n";
}
};
Run Code Online (Sandbox Code Playgroud)
在编译时它会抛出一个错误:
error: ‘void*’ is not a pointer-to-object type
Run Code Online (Sandbox Code Playgroud)
有人可以解释我做错了产生这个错误吗?
大家好我想用printf进行调试.但我不知道如何打印"out"变量.
在返回之前,我想打印此值,但其类型为void*.
int
hexstr2raw(char *in, void *out) {
char c;
uint32_t i = 0;
uint8_t *b = (uint8_t*) out;
while ((c = in[i]) != '\0') {
uint8_t v;
if (c >= '0' && c <= '9') {
v = c - '0';
} else if (c >= 'A' && c <= 'F') {
v = 10 + c - 'A';
} else if (c >= 'a' || c <= 'f') {
v = 10 + c - 'a';
} else …Run Code Online (Sandbox Code Playgroud) void-pointers ×10
c ×8
c++ ×4
casting ×2
32bit-64bit ×1
c99 ×1
intptr ×1
nullptr ×1
pointers ×1
printf ×1
templates ×1
type-punning ×1
uint ×1