# Thread: Pointer

1. ## Pointer

Code:
``` #include<iostream.h>

int 		X = 123  ;
float 	Y = 456.7f ;
double 	Z = 9876.54321;

void D( int N )
{
int d = 4444 ;
}

void C( int N )
{
int c = 4444 ;
D( N ) ;
}

void B( int N )
{
int b = 4444 ;
C( N ) ;

}

void A( int N )
{
int a = 4444 ;
B( N ) ;
}

void main( )
{
A( 12 ) ;

int  *p, *q, *r, *s, *t, *u ;
p = new int ;
q = new int ;
r = new int ;
s = new int ;
t = new int ;
u = new int ;

}```
this is my profs code, plz dont slam me for it

I have to display the size of each variable, and its adress and the contents of each variable , i have to find the static memory, find the starting adress of the pile (dynamic memory) and the sytarting adress of the heap, and the size of the dynamic and static memory.

Prof explains dick ........, and i dont understand much on pointers so any tips, hints, help, tricks, etc, is more the appretiated.
this is what i have to date
Code:
```#include<iostream>

using namespace std;

int 	X = 123  ;
float 	Y = 456.7f ;
double 	Z = 9876.54321;

void D( int N )
{
int d = 4444 ;

cout << "d" <<" "<< d <<" "<< sizeof(d) <<" " << &d << endl;
cout << "N" <<" "<< N <<" "<< sizeof(N) <<" " << &N << endl;
}

void C( int N )
{
int c = 4444 ;
D( N ) ;

cout << "c" <<" "<< c <<" "<< sizeof(c) <<" " << &c << endl;
cout << "N" <<" "<< N <<" "<< sizeof(N) <<" " << &N << endl;
}

void B( int N )
{
int b = 4444 ;
C( N ) ;

cout << "b" <<" "<< b <<" "<< sizeof(b) <<" " << &b << endl;
cout << "N" <<" "<< N <<" "<< sizeof(N) <<" " << &N << endl;

}

void A( int N )
{
int a = 4444 ;
B( N ) ;

cout << "a" <<" "<< a <<" "<< sizeof(a) <<" " << &a << endl;
cout << "N" <<" "<< N <<" "<< sizeof(N) <<" " << &N << endl;
}

int main( )
{
cout << "variable, contents, size, adress: " << endl;
A( 12 ) ;

int  *p, *q, *r, *s, *t, *u ;
p = new int ;
q = new int ;
r = new int ;
s = new int ;
t = new int ;
u = new int ;

cout << "X" <<" "<< X <<" "<< sizeof(X) <<" " << &X << endl;
cout << "Y" <<" "<< Y <<" "<< sizeof(Y) <<" " << &Y << endl;
cout << "Z" <<" "<< Z <<" "<< sizeof(Z) <<" " << &Z << endl;

return 0;
}```
does that look right to show each variable, contents and adress?
I also have to do the same for the pointers (p, r, q, etc)
I also need to identify where the static memory is at, the staring adress for the pile (dymic memory) and identify the starting adress of the heap (dynamic memory)^
Code:
`	cout << "*p" <<" "<< *p <<" "<< sizeof(*p) <<" " << &p << endl;`
if i add this line of code, i get the adress of the pointer, its size (4, int, checks out), and content some weird useless number since its searching for somthing that doesnt exist, so is it all good??

2. > Prof explains dick ........,

Which you should learn to appreciate. The field of biology has contributed enormously to programming and vice versa.

>have to display the size of each variable, and its adress and the contents of each variable , i have to find the static memory, find the starting adress of the pile (dynamic memory) and the sytarting adress of the heap, and the size of the dynamic and static memory.

size of each variable is obtained with the sizeof operator. Check it out here: http://www.cppreference.com/

