question regarding to C#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main()
{
int n = 3;
double *tptr = malloc(n*sizeof(double));
if (tptr != NULL)
free(tptr);
return 0;
}
got such error:
error C2440: 'initializing' : cannot convert from 'void *' to 'double *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
what's wrong with it? Thanks[dearjohn (7-1 15:29, Long long ago)]
[ 传统版 |
sForum ][登录后回复]1楼
I compiled and run it with cc and gcc under unixand there is no problem.[simon (7-1 15:34, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼
I am using VC, seemsI have to convert the type in this program.
double *tptr = (double*) malloc(n*sizeof(double));
after using this statement, there is no errors, faint. Do you know why? Is it because of VC's complier settings?[dearjohn (7-1 15:37, Long long ago)]
[ 传统版 |
sForum ][登录后回复]3楼
是的c++中付值的时候类型必须是安全的。c中就不用。
你用VC default 是c++[hula (7-1 16:45, Long long ago)]
[ 传统版 |
sForum ][登录后回复]4楼
(引用 dearjohn:I am using VC, seemsI have to convert the type in this program.
double *tptr = (double*) malloc(n*sizeof(double));
after usin...)suggest use: double *tptr = new double[n]if you use malloc(), you must tell which kind of type you want to convert.
suggest use new and delete in C++:
double *tptr = new double[n];
...
delete tptr;
[静 (7-1 17:03, Long long ago)]
[ 传统版 |
sForum ][登录后回复]5楼
(引用 hula:是的c++中付值的时候类型必须是安全的。c中就不用。 你用VC default 是c++)那么请问VC的default可不可以改成C?谢谢[dearjohn (7-1 19:45, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼
(引用 dearjohn:那么请问VC的default可不可以改成C?谢谢)你用的哪个版本的VC?找找啦,应该有选项的啦。
找不到就手工把 /TP 换成 /TC[hula (7-1 20:07, Long long ago)]
[ 传统版 |
sForum ][登录后回复]7楼
(引用 dearjohn:那么请问VC的default可不可以改成C?谢谢)当然你要知道c++的类型安全是一种进步养成习惯是好事情。[hula (7-1 20:08, Long long ago)] [ 传统版 | sForum ][登录后回复]8楼
(引用 静:suggest use: double *tptr = new double[n]if you use malloc(), you must tell which kind of type you want to convert.
suggest u...)呵呵,malloc比new更有效率new(int n)
{
void *p = malloc(n * sizeof(object));
for i = 0 to n
{
p[i]->constructor();
}
}[hula (7-1 20:15, Long long ago)]
[ 传统版 |
sForum ][登录后回复]9楼
(引用 hula:你用的哪个版本的VC?找找啦,应该有选项的啦。 找不到就手工把 /TP 换成 /TC)我是用VC6.0,原来可以改成default C吗?那就太好了,因为我现在在赶一个project,是用C的,如果像你说得那样的话,C++ default的编译环境可以改成C default的话,那就太好了[dearjohn (7-1 21:32, Long long ago)] [ 传统版 | sForum ][登录后回复]10楼
(引用 hula:是的c++中付值的时候类型必须是安全的。c中就不用。
你用VC default 是c++)这是我在别处看到的 在QA003121 "VC中的debug版和Release版本的程序有什么区别"中我们介绍了Debug和Release版本的主要区别。在MFC中,还大量使用了ASSERT宏,这些宏通常可以来纠正一些错误,如还没有初始化指针就使用等。你所遇到的信息就是ASSERT宏报告的错误。通常你要检查一下是否存在错误。在Release方法下,ASSERT宏不会执行,所以也没有错误信息。不过,MFC中的ASSERT宏有时管得有点宽,如果确认没有错误,也可以不理会它。
果真是这样吗?可惜我的时间了,呜呜呜,我是把书上的程序照抄上去都出错!真得要哭了![dearjohn (7-1 21:48, Long long ago)]
[ 传统版 |
sForum ][登录后回复]11楼
replace double *tptr = malloc(n*sizeof(double));with double *tptr = (double *) malloc(n*sizeof(double));
you need an explicit cast since malloc returns pointers of type void *. otherwise compiler will complain.
[va (7-1 22:38, Long long ago)]
[ 传统版 |
sForum ][登录后回复]12楼
double *tptr = (double *)malloc(n*sizeof(double));[鱽鳓 (7-1 22:52, Long long ago)] [ 传统版 | sForum ][登录后回复]13楼