Re: allocate memory for pointer

From: xuatla (xuatla_at_gmail.com)
Date: 07/15/04


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



Relevant Pages

  • Re: Help - string length question in C
    ... The array itself is not passed, but a pointer to the first element ... Experienced C programmers are well aware of this problem ... a mistake and does not catch that mistake in debugging or testing ...
    (comp.programming)
  • Re: why doesnt this code work? :)
    ... sorry I make a mistake, ... > With this declaration you have a pointer to an array and each field of the ...
    (comp.lang.c)
  • Re: allocate memory for pointer
    ... Now I know I made a mistake. ... >> value, not an array, then ... inline const TO& accessor_cast ...
    (comp.lang.cpp)
  • Re: Complex array definition
    ... (pointer to a pointer that points to a char). ... mistake, he really wanted to create a 2-d array of chars, and only needed ...
    (comp.lang.c)
  • Re: Library Design, f0dders nightmare.
    ... first demo mistake but I suggest to you that a sequence of blunders ... The stack is only used temporarily while creating the argv array. ... in the input buffer. ... so you are not wasting memory. ...
    (alt.lang.asm)