The address of a variable is obtained with the address-of operator (aka &). This is true of pointers too. So:
Code:
```int a = 10;
int* b = &a; // b is a pointer to a

std::cout << a << std::endl; // contents of a
std::cout << *b << std::endl; // contents of a (derefering the pointer gives us the the contents of a)
std::cout << &a << std::endl; // address of a
std::cout << &b << std::endl; // address of b
std::cout << b<< std::endl;  // contents of b (which, being a pointer is the same as address of a)```
I'm not sure what your professor means by the others though...

So here's my guess:

"Finding the static memory"
Describe which objects are being stored on the static storage. I'll give you one, Y. There's more.

"Finding the starting address of the pile" (does he mean stack?)
Of all variables stored in the stack, describe which one has the lowest address.

"Finding the starting address of the heap"
Same as above. But for variables dynamically allocated through new.

"their respective sizes"
It's the sum of the sizes of the variables in the stack (one) and the heap (the other).

3. Wow, your smarter than your prof:
his code:
Code:
`void main()`
your code:
Code:
`int main()`

4. so the code i posted is correct in refferance to the size and contents and adress... so x y z are stored staticly and all the pointers stored dynamicly? what about a, b, etc in the void functions? How do i find the lowest adress, their in hex, so how do i know which is lowest?

5. > what about a, b, etc in the void functions?

They are stored in the stack.

> How do i find the lowest adress, their in hex, so how do i know which is lowest?

The same you find which is smallest; if 13 or 27. Hex representations are still numbers. They are just represented differently. You can add them, subtract, compare...

6. I take it your proff is using an old IDE.

Code:
```#include <iostream.h>
void main()```

7. This assignment baffles me.
#include<iostream.h>
Non-standard.
void main( )
Non-standard.
float Y = 456.7f ;
Unusual naming. (All-caps is typically reserved for constants.)
find the starting adress of the pile (dynamic memory)
Never heard "pile" before. "Dynamic memory" is very rare, too, but usually refers to the heap, not the stack. Also, it's impossible to really find its "starting address". Nor is the information useful. Finding the lowest address of any of your stack objects requires quite a few hoops to jump through (Tip: on x86, the stack grows downward, so the lowest address will be that of d in the function D - but it also means the "starting" address is actually the highest address, which is probably that of p in main() - but not really, because the call to main() already puts stuff on the stack. Actually, to get the starting address, you must fully specify main's arguments and use those addresses, and then you still haven't found the start of the stack.), and unless you're armed with platform-specific knowledge, requires you to compare unrelated pointers, which is illegal.
the starting adress of the heap
This actually does require comparing unrelated pointers. Illegal.
size of the dynamic
The funny thing about dynamic memory is that it changes size all the time. If it's stack memory, the highest use will be in D() - but there's not really a way to find out how much it actually is. It will depend on the compiler and possibly on optimization options.
If it's heap memory, that's a bit easier. But not really: heap memory actually used is not the same as the sum of the size of all your allocations. The runtime adds some space overhead for its management stuff. So that's another quantity that's impossible to find out.

Bah. Write a program that generates numbers using rand() and it's likely to be more correct than whatever reference solution your "prof" came up with.

8. so if by asking for the contents of the pointers when their not acossiated to anything, isnt that illegal too, since their pointing to random spots in the memory that the program may not eve have acess too?

9. Yes, that's illegal too. But that's not actually done here, as far as I can see.

10. well i supposed to sho the contents of all variables, the pointers included, which would be illegal to acess their content, since they dont point at anything, thus have no content, right?

11. Think of a variable as a container (don't stretch this analogy too far) of which the contents are the value assigned to the variable and the address is its location in memory.

Think of a pointer as a container too of which the contents are the address of the variable it points too and its address is the pointer location in memory.

12. but if the pointer is not poiting to anything, asking it what it points to would be illegal wouldnt it?

13. Yes. But I believe CornedBee answered that already.

14. Check if it's null, if it's not then see what it has, otherwise you know it's null so /end

Popular pages Recent additions