STILL don't understand pointers?

i thought i had a handle on this months ago. the way i understand it is that a pointer is a block of memory containing another memory address. there's the pointer and the object pointed to.

however, the last example on the 'Classes I' page of this tutorial assigns values to pointers of class CRectangle without the pre existence of corresponding members. in such cases, the pointer obviously contains more than just a memory address. and come to think of it, use of cstrings does something similiar where an array of type char is referenced only by pointer.

i'm having a little trouble conceptualizing what's going on under the hood in cases like this. are such class members and character arrays accessible by anything other than the pointer (which i assume only contains the memory address of the first element)?
Reading your post, to me it seems you do understand pointers.

CRectangle * d = new CRectangle[2];

You should've been more specific but I suppose this is what's bugging you?
In the snippet above new[] allocates memory for two CRectangle objects, then returns the address of the first one.
So the pointer still stores nothing more than a memory address.

Now, since the objects are contiguous in memory we can use the pointer as if it was a C array, with the subscript operator [n].
pointer[n] means the same thing as *(pointer + n).

Read about pointer arithmetic if still in doubt.
Edit: or reformulate your question.
Last edited on
thanks, Catfish. yeah, this is what i was referring to:

1
2
3
4
5
6
7
8
  CRectangle a, *b, *c;
  CRectangle * d = new CRectangle[2];
  b= new CRectangle;
  c= &a;
  a.set_values (1,2);
  b->set_values (3,4);
  d->set_values (5,6);
  d[1].set_values (7,8);


a is populated through the member function, and c is a pointer to a, assigned with the address-of operator, but b and d[] are assigned through pointer only. but i think i get what you're saying, though the actual dynamics are still a little foggy for me.

you're basically saying that while the pointer still only contains the first memory address, each subsequent address is populated dynamically and only accessible through pointer arithmetic and member functions (which i'm still not sure how a pointer accesses member functions when there isn't a conventionally initialized "member"?)

i think what goes on with dynamic allocation might be my stumbling block here.
Last edited on
Every object has an "invisible" this pointer, which holds that object's memory address.

That said, I have a hunch only the data of objects is put into memory repeatedly for every object, whilst the member functions are "recorded" only once in the non-data part of the program, then called with the this pointer to know which object they're operating on.

each subsequent address is populated dynamically
new[] simply makes sure it gets a memory chunk that is safe to use. And then the objects use it as dictated by their member functions, constructor and destructor.
So I do not understand what you mean by "populated".
After line 1 is executed.
CRectangle a, *b, *c;

a is initialized/populated through its constructor, it is a value type.
b and c point to garbage, unutilized pointer types.

A pointer can be assigned to static memory (i.e. a value type), thats what c=&a is doing.

b and d are pointing to dynamic memory.
i think i have a little more research to do before i can pursue this question in a way that makes sense to anyone, but you've definitely pointed (n.p.i.) me in the right direction.

thanks guys.
You understand pointers fine. Seems like you just don't understand the -> operator.

the pointer obviously contains more than just a memory address.

You were right the first time. The pointer does not contain any more than a memory address. Nothing else.

You know how you can get the object a pointer is pointing at, like this: *pointer ?

If that object is some kind of class or structure, you can access its members like this:

someObject.member_function()

The -> operator is just shorthand for (*pointer). - this is, -> means "dereference this pointer, and then give me the following submember of that object".


1
2
3
4
pointer = & object;
object.a=4;
(*pointer).a = 4;
pointer->a=4;

The bottom three lines there all do the same thing.

i think what goes on with dynamic allocation might be my stumbling block here.
When you use new, it makes however many objects you have demanded in memory, and gives back a pointer to the first one. That's all there is to it. It is literally an array of objects.
Last edited on
hi Moschops. thanks for chiming in.

i think i lack the expertise to phrase my question correctly but basically... the four lines of code you provided presuppose the initialization of class "object". i'm pretty sure i have a grasp on that type of situation.

