pointers to array

Pages: 12
closed account (Dy7SLyTq)
Every single pointer type just uses up "a little bit to hold the address of what its pointing to". That's what a pointer is - an address of what it's pointing to.


no a pointer is a pointer to the adress. not the adress

a void* takes up no storage. it is the c style way of not having to know the type until later.
You can get the size of a pointer by using sizeof.
std::cout << sizeof(void*) << std::endl;

I don't think the standard guarantees that all pointers to data have the same size. Pointers to member functions are often larger than other pointers but that is another kind of pointer so maybe that's not relevant.
Last edited on
closed account (Dy7SLyTq)
as i just said... 4 bytes or 8 bytes in size
4 and 8 are common sizes but not guaranteed by the C++ standard.
closed account (Dy7SLyTq)
i thought they did that in 0x?
no a pointer is a pointer to the adress. not the adress

You're talking nonsense.

A pointer is a variable that stores a number - an integer. That number is an address in memory.

When you look at the bit of memory at that address, you find the actual data, and that actual data takes up whatever space it needs. But the data is not the address. The address is the number.

And you still need the number, and that number is a pointer.

This is true for all pointers, even void* ones.
Last edited on
closed account (Dy7SLyTq)
you said a pointer was an adress. thats wrong. its value is an adress. it points to the adress where the data is stored. and no, not void*'s. they dont point to data until told to. its the c equivalent to auto and template
> does a pointer occupy the same amount of memory for any data type?

Not necessarily.

This is what the C standard specifies:
A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
you said a pointer was an adress. thats wrong. its value is an adress.

You're just quibbling semantics now. A pointer is a variable, just like any other. Just like any other variable, it takes up some storage. Yes, its value is an address.

it points to the adress where the data is stored. and no, not void*'s. they dont point to data until told to.

You seem to be confused. No pointer, of any kind, points to memory where data is stored unless it's told to.

If I just write:

 
int* iPtr;

Then I haven't told it to point to any data.

If I write
 
int* iPtr = 0x1;

then I'm pointing it at an address in memory, but it's almost certainly not pointing to valid integer data.

If I write:
1
2
int i = 3;
int* iPtr = &i;

then now it's pointing to actual integer data, but only because I've told it to.

All of those are legal C++.

All these things are equally true for void* pointers.
Last edited on
DTSCode wrote:
i thought they did that in 0x?

No, that would limit the hardware that can run C++ programs (without getting worse performance) for no good reason.

DTSCode wrote:
a void* takes up no storage.

Sure it does (except if it's optimized away but that could happen to any type and only in special cases). The size of a void* is sizeof(void*). It will not magically change size when you assign to it.
well guys, this is what i wanna conclude..
a pointer is just a variable, similar to all other types, except that instead of storing a value, it stores the address.
so while declaring a pointer,

int *a;

we just confirm that the data type to which our pointer , a points to is an integer.
but then again, if a pointer always occupies 2/4 bytes , for every variable, regardless of its data-type,
then what is the need of specifying the data type while declaring a pointer?
Last edited on
closed account (Dy7SLyTq)
because c++ is strongly typed
The type has to be known to be able to use whatever the pointer points to in a meaningful way. Functions and operators might look the same but they often do different things depending on the type.

If p is a void*, what would cout << *p; print? This would give you a compiler error because it couldn't find a matching operator<< (ostream& operator<<(ostream&, void*)).

If C++ was trying to make this work the pointer would also have to contain information about what type is being pointed to to be able to call the correct function. This would give you additional memory and runtime overhead that doesn't go well with the C++ philosophy.

Being strict about the types is not only for the benefit of the compiler but also for the programmer because it allows us to catch many errors at compile time instead of at runtime.
Last edited on
Topic archived. No new replies allowed.
Pages: 12