Each frame: struct PP *State = new PP

This is a discussion on Each frame: struct PP *State = new PP within the C++ Programming forums, part of the General Programming Boards category; I am writting a firewire video processing program and on each frame, I create a new structure with struct PP ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    Each frame: struct PP *State = new PP

    I am writting a firewire video processing program and on each frame, I create a new structure with

    struct PP *State = new PP

    then I fill the struct as needed. After that, I laod it, push_back(State), onto a vector. I never delete it because I thought when I call ...new PP again for the next frame, it overwrites the old structure. Is this true? Or am I taking up RAM? Is it that eventhough I dynamically allocate the same structure on each frame, it is not created in the same memory spot? Let me know how this works with regards to where the structure is stored in memory & where the next structure is stored.

    Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    For every call to new, you must call delete to free the memory and destruct the struct. If you are pushing back a pointer into the vector, then that pointer will be stored in that vector until the vector is cleared or goes out of scope. The memory address pointed to by the pointer will still have a valid struct in it as long as you haven't called delete on that pointer.

    If you don't need the struct anymore after the next frame, then you can just declare he struct locally without new and it will go out of scope automatically. I doubt this though since you seem to be saving these pointers in the vector.

    If the PP struct is somewhat simple (and copyable) then you can just hold PP objects instead of pointers in your vector. This would also relieve you of the need to use new and you could just create local variables that get destroyed automatically.

    If you have to hold pointers, just make sure you delete the pointer and remove itfrom the vector when it is not needed anymore.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    hk_mp5kpdw to going to reply

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Is it that eventhough I dynamically allocate the same structure on each frame, it is not created in the same memory spot?
    Usually... no. However, there is a version of the new call that is refered to as placement new which takes as an argument an address to where you wish to create the newly allocated object. In general it is used to create new objects within a specific memory area (a buffer of some sort) that has already been allocated either statically or dynamically. In theory, I suppose you could use the everyday call to new to create an initial object/struct off the heap and thereafter use placement new to reallocate the subsequent objects into the same memory as that first object.

    After that, I laod it, push_back(State), onto a vector. I never delete it because I thought when I call ...new PP again for the next frame, it overwrites the old structure. Is this true? Or am I taking up RAM?
    If your assumption is that repeated calls to new would overwrite the memory already allocated then what is the purpose of the vector? Such a vector would consist of dozens/hundreds/thousands (one for each push_back() call) of pointers to the same address. What would be the point of that? Or am I misunderstanding you? Seems that a vector of pointers is unnecessary if that assumption were to hold true. A single pointer would be all that is necessary/usefull.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 05:07 AM
  2. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 03:56 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM

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