however, in the code i copy/pasted from the tutorial section of this site, b and d[] are initialized as pointers of type CRectangle and values are assigned "through" them, though no specific instance of CRectangle has been initialized to "encapsulate" those values.... again i feel like i'm not phrasing this correctly, but i think it has to do with dynamic allocation (?)

EDIT: somehow didn't see the last few lines of your post the first time. it's starting to make sense now. thanks. and you are correct that i don't completely understand "->".
Last edited on
EDIT: just now saw the bottom few lines of your post. it's starting to make sense now.


Yes, that's where it happens. In C++, using new allocates some memory and builds you complete objects, including running that object's constructor.


though no specific instance of CRectangle has been initialized to "encapsulate" those values....

That would be the situation in something like this:
1
2
object* p;
p->a; // Disaster! p doesn't actually point to an object, it's just some random value! 

But with the proper use of new...
1
2
3
4
object* p;
p = new object; // Aha, now p does point to an actual object, because new creates a complete object,
                       // and the return value from new is a pointer
p->a; // And everyone is happy 
Last edited on
the good part about self-educating is you can skip around and learn what you want when you want.

the bad part is you can skip around and learn what you want when you want. :)
When you create an instance of CRectangle you simply ask the compiler to allocate memory to store a CRectangle object's data (the "inner" variables).

When you use a pointer, you must allocate and deallocate the memory yourself.

So if the first case doesn't confuse you, why should the second?
It's just syntactic sugar that masks the same work that's being done in both cases: putting data onto memory.

You know how you can get the object a pointer is pointing at, like this: *pointer ?


And that above. In this case the asterisk (*) is the dereference operator, retrieving the data that the pointer points to.

1
2
3
4
5
6
7
pointer = & object;

object.a=4;
(*pointer).a = 4;
pointer->a=4;
// also:
pointer[0].a = 4;


Last four lines do the same thing... hm I think this was a bit overkill.
yeah i think i'm getting it.

now, not to beat a dead horse but, i see beginner code online and in text books where, say, an array is populated and the array is passed to another function by address. i guess what i'm wondering now is why populate the array in the first place if all you need is to allocate memory dynamically through a pointer?
Last edited on
An array is not a pointer, although one can behave like the other.
Arrays need to be given their size when declared, i.e. size needs to be known at compile time.
A fake array constructed with dynamic memory allocation and a pointer can have a size given at runtime.

The main drawback with dynamic memory allocation in C and C++ is that you need to manually deallocate the memory afterwards.

Still, could you explain what you mean by "populated"?
The main drawback with dynamic memory allocation in C and C++ is that you need to manually deallocate the memory afterwards.

oh ok, i gotcha. thanks.

Still, could you explain what you mean by "populated"?

oh, it's just a word i see in C++ books that i'd always taken to mean "assign values to variables". it wouldn't shock me if i was wrong, which i guess is what you're implying...
Last edited on
An array is not a pointer, although one can behave like the other.

Actually the name of the array is a pointer, it points to the first element in the array.
The memory it points to is static not dynamic

The main drawback with dynamic memory allocation in C and C++ is that you need to manually deallocate the memory afterwards

In C++ look at unique_ptr or shared_ptr, with these you don't need to free dynamic memory, they take care of it for you.
closed account (1vRz3TCk)
binarybob350 wrote:
Actually the name of the array is a pointer, it points to the first element in the array.
The memory it points to is static not dynamic
Catfish is correct, an array (or name thereof) is not a pointer. It is implicitly converted to a pointer as and when required.
+1 CodeMonkey, Catfish.

array names are cast to pointers to their first element, but they are not exactly the same thing. This is most often observed with the sizeof() operator:

1
2
3
4
5
int array[10];  // an array of 10 elements
int* ptr = array;  // a pointer to the first element of that array

cout << sizeof(array);  // size in bytes of the array
cout << sizeof(ptr);  // size in bytes of the POINTER (ie:  doesn't matter how big the array is) 


However it can also be observed by attempting (and failing) to pass a pointer to a function that takes an array by reference, or with typeid
Topic archived. No new replies allowed.