为指针分配内存 [英] allocate memory for pointer

查看:66
本文介绍了为指针分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了分段错误我检查了我的代码,发现

以下问题:


我想为数组重新分配内存。

我定义了以下函数:


int reallocateMemory(double * array,int newsize)

{

if(array)delete []数组;


array = new double [newsize];


返回1;

}


现在,

int main()

{

double * a = new double [2],* b = new double [10];


cout<< a<< ENDL; //的地址(1)


for(int i = 0; i< 10; i ++)b [i] = i; // b = {0,1,....,9}


reallocateMemory(a,10);


cout< < a<< ENDL; // a的地址与(1)相同! :(


for(int i = 0; i< 10; i ++)a [i] = 0.1 * i;


for (int i = 0; i< 10; i ++)

cout<< a [i]<<"" ;;

cout< < endl; //很好,a是0,0.1,...,0.9

for(int i = 0; i< 10; i ++)

cout<< b [i]<<"";

cout<< endl;


//错误:输出= 0.3,0.4,...,0.9,7,8,9

返回0;

}

所以a的内存没有被重新分配,虽然我把这个函数称为

更改它。


我认为是什么函数中的pass-by-pointer将直接更改

参数。因此,在我调用函数

reallocateMemory之后,应该更改a。但似乎是结果是

与传值相同。


我的功能有什么问题?


感谢您的帮助!


X

I encountered "segmentation fault" and I checked my code, found the
following problem:

I want to reallocate memory for an array.
I defined the following function:

int reallocateMemory( double *array, int newsize )
{
if (array) delete[] array;

array = new double[newsize];

return 1;
}

Now,
int main()
{
double *a = new double[2], *b = new double[10];

cout << a << endl; // address of a. (1)

for ( int i = 0; i < 10; i++ ) b[i] = i; // b = {0,1,....,9}

reallocateMemory( a, 10 );

cout << a << endl; // the address of a is same as (1)! :(

for ( int i = 0; i < 10; i++ ) a[i] = 0.1*i;

for ( int i = 0; i < 10; i++ )
cout << a[i] << " ";
cout << endl; // fine, a is 0,0.1,...,0.9

for ( int i = 0; i < 10; i++ )
cout << b[i] << " ";
cout << endl;

// error: output = 0.3,0.4,...,0.9,7,8,9

return 0;
}

So the memory of "a" is not reallocated although I call the function to
change it.

What I think is that passed-by-pointer in function will change the
parameter directly. Therefore after I call the function
reallocateMemory, "a" should be changed. But it seems that the result is
same as passed-by-value.

What''s the problem in my function?

Thank you for your help!

X

推荐答案



" xuatla" <徐**** @ gmail.com>在消息中写道

news:40 ************** @ gmail.com ...

"xuatla" <xu****@gmail.com> wrote in message
news:40**************@gmail.com...
我遇到了分段错误我检查了我的代码,发现了以下问题:

我想为数组重新分配内存。
我定义了以下功能:

int reallocateMemory(double * array,int newsize)
{
if(array)delete [] array;

array = new double [newsize];


如何将阵列返回到主?

返回1;
}

