Search code examples
c++pointersdereference

a couple of simple questions about basic pointer use in c++ and the c++ memory model


I've been studying along with the Stanford courses on iTunes U and have hit pointers in C++. I think I understand how pointers work, but I just want to check how to do some simple stuff. Let's say I want to create a dynamic array:

    double *array;

At this point there's a variable called "array" in the stack and nothing in the heap. First question - what's stored in "array" at this point? A pointer to some nonsense piece of memory?

I then allocate memory using "new":

    array = new double[10];

Second question - at this point, what's stored in "array"? A pointer to some contiguous piece of memory big enough to hold ten doubles? (Sorry for the simple questions, but I really want to make sure I understand)

I assign the double 2.0 to each element in the array:

    for(int i=0; i<array.length(); i++) array[i]=2.0;

Third question - is this different from using the dereference operator to assign? (i.e., *array[i]=2.0). I then pass the array to some other function:

    myFunc(double array[]){
        for(int i=1; i<array.length(); i++){
            array[i]=array[i]*array[i-1];
        }
    }

Fourth question - on the pass to myFunc, since array is an array of pointers to doubles, and not an array of doubles, it passes by reference without "&", right? That means the operations in my loop are affecting the actual data stored in "array". What if I wanted to pass by value, so that I wouldn't be touching the data in "array"? Would I use

    myFunc(double *array[]){...}?

Last question - what if I wanted to manipulate the memory addresses for the contents of "array" for some reason? Could i use

    someVar = &array[5];

to assign the the hex address of array[5] to someVar?

I've read the section on pointers in the reader and watched the Binky video a dozen times and it still doesn't make sense. Any help would be greatly appreciated.

EDIT: Thanks a lot to everyone who answered so far. If you wouldn't mind I just have one more question. In the declaration double *array;, "array" is declared as a pointer to a double, but once I use "new" to assign it, "array" ceases being a pointer to a double, and becomes an array of doubles, right?


Solution

    1. array contains junk data - whatever was in that memory location before array existed is still there. If you try to play with it you're going to shoot yourself in the foot, which is why you need to assign it to a valid memory location, (hence the ensuing call to new[]).
    2. Yes, array now contains a pointer (memory address) to some contiguous piece of memory big enough to hold ten doubles.
    3. *array[i]=2.0 won't actually compile. array[i] results in a double, and you can't use the dereference operator on a double.
    4. What you're passing is that address to the first element in the array. So you are passing the pointer by value, and the array by reference (as the pointer is a reference to the array.) To pass the array itself by value you'd have to have one parameter for each entry. You could also copy the array and send in the copy, but the copy itself would be passed by reference, too.
    5. double* someVar = &array[5]; will return to you a pointer to the 6th element of the array. array[5] gives you the double, and taking the address of it (with &) will give you the memory address (pointer) of that double.