Re: allocate memory for pointer
From: xuatla (xuatla_at_gmail.com)
Date: 07/15/04
- Next message: Peter Koch Larsen: "Re: Stop throwing C++ exceptions"
- Previous message: jimmy: "Re: survival of c++"
- In reply to: John Harrison: "Re: allocate memory for pointer"
- Next in thread: Sharad Kala: "Re: allocate memory for pointer"
- Reply: Sharad Kala: "Re: allocate memory for pointer"
- Reply: John Harrison: "Re: allocate memory for pointer"
- Reply: Rolf Magnus: "Re: allocate memory for pointer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 15 Jul 2004 01:23:19 -0700
Thanks all of you for the kind help.
I made a mistake. As I mentioned I thought passed-by-pointer is
different with passed-by-value and the change will be kept after calling
the function. Now I know I made a mistake. If "a" is a double value, not
an array, then
testfun( double *a)
{ *a = newvalue; }
and
{
...
double a;
testfun(&a);
}
will change the value of a. But what I used is the pointer to an array.
So it's differnt.
Thanks for pointing out my mistake and giving me the good answers.
I have one more related (and naive) question
(1) double* a --- a is a double array
(2) double *a --- a is a double pointer
what's the difference of (1) & (2)? both a are address and one points to
an array while another points to a value. is there any other characters
in expression tell c++ that there're different? Thanks!
X
John Harrison wrote:
> On Thu, 15 Jul 2004 00:20:39 -0700, xuatla <xuatla@gmail.com> wrote:
>
>> 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?
>>
>
> 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
- Next message: Peter Koch Larsen: "Re: Stop throwing C++ exceptions"
- Previous message: jimmy: "Re: survival of c++"
- In reply to: John Harrison: "Re: allocate memory for pointer"
- Next in thread: Sharad Kala: "Re: allocate memory for pointer"
- Reply: Sharad Kala: "Re: allocate memory for pointer"
- Reply: John Harrison: "Re: allocate memory for pointer"
- Reply: Rolf Magnus: "Re: allocate memory for pointer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|