帮助malloc() [英] Help with malloc()

查看:73
本文介绍了帮助malloc()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个函数,我在其中读取一个整数值,并且

动态创建一个double类型和大小的数组整数值:


double compute(int steps,int typeopt)

{

double * price_array;


price_array =(double *)malloc(sizeof(int)* steps);


...


}


到目前为止很好。


现在在我的main方法中,我调用了这个函数两次:

value = compute(steps,0);

printf(" \ n%5.5lf",value);


value = compute(steps,1);

printf(" \ n%5.5lf",value);

我第一次得到正确答案调用,但是当第二次调用该函数时,我得到

跟随错误(即

typeopt = 1):0x00411816处的未处理异常:0xC0000005:Access

违规写入位置0x00000000。


然后我回到计算方法并在方法结束时放一个免费的(price_array);

语句。


然后我收到以下消息:


HEAP:堆积在00355870处修改为0035606C过去请求的大小

7f4 Windows触发了一个断点。这可能是由于堆损坏了,并且表明它已加载的任何DLL中的错误。

输出窗口可能包含更多诊断信息。


我真的很感激任何帮助。


提前致谢,

Schiz

Hi,

I have a function in which I am reading in an integer value, and
dynamically creating an array of type double and size of the integer value:

double compute(int steps, int typeopt)
{
double *price_array;

price_array = (double *) malloc(sizeof(int) * steps);

...

}

So far so good.

Now in my main method, I invoke this function twice:

value = compute(steps, 0);
printf("\n%5.5lf", value);

value = compute(steps, 1);
printf("\n%5.5lf", value);
I get a correct answer for the first time it is invoked, but I get the
following error when the function is invoked the second time (i.e
typeopt = 1): Unhandled exception at 0x00411816: 0xC0000005: Access
violation writing location 0x00000000.

I then go back to the compute method and put a free(price_array);
statement at the end of the method.

I then get the following message:

HEAP: Heap block at 00355870 modified at 0035606C past requested size of
7f4 Windows has triggered a breakpoint. This may be due to a corruption
of the heap, and indicates a bug in or any of the DLLs it has loaded.
The output window may have more diagnostic information.

I would really appreciate any help at all.

Thanks in advance,
Schiz

推荐答案

Schizoid Man< sc *** @ sf.comwrites:
Schizoid Man <sc***@sf.comwrites:

>我有一个函数,我在其中读取一个整数值,并动态创建一个double类型和整数值大小的数组:
>I have a function in which I am reading in an integer value, and
dynamically creating an array of type double and size of the integer value:


> double compute(int steps,int typeopt)
{* / double * price_array;

price_array =(double *)malloc(sizeof(int)*脚步);
>double compute(int steps, int typeopt)
{
double *price_array;

price_array = (double *) malloc(sizeof(int) * steps);



^^^^^^^^^^^

可能:( sizeof * price_array)





-

Chris。

^^^^^^^^^^^
Possibly: (sizeof *price_array)

?

--
Chris.


Schizoid Man(ea * *********@geraldo.cc.utexas.edu)说:


|

|

|我有一个函数,我在其中读取整数值,并且

|动态创建一个类型为double / size
|的数组整数值:

|

| double compute(int steps,int typeopt)

| {

| double * price_array;

|

| price_array =(double *)malloc(sizeof(int)* steps);

|

| ...

|

| }

|

|到目前为止一直很好。

|

|现在在我的主方法中,我调用了这个函数两次:

|

| value = compute(steps,0);

| printf(" \ n%5.5lf",value);

|

| value = compute(steps,1);

| printf(" \ n%5.5lf",value);


< snipperectomy>


你还没提供足够的信息,以获得明确的答案。

从你提供的,我建议:


#include< stdio.h>

#include< stdlib.h>

double compute(unsigned,int);


double * price_array;

price_array = malloc(步骤*(sizeof(double)));


注意添加stdlib.h标头并丢弃cast。我的原型和

更改了你调用malloc()的语句使得

(可能是不正确的)关于你打算如何使用这些步骤的假设

变量;但在这一点上负值似乎不合适。


-

Morris Dovey

DeSoto Solar

美国爱荷华州德索托
http://www.iedu.com/DeSoto
Schizoid Man (in ea**********@geraldo.cc.utexas.edu) said:

| Hi,
|
| I have a function in which I am reading in an integer value, and
| dynamically creating an array of type double and size of the
| integer value:
|
| double compute(int steps, int typeopt)
| {
| double *price_array;
|
| price_array = (double *) malloc(sizeof(int) * steps);
|
| ...
|
| }
|
| So far so good.
|
| Now in my main method, I invoke this function twice:
|
| value = compute(steps, 0);
| printf("\n%5.5lf", value);
|
| value = compute(steps, 1);
| printf("\n%5.5lf", value);

<snipperectomy>

You haven''t really provided enough info to allow a definitive answer.
From what you have provided, I''d suggest:

#include <stdio.h>
#include <stdlib.h>
double compute(unsigned,int);

double *price_array;
price_array = malloc(steps * (sizeof (double)));

Note addition of stdlib.h header and discard of cast. My prototype and
alteration of the statement in which you invoke malloc() makes
(possibly incorrect) assumptions as to how you intend to use the steps
variable; but at that point negative values seem inappropriate.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto


Schizoid Man写道:
Schizoid Man wrote:




我有一个函数,我在其中读取整数值,并且

动态创建double类型的数组和整数的大小

值:


双倍计算(int steps,int typeopt)

{

double * price_array;


price_array =(double *)malloc(sizeof(int)* steps);


...


}


到目前为止一切顺利。
Hi,

I have a function in which I am reading in an integer value, and
dynamically creating an array of type double and size of the integer
value:

double compute(int steps, int typeopt)
{
double *price_array;

price_array = (double *) malloc(sizeof(int) * steps);

...

}

So far so good.



到目前为止还不太好。你有一个错误的类型与sizeof运算符。

如果double是一个不同于int的大小,你将没有合适的金额

分配。


摆脱演员阵容,并将分配更改为:


price_array = malloc(steps * sizeof * price_array);

那样你就不会搞砸了。

So far NOT so good. You have the wrong type with the sizeof operator.
If double is a different size than int, you won''t have the right amount
allocated.

Get rid of the cast, and change the allocation to:

price_array = malloc(steps * sizeof *price_array);
That way you can''t screw up the type.


这篇关于帮助malloc()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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