无效转换从void *到int ** [英] invalid conversion from void* to int**

查看:109
本文介绍了无效转换从void *到int **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用这个alloc_mem函数:


- - - - - - - - - - - - - - - - - - - - - - - -

void * alloc_mem(size_t num_elems,size_t elem_size,

char * filename,int line,

size_t * total_mem)

{

void * mem;

size_t size = num_elems * elem_size;

size + =(sizeof(size_t)< = elem_size)? elem_size

:sizeof(size_t);

mem = malloc(size);


if(!mem)

{

fprintf(stderr,"%s:line%d,malloc(%lu)failed.\ n,

filename,line ,(unsigned long)大小);

退出(EXIT_FAILURE);

}


/ *为此指针分配的内存* /

memcpy(((char *)mem)+ num_elems * elem_size,

& size,sizeof size);

* total_mem + =大小; / *更新分配的总内存,直到现在* /


返回内存;


}

- - - - - - - - - - - - - - - - - - - - - - - -


但我随后声明了一些数组:


double ** two_D_double;

int ** two_D_int;

double * one_D_double;

int * one_D_int;

等等...


MS visual studio 2005 + gcc并没有抱怨。但是使用g ++我得到了这样一个

错误:


无效转换从void *到int ** (双**相同)


我被告知我应该在这里询问g ++编译器,尽管代码

实际上是在C-中实现的程序。那么什么是语言

的区别 - 为什么我不能在C ++中进行从void *隐式转换为int **"

? br $>
最好的问候/ Med venlig hilsen

Martin J?rgensen


-

--- -------------------------------------------------- ----------------------

Martin J?rgensen的家 - http://www.martinjoergensen.dk

推荐答案

Martin J? ?rgensen写道:
Martin J??rgensen wrote:


我正在使用这个alloc_mem函数:

- - - - - - - - - - - - - - - - - - - - - - - -

void * alloc_mem(size_t num_elems,size_t elem_size,
char * filename,int line, size_t * total_mem)
{
void * mem;
size_t size = num_elems * elem_size;
size + =(sizeof(size_t)< = elem_size)? elem_size
:sizeof(size_t);
mem = malloc(size);

if(!mem)
{
fprintf(stderr," %s:行%d,malloc(%lu)failed.\ n,
文件名,行,(无符号长)大小);
退出(EXIT_FAILURE);
} memcpy(((char *)mem)+ num_elems * elem_size,
& size,sizeof size);
* total_mem + = size; / *更新分配的总内存,直到现在* /

返回内存;

}
- - - - - - - - - - - - - - - - - - - - - - - -
然后我宣布了一些数组:

double ** two_D_double;
int ** two_D_int;
double * one_D_double;
int * one_D_int;
等等......

MS visual studio 2005 + gcc并没有抱怨。但是使用g ++我会收到如下错误:

无效转换从void *到int ** (同样的双**)

我被告知我应该在这里询问g ++编译器,尽管代码实际上是在C程序中实现的。


不要在C程序中使用g ++。 g ++是C ++编译器。

那么语言差异是什么 -


你真的不认为C和C ++是完全一样的,你呢?

为什么我不能做从void *到int **的隐式转换
在C ++中?
Hi,

I''m using this alloc_mem-function:

- - - - - - - - - - - - - - - - - - - - - - - -

void *alloc_mem (size_t num_elems, size_t elem_size,
char *filename, int line,
size_t *total_mem)
{
void *mem;
size_t size = num_elems*elem_size;
size += (sizeof (size_t) <= elem_size) ? elem_size
: sizeof (size_t);
mem = malloc(size);

if (!mem)
{
fprintf(stderr, "%s: line %d, malloc(%lu) failed.\n",
filename, line, (unsigned long) size);
exit(EXIT_FAILURE);
}

/* save memory allocated for this pointer */
memcpy(((char *)mem)+num_elems*elem_size,
&size, sizeof size);
*total_mem += size; /* update total memory allocated untill now */

return mem;

}
- - - - - - - - - - - - - - - - - - - - - - - -

But I then declared some arrays like:

double **two_D_double;
int **two_D_int;
double *one_D_double;
int *one_D_int;
etc. etc...

MS visual studio 2005 + gcc doesn''t complain. But with g++ I get such an
error as:

"invalid conversion from void* to int**" (the same for double **)

I was told I should ask about the g++ compiler here, although the code
is actually implemented in a C-program.
Don''t use g++ for C programs. g++ is the C++ compiler.
So what''s the language difference -
You didn''t really think C and C++ were exactly the same, did you?
why can''t I do an "implicit conversion
from void* to int**" in C++ ?




因为这就是语言的定义方式。从void *到任何

其他对象指针的转换总是需要演员。



Because that''s how the language is defined. Conversions from void* to any
other object pointer always need a cast.


Rolf Magnus写道:
Rolf Magnus wrote:
Martin J ?? rgensen写道:
-snip-

不要将g ++用于C程序。 g ++是C ++编译器。
Martin J??rgensen wrote: -snip-
Don''t use g++ for C programs. g++ is the C++ compiler.




这不是问题。



That wasn''t the question.

那么语言差异是什么 -
So what''s the language difference -



你真的不认为C和C ++是完全一样的,是吗?


You didn''t really think C and C++ were exactly the same, did you?



这是一个荒谬的评论。



That''s a ridiculous comment.

为什么我不能做一个隐含的转换来自虚空* to int **在C ++中?
why can''t I do an "implicit conversion
from void* to int**" in C++ ?



因为这就是语言的定义方式。从void *到任何
其他对象指针的转换总是需要强制转换。


Because that''s how the language is defined. Conversions from void* to any
other object pointer always need a cast.




所以void *在C ++中不是任何通用指针类型。 />
祝你好运/ Med venlig hilsen

Martin J ?? rgensen


-

--- -------------------------------------------------- ----------------------

Martin J ?? rgensen的家 - http://www.martinjoergensen.dk


Martin Jorgensen写道:
Martin Jorgensen wrote:
所以void *在C ++中不是任何泛型指针类型。
So void* isn''t any generic pointer type in C++.




想到void *的最好方法是a可以安全地存储任何

指针'的值的东西。它不是真正的指针,因为它没有指向

类型,所以你不能取消引用它。


-

Phlip
http://c2.com / cgi / wiki?ZeekLand < - 不是博客!!!



The best way to think of void* is "a thing that can safely store any
pointer''s value". It''s not really a pointer because it doesn''t point to a
type, so you can''t dereference it.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


这篇关于无效转换从void *到int **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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