现在,
int main()
{
double * a = new double [2],* b = new double [10];

cout<< a<< ENDL; //的地址(1)

for(int i = 0; i< 10; i ++)b [i] = i; // b = {0,1,....,9}

reallocateMemory(a,10);
I encountered "segmentation fault" and I checked my code, found the
following problem:

I want to reallocate memory for an array.
I defined the following function:

int reallocateMemory( double *array, int newsize )
{
if (array) delete[] array;

array = new double[newsize];
How do you return array back to main now?
return 1;
}

Now,
int main()
{
double *a = new double[2], *b = new double[10];

cout << a << endl; // address of a. (1)

for ( int i = 0; i < 10; i++ ) b[i] = i; // b = {0,1,....,9}

reallocateMemory( a, 10 );




此函数后a指向已删除的内存。从此尝试任何东西

在那个内存上是UB。


所以它解决了问题变更重新分配到 -

int reallocateMemory(double *& array,int newsize)

^^^^^^^^

您现在正在调用对指针的引用。现在对阵列

所做的任何更改都将反映在主要内容中。


-Sharad



After this function a points to deleted memory. Henceforth trying anything
on that memory is UB.

So it resolve the problem change reallocate to -
int reallocateMemory( double *&array, int newsize )
^^^^^^^^
You are now paasing a reference to a pointer. Any changes now made to array
will be reflected back in main.

-Sharad

2004年7月15日星期四00:20:39 -0700,xuatla< xu **** @ gmail.com>写道:
On Thu, 15 Jul 2004 00:20:39 -0700, xuatla <xu****@gmail.com> wrote:
我遇到了分段错误我检查了我的代码,发现了以下问题:

我想为数组重新分配内存。
我定义了以下功能:

int reallocateMemory(double * array,int newsize)
{
if(array)delete [] array;

array = new double [newsize];

返回1;
}
现在,
int main()
{
double * a = new double [2],* b =新双[10];

cout<< a<< ENDL; //的地址(1)

for(int i = 0; i< 10; i ++)b [i] = i; // b = {0,1,....,9}

reallocateMemory(a,10);

cout<< a<< ENDL; // a的地址与(1)相同! :( int i = 0; i< 10; i ++)a [i] = 0.1 * i;

for(int i = 0; i< ; 10; i ++)
cout<< a [i]<<"";
cout<< endl; //罚款,a是0,0.1,.. (int i = 0; i< 10; i ++)
cout<< b [i]<<"";
cout<< endl;

//错误:输出= 0.3,0.4,...,0.9,7,8,9

返回0;
}

所以a的记忆并没有重新分配,虽然我把这个功能叫做改变它。

我认为通过了 - 函数中的by-pointer将直接改变
参数。因此,在我调用函数
reallocateMemory后,应更改a。但似乎结果与通过的相同 - by-value。


是的,指针和其他一切一样按值传递。

我的函数有什么问题?
I encountered "segmentation fault" and I checked my code, found the
following problem:

I want to reallocate memory for an array.
I defined the following function:

int reallocateMemory( double *array, int newsize )
{
if (array) delete[] array;

array = new double[newsize];

return 1;
}

Now,
int main()
{
double *a = new double[2], *b = new double[10];

cout << a << endl; // address of a. (1)

for ( int i = 0; i < 10; i++ ) b[i] = i; // b = {0,1,....,9}

reallocateMemory( a, 10 );

cout << a << endl; // the address of a is same as (1)! :(

for ( int i = 0; i < 10; i++ ) a[i] = 0.1*i;

for ( int i = 0; i < 10; i++ )
cout << a[i] << " ";
cout << endl; // fine, a is 0,0.1,...,0.9

for ( int i = 0; i < 10; i++ )
cout << b[i] << " ";
cout << endl;

// error: output = 0.3,0.4,...,0.9,7,8,9

return 0;
}

So the memory of "a" is not reallocated although I call the function to
change it.

What I think is that passed-by-pointer in function will change the
parameter directly. Therefore after I call the function
reallocateMemory, "a" should be changed. But it seems that the result is
same as passed-by-value.
Yes, pointers are passed by value just like everything else.

What''s the problem in my function?




你正在经过价值。如果你想要o使用一个函数来改变你有三个选择的变量值。


1)使用返回值


double * reallocateMemory(double * array,int newsize)

{

delete [] array;

返回new double [newsize ];

}


a = reallocateMemory(a,10);


2)使用参考


void reallocateMemory(double *& array,int newsize)

{

delete [] array;

array = new double [newsize];

}


reallocateMemory(a,10);


3)使用指针(在你的情况下,这将是指向指针的指针) )


void reallocateMemory(double ** array,int newsize)

{

delete [] * array;

* array = new double [newsize];

}


reallocateMemory(& a,10);

BTW没有必要在删除指针之前测试NULL


if(ptr)delete [] ptr;


完全正常工作与


删除[] ptr;


删除NULL保证无效。


john



You are passing by value. If you want to use a function to change that
value of a variable you have three alternatives.

1) Use a return value

double* reallocateMemory( double *array, int newsize )
{
delete[] array;
return new double[newsize];
}

a = reallocateMemory( a, 10 );

2) Use a reference

void reallocateMemory( double*& array, int newsize )
{
delete[] array;
array = new double[newsize];
}

reallocateMemory( a, 10 );

3) Use a pointer (in your case this would be a pointer to a pointer)

void reallocateMemory( double** array, int newsize )
{
delete[] *array;
*array = new double[newsize];
}

reallocateMemory( &a, 10 );
BTW it is not necessary to test for NULL before deleteing a pointer

if (ptr) delete[] ptr;

works exactly the same as

delete[] ptr;

Deleting NULL is guaranteed to have no effect.

john


[...]
int reallocateMemory(double * array,int newsize)
{
if(array)delete [] array;

array = new double [newsize];

返回1;
}
[ ...]
我的函数有什么问题?
int reallocateMemory( double *array, int newsize )
{
if (array) delete[] array;

array = new double[newsize];

return 1;
} [...]
What''s the problem in my function?



你的函数只改变了没有

影响的局部变量数组你的变量a在main中。将函数声明更改为

int reallocateMemory(double *& array,int newsize)

应该可以解决问题。现在不是a的值传递给函数

但它的引用因此你可以修改它的值...


your function changes only the local variable array which has no
influence to your variable a in main. Changing the functiondeclaration to
int reallocateMemory( double *&array, int newsize )
should do the trick. Now not the value of a is passed to the function
but the reference of it so you''re able to modify its value...


这篇关于为指针分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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