Archived

This topic is now archived and is closed to further replies.

memory allocation inside allocation

This topic is 5336 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, let's say i have a c++ class which allocte memory:
      
class classA
{
    classB *b;
    
    a()
    {
       b=new classB();
    }
};
   
now, will it be legal to write in other part of the code:
            
    classA a=new classA() ;
  
I'm asking this because here I have a memory allocation inside another memory allocation, and I got some weird bug in simillar case (although no compilation, linking problems). thanks for any idea [edited by - friman on May 6, 2003 11:28:28 AM]

Share this post


Link to post
Share on other sites
Yes, it''s perfectly legal to do this...however this statement is illegal:

classA a=new classA;

needs to be changed to:

classA* a=new classA;

Also, always make sure you delete the memory after you are done with it.

Let us know a bit more about this "wierd bug".

Regards,
Jeff

Share this post


Link to post
Share on other sites
rypyr you are definitely right about that pointer, but this is not the problem in the code.

The problem is that I have two variants of the same program,
version 1: like in my previous message: classA allocates classB.
version 2: with no allocation, just like that:
classA
{
classB b;
}
The second version is OK, In the first version some of the functions doesn''t work as I expect, although there are no memory violations or any apparent problem.
(I, of course changed the operators from "." to "->" and all the other things)



Share this post


Link to post
Share on other sites
Ok, this is a part of a complex code,

this is a kind of game:

       
class Game
{
drawScene *render; //version 1 works bad

//drawScene render; // version 2 works ok

bool loaded;

public:

Game::Game();
Game::~Game();
void Game::loadGame();

.............

int Game::drawScene1();
};


the initialization, and the function that works bad


    
Game::Game(): loaded(false)
{
render=NULL; // version 1

render= new drawScene(); // version 1

}
int Game::drawScene1()
{
if( render )
{
return render->DrawScene1(); // version 1

//return render.DrawScene1(); //version 2

}
else return 0;
}



inside drawScene there are other memory allocations.



[edited by - friman on May 6, 2003 12:26:11 PM]

Share this post


Link to post
Share on other sites
I''ll try to explain:
render->DrawScene1() function should draw some 3d models and some orthographic stuff. ''render'' itself is an object that makes other memory allcations.
in my problematic version render->DrawScene1() draws the orthographic stuff, BUT NOT THE MODELS. it works partialy.

I don''t figure out why things like that may happen.

Share this post


Link to post
Share on other sites
When you have class members which are pointers, you generally need to define you own copy constructor and assignment operator, else, when copying objects, shallow copies are made instead of deep copies - maybe try a search on Google for an explanation. If you are creating copies of Game objects (which you may not realise you are), this is probably what is causing your problems.

Try something like:


class Game
{
public:

    ... etc

    Game(const Game& obj);
    Game& operator=(const Game& obj);

    ... etc

};


and in the member body's make a deep copy of the object that render points to and copy the other members normally (shallow copy) ...



// Note: you can use member initialiser lists for this function, as I see you did for your default constructor, but I'm just keeping it simple
Game::Game(const Game& obj)
{
    render = new drawScene(*(obj.render));

    // do the "normal" copies ...
    loaded = obj.loaded;

    ... etc
}

Game& Game::operator=(const Game& obj)
{
    if (this == &obj)
        return *this;

    delete render;

    render = new drawScene(*(obj.render));

    // do the "normal" copies ...
    loaded = obj.loaded;

    ... etc

    return *this;
}


This probably will have confused you, but have a look on Google for information on classes and dynamic memory allocation. I might come back in a minute and post an explanation though, depending on whether I have enough time.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 6, 2003 12:57:01 PM]

Share this post


Link to post
Share on other sites
Lektrix, that is a good point, thanks.
I use only one Game object in the entire game so don''t need to "=" it or to copy it, so Why do I need a copy-constructor for Game ?
Yet, you gave me a good point, I have to think about it.

Share this post


Link to post
Share on other sites

I found The answer to the enigma !


let's say you have a class like that:
 
class classB
{
int aVar;
.........
};

When classB is initialized in this way:
classB b;
The variable aVar got automatically zero value.
But if classB is initialized in this way:
classB *b=new classB();
the variable aVar can have any value.
So I made a mistake not to set aVar value in the constructor,
and to count on the compiler to do it for me.



[edited by - friman on May 9, 2003 6:55:46 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by friman
I use only one Game object in the entire game so don''t need to "=" it or to copy it

Then disable the assignment op and copy ctor like so:

  
class thing
{
private:
thing(const thing&);
thing& operator=(const thing&);
};

Share this post


Link to post
Share on other sites