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 17:09

>>39
So do I. I really miss him.

Name: 26 2012-09-02 17:25

Ok, I forgot to include the word "constant" pointer, BFD... I'm still correct.

>>38
Are you an idiot? Run this code and tell me what you think.

#include <cstdio>

int main(int argc, char** argv) {
    int a[5] = { 3, 5, 7, 9, 11 };
    int * const b = a;

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

    return 0;
}


How can someone not know that the array variable name is a constant pointer to the array data... elementary shit.

Name: Anonymous 2012-09-02 17:33

>>40
yes, the point was that a constant pointer is still a pointer, and a pointer is a value that is stored and accessible somewhere in memory. An array can be thought of as just a value, the address of where it is allocated. The value of the array (it's address) has no place of storage that you can access.

&p[0] == p == *&p == *&p will hold for all pointer values

Name: Anonymous 2012-09-02 17:37

>>42
you forgot to evaluate &a and &b

Name: Anonymous 2012-09-02 17:38

>>44
nice dubs bro

Name: Anonymous 2012-09-02 17:40

>>45
thanks

Name: sage 2012-09-02 17:42

You're both wrong. The array name is treated semantically as a constant pointer to non-constant data, but it's not an actual pointer in that it's a separate location in memory that stores the address of the array it points to. It sits there with the array itself and you can treat it as a constant pointer.
Boring topic.

Name: Anonymous 2012-09-02 19:10

***&SEPPLES

Name: Anonymous 2012-09-02 20:36

You guys are stupid as hell, I said that a is a pointer IN THE FUNCTION, arrays in function declarations are interpreted as pointers, never did I say that int a[5] is a pointer.

Name: Anonymous 2012-09-02 20:37

>>26
Multiple levels of indirection,
It has that.

a dynamic array on the heap
It has that as well.

and an actual local pointer to that array in main.
It has that too.

Can't you read simple C++ code?

Name: Anonymous 2012-09-02 22:35


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