Problem with pointer to vector objects

This is a discussion on Problem with pointer to vector objects within the C++ Programming forums, part of the General Programming Boards category; I have 2 static lists. EntityList::EnemyList, which is a list of enemies, and EntityList::MasterList, which is a list of pointers ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    36

    Problem with pointer to vector objects

    I have 2 static lists. EntityList::EnemyList, which is a list of enemies, and EntityList::MasterList, which is a list of pointers to ALL entities, like enemies, the player, bullets, etc.

    In my factory function for entities, I have this code, which creates a new Enemy object:
    Code:
    EntityList::EnemyList.push_back(Enemy());
    EntityList::MasterList.push_back(&EntityList::EnemyList.back());
    return &EntityList::EnemyList.back();
    This works fine, but as soon as I create more than one enemy, the pointer in MasterList apparently becomes invalid because the vector is resized and repositioned.

    This code works, but there is a memory leak:
    Code:
    Enemy *ent=new Enemy();
    EntityList::EnemyList.push_back(*ent);
    EntityList::MasterList.push_back(ent);
    return ent;
    Is there a happy medium here? Neither works 100%. I was thinking smart pointers but those would just go out of scope after the function, no?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Make you EnemyList to store the pointers and be the owner of the pointer. It means when the pointer is removed from this list - the object pointing by the pointer should be deleted
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    How do I do that? I have never actually used smart pointers before. Could you show me a quick example?
    Last edited by ryeguy; 02-11-2008 at 02:42 AM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    A smart pointer is typically set and forget.
    In other words, you use it like you would any pointer, but it deletes the memory automatically.
    However, what vart speaks of is not a smart pointer.
    Regardless, I would always recommend smart pointers due to amount of work they take off you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    This must be over my head then. What exactly is he talking about when he says set the owner of a pointer?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Vart is referring to that EnemyList should "own" the pointer in that it's the one that controls whether the pointer lives or dies. When EnemyList is destroyed, it should also delete the pointer. When working with raw pointers, it's important to have a clear image on who deletes the pointers. Typically you should define one class to be responsible for this and not delete it wherever because it's very hard to manage and track down memory issues when you do that and it's also a very good chance that you'll delete the pointer when you shouldn't.

    But if you can, you should just download boost and used shared_ptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21