Memory allocation

Jan 9, 2013 at 11:52pm
Hello!!!

why the result is different...how the memory allocation was made ?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
struct teste{
              int numero;
              struct teste *ptr;
            };
int main(void)
{
   teste *p=new teste;
   cout<<"Endereco de p->ptr  "<<p->ptr;
   cin.get();
}


p->ptr =0 , if i put one more pointer: *ptr2, the result is different.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
struct teste{
              int numero;
              struct teste *ptr;
              struct teste *ptr2;  // here
       };
int main(void)
{
   teste *p=new teste;
   cout<<"Endereco de p->ptr  "<<p->ptr;
   cout<<"\nEndereco de p->ptr2 "<<p->ptr2; // here
   cin.get();
}

the pointer receive an address...?
(sorry my bad english...)
Jan 10, 2013 at 12:01am
The pointers aren't initialized, they hold whatever values were in memory.
Use teste *p = new teste(); if you want them to be zeroed out. Or write a constructor.
Last edited on Jan 10, 2013 at 12:02am
Jan 11, 2013 at 11:09pm
but...the first result is zero "0".
the second result are two addresses....
the first pointer in the first code was zero...
the second should be the same result...or not ?

I just wanna learn how it works....can you explain ?

thanks...
Topic archived. No new replies allowed.