如何进行这种类型的转换? [英] How can I do this type conversion?

查看:93
本文介绍了如何进行这种类型的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在这里。

  struct process * init_process(char * programName,int startTime,int cpuTime){
struct process * proc;
proc =(malloc(sizeof(struct process)));
if(proc == NULL){
printf(致命错误:内存分配失败。\\\
Terminating.\\\
);
exit(1);
}
proc-> programName = programName;
proc-> cpuTime = cpuTime;
proc-> startTime = startTime;
proc-> next = NULL;
return(proc);
};

编译器说错误:从'void *'到'process *'的无效转换
我尝试使用进程*(malloc(sizeof(struct process)))做类型转换,但编译器说'错误:期望主表达式之前'*'令牌'这一次。 / p>

任何人都可以帮我解决这个问题吗?

解决方案

您应该使用新样式casts:

  proc = static_cast< process *>(malloc(sizeof(struct process))) ; 

但是,如果你真的使用C ++编译器,你应该使用 new / new [] 用于动态分配(和 delete / delete [] 用于重新分配)。

  proc = 

如果要将C代码移植到C ++,并且不想修改当前 malloc()调用,您可以尝试添加:

  #ifdef __cplusplus 
namespace cxx {
class voidptr {
void * p_;
public:
voidptr(void * p = 0):p_(p){}
template< typename T&
operator T *()const {return static_cast< T *>(p_); }
};
voidptr malloc(size_t sz){return :: malloc(sz); }
voidptr calloc(size_t cnt,size_t sz){return :: calloc(cnt,sz); }
voidptr realloc(void * p,size_t newsz){return :: realloc(p,newsz); }
}
#define malloc(x)cxx :: malloc(x)
#define calloc(x,y)cxx :: calloc(x,y)
#define realloc(x,y)cxx :: realloc(x,y)
#endif

适用于C. 适用于C ++。


My code is here.

struct process *init_process (char *programName, int startTime, int cpuTime) {
    struct process *proc;
    proc = (malloc(sizeof(struct process)));
    if (proc == NULL) {
        printf("Fatal error: memory allocation failure.\nTerminating.\n");
        exit(1);
    }
    proc->programName = programName;
    proc->cpuTime = cpuTime;
    proc->startTime = startTime;
    proc->next = NULL;
    return(proc);
};

The compiler says that "error: invalid conversion from ‘void*’ to ‘process*’" in line 3. I try to use process*(malloc(sizeof(struct process))) to do the type conversion but the compiler says that 'error: expected primary-expression before ‘*’ token' this time.

Can anyone help me solve this problem?

解决方案

In C++, you should use the new style casts:

    proc = static_cast<process *>(malloc(sizeof(struct process)));

But, if you really are using a C++ compiler, you should really be using new/new[] for dynamic allocation (and delete/delete[] for deallocation).

    proc = new process;

If you are porting C code to C++, and you don't want to modify the current malloc() calls, you can try adding this:

#ifdef __cplusplus
namespace cxx {
    class voidptr {
        void *p_;
    public:
        voidptr (void *p = 0) : p_(p) {}
        template <typename T>
        operator T * () const { return static_cast<T *>(p_); }
    };
    voidptr malloc (size_t sz) { return ::malloc(sz); }
    voidptr calloc (size_t cnt, size_t sz) { return ::calloc(cnt, sz); }
    voidptr realloc (void *p, size_t newsz) { return ::realloc(p, newsz); }
}
#define malloc(x) cxx::malloc(x)
#define calloc(x,y) cxx::calloc(x,y)
#define realloc(x,y) cxx::realloc(x,y)
#endif

Works for C. Works for C++.

这篇关于如何进行这种类型的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