pointer memory allocation

Hey guys. I am back. My semi starts soon. I wanted to know one essential thing. How is memory allocated in a pointer? Let me answer this and correct me if I am wrong: The data type defines how much memory will be allocated for a ptr variable..correct?

[code]struct test{

int one;
char two;
float* ptr;

}

struct test *ptr_test// memory of ptr_test equal memory of all the vars defined in test

how does the float* ptr works then?
sorry if I sound stupid
Last edited on
Let me answer this and correct me if I am wrong: The data type defines how much memory will be allocated for a ptr variable..correct?


That is not correct. What you are describing is an object, not a pointer.

A pointer merely contains an address, and therefore is typically 4 or 8 bytes in size, regardless of what it points to.

so a struct ptr will point to the first 4/8 bytes of the struct memory?
A pointer is an object which holds an address to another object.
csstudent123 wrote:
so a struct ptr will point to the first 4/8 bytes of the struct memory?
No, the pointer will point to the struct. The size of the struct is only taken into account by the compiler when generating the low-level code. The pointer itself has a size dependent on your operating system - 32-bit computers use 32-bit pointers, and 64-bit computers use 64-bit pointers.
ty ... but what does it mean to point to the struct? It must point at an address so what is that address of?
It's the address of the struct - which means the address of start of the block of memory where the struct is.
@ OP: The analogy that I like to use when describing pointers is to compare them to a URL, or in other words a websites address. A URL has nothing to do with representing the data that makes up a website. It is only an address that you go to in order to interact with data on the internet. Writing the URL for a website down on a piece of paper, does not turn that piece of paper into the website. Similarly, giving you an address where there is no valid website, or in other words nothing is allocated, is useless to you, isn't it? I understand that analogy is always suspect but is this starting to make sense to you at all?
Think of a pointer like an index in an array, with the array being the machine's memory (that's essentially all they are actually).
closed account (D80DSL3A)
An analogy I used early on for understanding pointers:
You're looking for Bob, but you find Joe. What might you try? Ask Joe if he knows where Bob is.
You're hoping that Joe can serve as a pointer to Bob. The basic concept is actually quite intuitive for us.
Last edited on
+1 Disch
the pointer may point to a particular object whose type is test, by using its address.

Aceix.
Thanks all. I find Mikeyboy's answer easy to understand most

which means the address of start of the block of memory where the struct is.


The total block of struct memory= size of all the data type declared with start address 0X000 to 0X999. So assume struct test has 0X000-0X999 thus
struct test *ptr_test points to 0X000...and float* ptr is somewhere stored in this address range but the actual value when deference *ptr//float may exist outside 0X999............is my explanation ok?
You have to understand that a pointer VARIABLE and the object being pointed at are two separate things.

Look at this:
int * ptr; int var;
[ -> ] {5}

Suppose that the one in the square brackets with the arrow is the pointer variable - it exists somewhere in memory - it is a standalone variable.

The curly braces with the 5 is an int variable residing in its OWN spot of memory! They are two very separate variables. Now, suppose that on our systems, an int is 4 bytes, so the variable would take up 4 bytes:

[101][102][103][104]

If we do this: ptr = &var;

We store the address of the FIRST byte occupied by 'var' inside 'ptr'.

The result looks like this:

int * ptr; int var;
[ 101 ] {5}

Notice that it is ptr's CONTENT that has changed, it now contains the address of the first byte occupied by 'var'.

The pointer itself has its OWN memory and there is no connection between var's memory and ptr's memory save that ptr contains the address of var.

Same with struct objects, class objects, dynamic memory and everything else. That's all there is to a pointer.

Hope that helps :)
so my example was ok?
Last edited on
Yes, your example was spot on :)
thanks..a relief
Topic archived. No new replies allowed.