Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[C++] Allocating local variables

Name: Anonymous 2012-09-02 0:12

Shouldn't this be working? I'm trying to point all three pointers to this new int array, but the address isn't right.

<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 the dynamic memory
    allocate(arr, ptrX, ptrY, ptrZ);

    return 0;
}</code>

Name: Anonymous 2012-09-02 0:13

SHIT.

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 the dynamic memory
    allocate(arr, ptrX, ptrY, ptrZ);

    return 0;
}

Name: Anonymous 2012-09-02 0:46

It doesn't surprise me to find such broken code from a SEPPLES user, I'm just glad you're not using C.

Name: Anonymous 2012-09-02 0:48

For real.. what part of my code is broken exactly?

No need to attack me for using C++. I'm a student with not a lot of choice in that regard.

Name: Anonymous 2012-09-02 1:09

you can't reassign arrays. a = ..... wont work.

Name: Anonymous 2012-09-02 1:27

>>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: Anonymous 2012-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: Anonymous 2012-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.

http://c-faq.com/aryptr/aryptrparam.html

Name: Anonymous 2012-09-02 1:32

>>8
>a in that function is actually a pointer

Nothing wrong with allocating a dynamic int array to an int pointer.

Name: Anonymous 2012-09-02 1:38

Why so many goddamn levels of indirection?

Name: Anonymous 2012-09-02 1:38

>>6
When you wrote int arr[5]; then you have already allocated an array of five ints.

When you type int a[] in the function this gets interpreted as int *a which is a local variable.

Unless you are mandated to use the heap you should avoid in this case entirely.

void set_ptrs (int * storage, int ** x, int *** y, int **** z) {
  *x = storage;
  *y = x;
  *z = y;
}

int main(void) {
  int arr[5];
  int * ptrX, ** ptrY, *** ptrZ;

  set_ptrs(arr, &ptrX, &ptrY, &ptrZ);

  return 0;
}


I have not tested this code but I am confident that it will do what you wish.

Name: Anonymous 2012-09-02 1:39

>>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.

Name: Anonymous 2012-09-02 1:43

>>11
Ahh, thank you.

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: Anonymous 2012-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: Anonymous 2012-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;

    allocate(arr, ptrX, ptrY, ptrZ);

    for (int i = 0; i < 5; ++i)
    {
        arr[i] = i*2;
    }

    return 0;
}


Tested. Works

Name: Anonymous 2012-09-02 1:54

>>15
that worked.. thanks

Name: Anonymous 2012-09-02 2:09

>>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.

>>11
it was a reference

>>12
The function by itself is ok, but not in the context in how it is called. Look at what it is trying to do. Is it defined?

>>15
A house is really just a vehicle. Use a vehicle. You're welcome.

Name: >>17 2012-09-02 2:16

nevermind. I read it wrong. a was not a reference. how embarrassing.

Name: Anonymous 2012-09-02 5:52

****
LOL!

Name: Anonymous 2012-09-02 13:22

>>17
"A house is really just a vehicle"

No, it isn't. Terrible analogy.

My code was correct. I solved his problem. I win.

Name: Anonymous 2012-09-02 13:58

>>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: Anonymous 2012-09-02 13:58

>>17
Actually when he wrote int a[] in the function declaration he had a as a pointer.

Name: Anonymous 2012-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.

Name: >>21 2012-09-02 14:00

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.

Name: Anonymous 2012-09-02 14:09

>>20,23
they are both equally pointless. there's no point in competing over who was better able to do OP's homework.

Name: Anonymous 2012-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.

Name: Anonymous 2012-09-02 14:38

A house is actually a cudder.

Name: Anonymous 2012-09-02 14:58

>>26
one day you will solve the riddle.

Name: Anonymous 2012-09-02 15:21

>>28
Now it's a riddle. Admit it, you typed something stupid.

You probably didn't even know that the array variable is a pointer. HAH. You probably think

int arr[5];
cout << arr;


is doing something different than

int arr[5];
cout << &arr[0];


Do you even know how memory works? Ha HA.

Name: Anonymous 2012-09-02 16:15

>>29
I dunno about C++ but with C array types ARE NOT pointer types. But basically in every use case like this they degenerate to pointers right away.

Name: Anonymous 2012-09-02 16:16

>>29
and what about:


int* a;
a = new int[4];


versus


int a[5];
a = new int[4];


I think there might be difference here. What can you do to a car that you can't do to a house?

Name: >>31 2012-09-02 16:19

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.

Name: >>31 2012-09-02 16:30

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: Anonymous 2012-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)).

Name: Anonymous 2012-09-02 16:33

>>34
there is still a difference in that you can reassign b after it has been assigned to new int[5], but you can never change the value of a.

Name: Anonymous 2012-09-02 16:39

>>35
Right. What >>26 should've said was that the array name is a constant pointer to the first element of that array. That's true.

So yeah, if you have this array declaration:

int arr[5];

then arr would be synonymous with &arr[0] except that it can't be reassigned. It's a constant pointer whose address stored never changes.

Name: Anonymous 2012-09-02 16:44

I think the lesson to be gained here is how intimately close arrays and pointers are in C++.

Name: Anonymous 2012-09-02 16:56

>>36
even then, an array isn't the same as a constant pointer. Try running this:


#include <cstdio>

int main(int argc, char** argv) {

  int arr[5];
  int* const b = new int[5];

  printf("arr = %p, &arr = %p\n", arr, &arr);
  printf("b = %p, &b = %p\n", b, &b);

  delete[] b;
  return 0;
}


>>37
no

Name: Anonymous 2012-09-02 17:04

I miss kodak-san. ey would have had so much fun in this thread.

Name: Anonymous 2012-09-02 17:08

>>38
Runs okay for me.

arr = 0023FE94, &arr = 0023FE94
b = 003330F0, &b = 0023FE88

Press any key to continue . . .


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.

You could also add this:

#include <cstdio>

int main(int argc, char** argv) {

  int arr[5];
  int* const b = new int[5];

  printf("arr = %p, &arr = %p, &arr[0] = %p\n", arr, &arr, &arr[0]);
  printf("b = %p, &b = %p\n", b, &b);

  delete[] b;
  return 0;
}


and see how arr, &arr and &arr[0] are really synonymous.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List