>>5
But it's not a re-assignment. I'm initializing it. And that part works. The dynamic array gets allocated fine, but the other three pointers don't contain the correct address. They won't point to the array on the heap.
Name:
Anonymous2012-09-02 1:30
try this
<code>void allocate(int * a, int * &x, int ** &y, int *** &z)
{
a = new int [ 5 ];
x = a;
y = new int*;
*y = a;
z = new int**;
*z = new int*;
**z = a;
return;
}
int main()
{
int arr[5];
int *ptrX;
int **ptrY;
int ***ptrZ;
allocate(arr, ptrX, ptrY, ptrZ);
return 0;
}</code>
Name:
Anonymous2012-09-02 1:30
>>5
Unless Bjarne decided to break compatibility with C then a in that function is actually a pointer, that code is so horrible it wouldn't surprise me to find that something else was the issue.
>>9 Nothing wrong with allocating a dynamic int array to an int pointer.
Yes this is why I said that whatever error OP is experiencing should lie elsewhere.
Unfortunately, I am required to create an int array on the heap, but only declare the variables locally in main. That's possible, right?
Could I do something like this?
void set_ptrs (int * a, int ** x, int *** y, int **** z) {
*a = new int[5];
*x = a;
*y = x;
*z = y;
}
int main(void) {
int arr[];
int * ptrX, ** ptrY, *** ptrZ;
set_ptrs(arr, &ptrX, &ptrY, &ptrZ);
return 0;
}
Name:
Anonymous2012-09-02 1:49
>>13
Well since a is a local variable in the function ptrX and its closely named friends won't share values with arr.
If you're not using the variable just drop it.
#include <stddef.h> /* size_t */
void allocate (size_t n, int ** x, int *** y, int **** z) {
*x = new int[n];
*y = x;
*z = y;
}
int main(void) {
int * ptrX, ** ptrY, *** ptrZ;
allocate(5, &ptrX, &ptrY, &ptrZ);
delete [] ptrX;
return 0;
}
Again this is untested and I don't really know C++ but I think it should work.
I would encourage you never to design functions that allocate things you have to deallocate on another level, if you must at least provide an explicit reminder in the function documentation and a corresponding deallocate/free/close function.
Name:
Anonymous2012-09-02 1:51
An array name is really just a pointer. Use a pointer. You're welcome.
void allocate(int * &a, int * &x, int ** &y, int *** &z)
{
a = new int [ 5 ];
x = a;
y = new int*;
*y = a;
z = new int**;
*z = new int*;
**z = a;
return;
}
int main()
{
int * arr;
int * ptrX;
int ** ptrY;
int *** ptrZ;
>>6
And that part works. The plant looses its leaves and turns brown fine, but the other three plants don't contain the same number of lost leaves and level of brownness. They won't turn brown.
>>8
a is a reference to a pointer, and in the context of how it is called, it is a reference to an array.
>>20
within the realm of my mind, I won, and you will never be able to take away my victory. The analogy is just as terrible as ``An array is really just a pointer''.
Name:
Anonymous2012-09-02 13:58
>>17
Actually when he wrote int a[] in the function declaration he had a as a pointer.
Name:
Anonymous2012-09-02 13:59
>>20
The code you wrote has a bunch of memory leaks, >>14 contains the least bullshit and has no memory leaks.
although the difference between an array and a pointer was not the cause of OP's problem, which I had thought when writing >>17. So it makes sense to not get into the differences between an array and a pointer for now.
>>20,23
they are both equally pointless. there's no point in competing over who was better able to do OP's homework.
Name:
Anonymous2012-09-02 14:18
>>21
"The analogy is just as terrible as ``An array is really just a pointer''."
An array name IS a pointer. Literally. It's a pointer to the first element of the array. A house isn't a vehicle. Your analogy was dumb and proved nothing.
>>23
The code I wrote isn't a complete program, you idiot. Obviously you would deallocate, and add your includes. >>14 is fine, except it doesn't do at all what the OP wanted. Multiple levels of indirection, a dynamic array on the heap and an actual local pointer to that array in main. >>15 does.
the analogy would probably work better with the array being a house and the pointer being a piece of text representing the address of the house. And then when you refer to the house, people implicitly understand that you are only communicating the address of the house.
and I must express my disappointment in that you didn't try to solve the riddle. You'll never know the joy of discovering it yourself now.
Name:
Anonymous2012-09-02 16:30
>>31
Different poster here, but I'm not sure that 2nd code snippet would work. Might throw an error. Even if it doesn't though, it's doing something different than the first. It's allocating two different arrays; an array with 5 integers on the stack, and an array with 4 integers on the heap.
But this:
int a[5] = {1, 2, 3, 4, 5};
should be equivalent to this:
int *b;
b = new int[5];
except b would be on the heap. You could reference both the same way though. Array notation (b[3]) or pointer notation (*(a + 3)).
If you're referring to b and &b being different addresses, that's because the address being ointed to is different the actual address of the pointer variable. The array name (arr) is a constant pointer, but its address is the same as the address of the first array element it points to because it can't ever be changed.