本文介绍了与malloc有一点金发时刻......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需要一些帮助!

我知道这是一个简单的问题,但是它正在破坏我的头脑而我最终只是盯着你的b $ b多年的屏幕...

我有一个多源程序-myProg1.cpp

-myProg2.cpp

-foo.h

所有发生的事情是myProg1将一个int传递给myProg2,后者使用

int使用malloc()为数组动态分配内存


我以前做过这个没有问题,但今天没有!!!!

这里是代码:


/ * myprog1 .cpp * /

#include< stdio.h>

#include" foo.h"


int maxDepth ;


void main(无效)

{

int prenum;

printf(" \ n输入单词的大小\ n");

scanf("%d",& prenum);

maxDepth = prenum + 3;

myfunction();

}


/ * myprog2.cpp * /


#includ e< stdio.h>

#include< stdlib.h>


extern int maxDepth;


int * raw;


void myfunction(void){

int i;

raw =(int)malloc(maxDepth * sizeof(int)); / *错误指向此

行* /

for(i = 0; i< 4; i ++){

raw [i] = i * i;

printf("%d / n",raw [i]);

}

免费(原始) ;

}

/ * foo.h * /

void myfunction(void);


我继续收到以下错误:

f:\ project \ test2 \ encoder.cpp(18):错误C2440:''='':无法转换

从''int''到''int *''

从积分类型转换为指针类型需要

reinterpret_cast,C风格的强制转换或函数式转换

执行cl.exe时出错。

我感谢任何帮助,

Tony

Hi, just need a bit of help!
I know this is a simple question but it''s wrecking my head and I end up
just staring at the screen for ages...
I have a multi-source program -myProg1.cpp
-myProg2.cpp
-foo.h
All that happens is that myProg1 passes an int to myProg2 which uses
the int to dynamically allocate memory for an array using malloc()

I''ve done this before with no problem but not today!!!
Here''s the code:

/*myprog1.cpp*/
#include <stdio.h>
#include "foo.h"

int maxDepth;

void main(void)
{
int prenum;
printf("\nEnter the size of the word\n");
scanf("%d",&prenum);
maxDepth = prenum + 3;
myfunction();
}

/*myprog2.cpp*/

#include <stdio.h>
#include <stdlib.h>

extern int maxDepth;

int * raw;

void myfunction(void) {
int i;
raw = (int)malloc(maxDepth * sizeof(int));/*Error points to this
line*/
for(i=0;i<4;i++){
raw[i] = i*i;
printf("%d/n",raw[i]);
}
free(raw);
}
/*foo.h*/
void myfunction(void);

I keep on getting the following error:
f:\project\test2\encoder.cpp(18) : error C2440: ''='' : cannot convert
from ''int'' to ''int *''
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
I appreciate any help,
Tony

推荐答案





两个问题:

1)你正在将指针转换成一个整数,你不想在

的任何一个演员

所有这里都在​​C中在这种情况下。 raw是一个int *,你为什么要把malloc返回的void *转换成一个int?


2)你显然是在编译C ++。如果你的意思是这样做,那么你应该在comp.lang.c ++中询问
。 < OT>如果是这样的话你应该使用new,

或者如果需要malloc你需要转换为(int *)。 < / OT>


更好的范例是


raw = malloc(maxDepth * sizeof * raw);


你应该检查malloc没有返回NULL。


-David





Two issues:
1) You are casting the pointer to an integer, you don''t want any cast
at
all here in "C" in this case. raw is an int*, why are you casting
the void* returned by malloc to an int?

2) You are apparently compiling as C++. If you mean to be doing that,
you should
ask in comp.lang.c++. <OT> you should probably use new if so,
or if malloc is wanted you need to cast to (int*). </OT>

A better paradigm would be

raw = malloc(maxDepth * sizeof *raw);

And you should check that malloc didn''t return NULL.

-David




[...]


嗯,你是用C ++编写的,而不是C语言,所以从技术上来说,你这个小组的主题是




但是,上面两行在C和C ++中一样无效,所以这里

去...


什么类型是& ; raw"?

你将malloc()的返回转换为什么?

编译器抱怨什么类型的转换?


并且,因为这是clc而不是clc ++ ...


" void main(void)"在C中是无效的。(我不知道C ++。)


不要在C中使用malloc转换返回值,因为这会掩盖

与缺少头文件有关的错误。 (但是,我理解

在C ++中需要演员。)


-

+ ----- -------------------- + -------------------- + -------- --------------------- +

| Kenneth J. Brody | | |

| kenbrody / at\spamcop.net | | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>


[...]

Well, you''re writing in C++, not C, so technically you''re off-topic for
this group.

However, the above two lines are just as invalid in C as in C++, so here
goes...

What type is "raw"?
What are you casting the return of malloc() to?
What type conversion is the compiler complaining about?

And, since this is clc and not clc++...

"void main(void)" is invalid in C. (I don''t know about C++.)

Don''t cast the return value from malloc in C, as this will mask
errors related to missing header files. (However, I understand
that the cast is needed in C++.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


这篇关于与malloc有一点金发时刻......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:47