给一个void *变量作为输入(只能指向一个进程或线程),我想首先确定它的类型,然后将其转换为该类型.
我应该如何在C++中做到这一点?我知道这是一个愚蠢的问题,但我以前从未做过C/C++,也不能想到C/C++.
编辑:我需要在Linux和Windows上实现这一点.
在下面的程序中,如果我向void指针添加1,它会向前移动一个字节.但是,正如预期的那样,它分别为指针int和double指针移动4和8个字节.为什么 void指针移动1个字节,就像一个字符指针会吗?
#include<stdio.h>
int main(void)
{
int num=3,*int_ptr=#
double sum=3,*double_ptr=∑
void *void_ptr1=&num,*void_ptr2=∑
printf("%p,%p,%p,%p,%p,%p,%p,%p",void_ptr1,void_ptr1+1,\
void_ptr2,void_ptr2+1,int_ptr,int_ptr+1,double_ptr,double_ptr+1);
}
Run Code Online (Sandbox Code Playgroud) 作为一个实验:
\n\n我有一个带有签名的假设函数:void func(void *),我必须在代码中传递一个整数值:func(42)。它必须使用文字整数来完成。
是否有 \xe2\x80\x9cright\xe2\x80\x9d 方法来传递整数值,例如 \xe2\x80\x9chacked\xe2\x80\x9d 作为指针中的地址(例如地址 0x2A),并以某种方式将其转换回整数(0x2A 示例为 42)。所有这一切都没有意外行为?
\n\n简而言之:能够将指针地址转换为保存地址编号的整数。
\n我目前正在编写一个委托类,用于我的几个程序.我的松散函数代码有效,但我在绑定成员函数的代码中遇到编译器错误.编译器错误内容是
错误:在'('标记之前的预期unqualified-id.
我没有看到为什么会出现这种情况.我的代码,不包括编译得很好的部分,如下所示:
template <typename T> class Delegate;
template <typename R, typename... Args>
class Delegate<R(Args)>
{
typedef void* InstancePtr;
//...
template <typename C, R (C::*classFunction)(Args...)>
static inline R MakeStubFunction(InstancePtr instance, Args... args)
{
// vvv error on this line vvv
return ( static_cast<C*>(instance)->(*classFunction) )(args...);
}
//...
};
Run Code Online (Sandbox Code Playgroud)
任何人都可以指出编译器错误的来源吗?为了能够在将来解决这些问题,"不合格身份"是什么意思?
我正在创建一个新struct SThreadInfo函数:
struct SThreadInfo {
int function;
Exchange* pThis;
};
struct SThreadInfo *threadInfo = new (struct SThreadInfo);
threadInfo->function = 0;
threadInfo->pThis = this;
Run Code Online (Sandbox Code Playgroud)
然后,在同一个函数中,我创建一个新的线程,将struct作为void-pointer传递:
pthread_t ret;
pthread_create(&ret, NULL, Exchange::staticThreadHelper, (void*)threadInfo);
Run Code Online (Sandbox Code Playgroud)
在新线程中,我从void-pointer重建结构:
void* Exchange::staticThreadHelper(void* t)
{
struct SThreadInfo* threadInfo = (struct SThreadInfo*)t;
//....
//....
}
Run Code Online (Sandbox Code Playgroud)
在这个函数的最后我想删除由t和分配的内存threadInfo.到目前为止,它适用threadInfo但不适用t
delete threadInfo;
delete static_cast<struct SThreadInfo*>(t);
Run Code Online (Sandbox Code Playgroud)
当我尝试将void-pointer强制转换回SThreadInfo时,会引发一个SIGABRT.有人能告诉我如何从空指针中正确删除内存吗?
我创建了2个线程并使用线程函数计算PI的值.我试图将线程函数的值返回到main(以计算PI为例),但它没有给我正确的答案.每当我运行它时,线程函数的cout语句给出了正确的答案,但是在取消引用void*运算符时我没有得到相同的结果.这是我的代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sys/types.h>
#include<unistd.h>
#include<string>
#include<math.h>
using namespace std;
class ThreadObj
{
public:
int n;
float flag;
};
void* Func(void* param)
{
float num, den,partialN;
float partialSum = 0;
ThreadObj* obj1 = (ThreadObj*)param;
num = obj1->flag;
partialN = obj1->n;
for ( int i=0; i<partialN; i++)
{
if(num==1)
{
den = 4*i + 1;
}
else
{
den = 4*i+3;
}
partialSum = partialSum + (num/den);
}
cout<<"Answer of Thread's execution is: "<<partialSum<<endl;
//Returning the answer and casting …Run Code Online (Sandbox Code Playgroud) 在C中,是否可以保证任何指针类型都能void *成功往返?
也就是说,类似以下内容保证可以工作:
typedef struct {
...
} A;
A *p = ...;
void *v = p;
A *p2 = v;
// use p2 here
Run Code Online (Sandbox Code Playgroud)
不管是什么类型的A?
我发现使用 C 编译器,下面的代码可以工作,但不能使用 C++ 编译器。我知道强制转换void**为正确的用法,但我不明白为什么即使我使用void*(注释掉)它也会用 C 编译器编译。
#include <stdio.h>
int fn(void **arg)
{
int *pvalue = *(int**)arg;
*pvalue = 200;
return 0;
}
int main()
{
int value = 99;
int *pvalue = &value;
// fn((void *)&pvalue); // works only in C
// error C2664: 'int fn(void **)': cannot convert argument 1 from 'void *' to 'void **'
fn((void **)&pvalue); // correct, works for both C/C++
printf("%d", value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会这样吗?
我想知道 void 指针是如何实现的。我试图用 x86-64 在 Godbolt 上找到它(你可以在这里看到它),但它没有透露任何信息。空指针是如何实现的?
编辑: 这是我使用的代码:
int main() {
int volatile y = 123;
void* volatile x = &y;
}
Run Code Online (Sandbox Code Playgroud)
我想在这里看到的只是它的x样子。我放置了 volatile 以便 gcc 不会将其作为死代码消除。