weird numbers such as -8164983 come out...
Printable View
weird numbers such as -8164983 come out...
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.
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.
outputs: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;
}
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?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
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!
> 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.
assignement is due tomorrow, and we've already confirmed hes inconfident, guess il do the best i can!