Pointer

This is a discussion on Pointer within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream.h> int X = 123 ; float Y = 456.7f ; double Z = 9876.54321; void D( int N ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    8

    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. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    > 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).
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Wow, your smarter than your prof:
    his code:
    Code:
    void main()
    your code:
    Code:
    int main()
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    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. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    > 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...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    I take it your proff is using an old IDE.

    Code:
    #include <iostream.h>
    void main()
    I'm just trying to be a better person - My Name Is Earl

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    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?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Yes, that's illegal too. But that's not actually done here, as far as I can see.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    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?

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    but if the pointer is not poiting to anything, asking it what it points to would be illegal wouldnt it?

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Yes. But I believe CornedBee answered that already.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    Check if it's null, if it's not then see what it has, otherwise you know it's null so /end

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21