Dynamic memory allocation- class objects

Hello!
I tried in Gamer's example to "preserve" an object created in a function in the A class using dynamic allocation, so that I can call it directly from main.

Please, can U help me finding basic mistakes? I can't google easy example where objects of the classes are memorised like usula objects (variables).


Please, help, SOS!!!
Many thansk in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>

class B
{
    friend class A;
   
private:
    B(int c) : i(c) {}
    int i;
};

class A
{
public:    
    void a()
    {
        B b(10);
        cout<<b.i<<endl;
        b.i=b.i+2;
        A * p= new A(b.i); //here I wanted to memorise the OBJECT using the 
       }
           
};

int main()
{
    A a;
    a.a();
    cout<<p->i<<endl; //here I want to cout that exact memorised object's              //attribute..
    delete p;
}
line 20 declares a variable which is an A* but it is declared inside a(){} so when a() finishes p is destroyed. if you want p to exist after a() exits you need to declare it at class scope like B::i

also, when a() exits and destroys p, it does NOT destroy the A that was newed because it is on the heap. which leaves you with a memory leak.

Hello!
Many thansk for answer!!!

Plese, how would look the
to declare it at class scope like B::i
like? Where can I find syntax rules?


@line 20 p= new A(b.i);
@line 22 A* p;
@line 29 cout<<a->p<<endl;

class A also needs a destructor so it can delete p. main is the wrong place for that job.
Last edited on
Many thanks! Plese, before I lounch anohter stupid question, if someone has a good example link with class objects codes, please send me here! Thanks in advance!!!
Last edited on
Hello!
Wait wait wait, WHAT is on the heap?


why do we have a->i in the line 29 and not p->?

a is name of the function, p is name of the pointer?
I ment a link about class objects as objects in dynamic memory.
Thanks!!!

Last edited on
Wait wait wait, WHAT is on the heap?

the A object that was created using the "new" keyword. the p is only a pointer to it, as denoted by the *


why do we have a->i in the line 29 and not p->?

we cant see p, it is "inside" a.
so we say a->p meaning the p inside a.

there is no difference between class objects or any other data type when talking about stack/heap memory. we are just saying where they are stored.

if you declare a variable (an instance of a class is also a variable) it will go on the stack.
if you create one using "new" it will go on the heap.

http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/
Last edited on
Hello!
With and without destructor it gives the same error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

class B
{
    friend class A;
   
private:
    B(int c) : i(c) {}
    int i;
};

class A
{
public:  
A*p;  
    void a()
    {
        B b(10);
        cout<<b.i<<endl;
       // b.i=b.i+2;
        p= new A(77); //here I wanted to memorise the OBJECT using the 
       }

~A(){delete p};          
};

int main()
{
    A a;
    a.a();
    cout<<a->i<<endl; //here I want to cout that exact memorised object's              //attribute..
    delete p;
}


t.cpp: In member function 'void A::a()':
Line 21: error: no matching function for call to 'A::A(int)'

What is that "matching function" that is missing?
Last edited on
the missing function is the constructor that takes an int, you have one for B at line 8.

also, line 31 still references a->i, but only B has an i, A has a p.

and, at line 32 you still try to delete p, that is now done by A's destructor so that line can go.
Hello!
Problem is that I try to create a new object INSIDE THE FUNCTION a, and to preserve it out of the a scope, so that we can call it from main.

Do I need ANOTHER constructor in A?
Hello, please help someone,m completely lost!
Maybe any link with code doing sth similar, just to compare?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

class B
{
    friend class A;
   
private:
    B(int c) : i(c) {}
    int i;
};

class A
{
public:  
  
    void a()
    {
        B b(10);
        cout<<b.i<<endl;
        A(int z): b.i(z} {};
        //b.i=b.i+2;
        p= new A(77); //here I wanted to memorise the OBJECT using the 
       }
A *p;
~A(){delete p};          
};

int main()
{
    A a;
    a.a();
    cout<<a->i<<endl; //here I want to cout that exact memorised object's              //attribute..
    delete p;
}
when you say OBJECT do you mean A or B?

if it is only the value you want to preserve, why not just take a copy?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
class B
{
    friend class A;
   
private:
    B(int c) : i(c) {}
    int i;
};

class A
{
public:  
  
    void a()
    {
        B b(10);
        cout<<b.i<<endl;
        b.i=b.i+2;
        i = b.i; //here I wanted to memorise the OBJECT using the 
       }
  int i;
          
};

int main()
{
    A a;
    a.a();
    cout<<a.i<<endl; //here I want to cout that exact memorised object's              //attribute..
}
Last edited on
Hello!
The point of all is to use DINAMIC MEMORY for CLASS OBJECTS...I need to think up an example...
Hello!
Hope U understand, I wanted to create dynamic allocatd object in function a(), class A, and so I want it to survive to main..

Please, what are mistakes?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

class B
{
    friend class A;
   
private:
    B(int c) : i(c) {}
    int i;
};

class A
{
public:  
A*p;  
    void a()
    {
        B b(10);
        cout<<b.i<<endl;
       // b.i=b.i+2;
        p= new A; //here I wanted to memorise the OBJECT WITHIN the class /function...
        *p=B b(5);
       }

~A(){delete p;};          
};

int main()
{
    A a;
    a.a();
    cout<<p->a()<<endl; //here I want to cout that exact memorised object's              //attribute..
   
}

	


Thanks!!!
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

class B
{
    friend class A;
   
private:
    B(int c) : i(c) {}
    int i;
};

class A
{
public:  
B *p;  

    
        B b(10);
        cout<<b.i<<endl;
       // b.i=b.i+2;
       A(){p=new B(5)}
         //here I wanted to memorise the OBJECT WITHIN the class /function...
        
};

~A(){delete p;};          
};

int main()
{
    A a;
    a.a();
    cout<<p->a()<<endl; //here I want to cout that exact memorised object's              //attribute..
   
}

	

19:13: error: expected identifier before numeric constant

19:13: error: expected ',' or '...' before numeric constant

20:9: error: 'cout' does not name a type In constructor 'A::A()':

22:22: error: expected ';' before '}' token At global scope:

27:4: error: declaration of '~A' as non-member

28:1: error: expected declaration before '}' token
do you mean like this?

1
2
3
4
5
6
7
8
9
    
   void a()
    {
        B b(10);
        cout<<b.i<<endl;
        b.i=b.i+2;
        p= new B(b.i); //save a copy of b for later 
     }
 


also, your latest code @34 still attempts to reference p which is inside a. you must say a.p to get that p. even then you wont be able cout it because only class A is a friend of class B, so only class A can reach inside to get at i.

The following will fail because you cant reach inside to get to i.
cout << a.p->i << endl;
see http://www.cplusplus.com/doc/tutorial/inheritance/

i is private to class B, class A can access it because it is a friend, but that "cout" line of code is not in class A.




Last edited on
Topic archived. No new replies allowed.