Thread: Pointer

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    weird numbers such as -8164983 come out...

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    An uninitialized pointer can point to anywhere, so you will get strange values if you attempt to print them or the values they "point" to. If you attempt to dereference them and get their values without initializing them to point to something, then you might get a crash or a random number or something else, that is illegal.

    If you initialize the pointer to point to a new int, but don't initialize the int itself, then what you are doing is technically legal I believe, but you will still get some unknown value because the int is not initialized.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Weird where?

    p = new int; cout << *p; is weird?
    Quite probably since dynamic memory and local variables are junk until you specifically initialise them.

    I'm puzzled as to what it is you're trying to achieve here as nearly all of the answers are implementation specific, meaning you can't use the answers for "general" programming use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    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;
    	cout << "*p" <<" "<< *p <<" "<< sizeof(*p) <<" " << &p << endl;
    	cout << "*q" <<" "<< *q <<" "<< sizeof(*q) <<" " << &q << endl;
    	cout << "*r" <<" "<< *r <<" "<< sizeof(*r) <<" " << &r << endl;
    	cout << "*s" <<" "<< *s <<" "<< sizeof(*s) <<" " << &s << endl;
    	cout << "*t" <<" "<< *t <<" "<< sizeof(*t) <<" " << &t << endl;
    	cout << "*u" <<" "<< *u <<" "<< sizeof(*u) <<" " << &u << endl;
    
    	return 0;
    }
    outputs:
    Code:
    variable, contents, size, adress:
    d 4444 4 0012FDE0
    N 12 4 0012FDEC
    c 4444 4 0012FE3C
    N 12 4 0012FE48
    b 4444 4 0012FE98
    N 12 4 0012FEA4
    a 4444 4 0012FEF4
    N 12 4 0012FF00
    X 123 4 00476DC0
    Y 456.7 4 00476DC4
    Z 9876.54 8 00476DC8
    *p -842150451 4 0012FF7C
    *q -842150451 4 0012FF78
    *r -842150451 4 0012FF74
    *s -842150451 4 0012FF70
    *t -842150451 4 0012FF6C
    *u -842150451 4 0012FF68
    so lets recap: I had to display each variable, contents, size and adress, donce (i think). When outputtin the contents of my pointers i get -842150451, now is this an illegal content because it doesnt point at anything?

    Using the above info, i have to draw a map of the memory:

    where does the static memory start and end, and does it climb or desend in terms of location im gunna assume, same for systeme stack, dynamic heap and dymanic memory, and i have to determine the total size of the dynamic memory and static memory. If its impossible, is their a way to estimate about the values?

    Thanks guys, big help there!

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > where does the static memory start and end, and does it climb or desend in terms of location im gunna assume, same for systeme stack, dynamic heap and dymanic memory, and i have to determine the total size of the dynamic memory and static memory. If its impossible, is their a way to estimate about the values?

    Some of those questions don't make sense. Others are probably impossible or very difficult to answer. Your level of knowledge (and your professor code) made me give you a few guesses as to what your professor may mean on my first or second post.

    However that's what they are. Uneducated guesses. You will be better served if you ask your professor to clarify those questions.
    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. #21
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    assignement is due tomorrow, and we've already confirmed hes inconfident, guess il do the best i can!

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