Free memory in a vector<char*>

Discussion in 'C++' started by yinglcs@gmail.com, Aug 5, 2007.

  1. Guest

    Hi,

    I have a vector<char*>

    vector<char *> arguments;

    and I have code to free memory (from example in effective stl):

    struct DeleteObject {

    template<typename T>
    void operator()(const T* ptr) const {
    delete ptr;
    }
    };

    for_each(arguments.begin(), arguments.end() , DeleteObject());

    arguments.clear();

    My question is I currently is doing this:
    // allocate memory on the heap
    char* arg2 = new char[20];
    memset (arg2, '\0', 20);
    memcpy( arg2, "dummy", strlen("dummy") );
    arguments.push_back(arg2);

    I wonder if I can simplify by doing this:
    arguments.push_back("dummy");

    But if i do this, where does the memory allocated? And If I can still
    use this:

    for_each(arguments.begin(), arguments.end() , DeleteObject());

    to free all memory used by arguments?

    Thank you.
    , Aug 5, 2007
    #1
    1. Advertising

  2. Barry Guest

    wrote:
    > Hi,
    >
    > I have a vector<char*>
    >
    > vector<char *> arguments;
    >
    > and I have code to free memory (from example in effective stl):
    >
    > struct DeleteObject {
    >
    > template<typename T>
    > void operator()(const T* ptr) const {
    > delete ptr;
    > }
    > };
    >
    > for_each(arguments.begin(), arguments.end() , DeleteObject());
    >
    > arguments.clear();
    >
    > My question is I currently is doing this:
    > // allocate memory on the heap
    > char* arg2 = new char[20];
    > memset (arg2, '\0', 20);
    > memcpy( arg2, "dummy", strlen("dummy") );
    > arguments.push_back(arg2);
    >
    > I wonder if I can simplify by doing this:
    > arguments.push_back("dummy");


    you can
    but better change vector<char*> into vector<char const*>

    >
    > But if i do this, where does the memory allocated?
    >


    "dummy" is a string constant, it's a static const data

    > And If I can still
    > use this:
    >
    > for_each(arguments.begin(), arguments.end() , DeleteObject());
    >
    > to free all memory used by arguments?
    >


    No, since the memory is not on the heap


    > Thank you.
    >
    Barry, Aug 5, 2007
    #2
    1. Advertising

  3. * :
    > Hi,
    >
    > I have a vector<char*>
    >
    > vector<char *> arguments;
    >
    > and I have code to free memory (from example in effective stl):
    >
    > struct DeleteObject {
    >
    > template<typename T>
    > void operator()(const T* ptr) const {
    > delete ptr;
    > }
    > };
    >
    > for_each(arguments.begin(), arguments.end() , DeleteObject());
    >
    > arguments.clear();
    >
    > My question is I currently is doing this:
    > // allocate memory on the heap
    > char* arg2 = new char[20];
    > memset (arg2, '\0', 20);
    > memcpy( arg2, "dummy", strlen("dummy") );
    > arguments.push_back(arg2);
    >
    > I wonder if I can simplify by doing this:
    > arguments.push_back("dummy");
    >
    > But if i do this, where does the memory allocated? And If I can still
    > use this:
    >
    > for_each(arguments.begin(), arguments.end() , DeleteObject());
    >
    > to free all memory used by arguments?


    In order not to mince words, all of the above is Wrong (TM).

    Use vector<string>, and be done with it.

    Don't use raw pointers, don't use memset, don't use memcpy, and above
    all, don't think about efficiency (the usual reason for newbies doing
    this) because first of all it doesn't count when the program is
    incorrect, as above, and secondly, newbie ideas of efficiency are all
    Wrong, i.e., the end result isn't efficiency anyway.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 5, 2007
    #3
  4. Barry Guest

    wrote:
    > Hi,
    >
    > I have a vector<char*>
    >
    > vector<char *> arguments;
    >
    > and I have code to free memory (from example in effective stl):
    >
    > struct DeleteObject {
    >
    > template<typename T>
    > void operator()(const T* ptr) const {
    > delete ptr;
    > }
    > };
    >
    > for_each(arguments.begin(), arguments.end() , DeleteObject());
    >
    > arguments.clear();
    >
    > My question is I currently is doing this:
    > // allocate memory on the heap
    > char* arg2 = new char[20];
    > memset (arg2, '\0', 20);
    > memcpy( arg2, "dummy", strlen("dummy") );
    > arguments.push_back(arg2);
    >
    > I wonder if I can simplify by doing this:
    > arguments.push_back("dummy");


    you can
    but better change vector<char*> into vector<char* const>

    >
    > But if i do this, where does the memory allocated?
    >


    "dummy" is a string constant, it's a static const data

    > And If I can still
    > use this:
    >
    > for_each(arguments.begin(), arguments.end() , DeleteObject());
    >
    > to free all memory used by arguments?
    >


    No, since the memory is not on the heap


    > Thank you.
    >
    Barry, Aug 5, 2007
    #4
  5. On Aug 5, 6:58 am, "" <> wrote:
    > Hi,
    >
    > I have a vector<char*>
    >
    > vector<char *> arguments;
    >
    > and I have code to free memory (from example in effective stl):
    >
    > struct DeleteObject {
    > template<typename T>
    > void operator()(const T* ptr) const {
    > delete ptr;
    > }
    > };
    >


    In addition to the other responses, if you're allocating using
    char* arg2 = new char[20];
    Then should really use a differentDeleteObject, called e.g.
    DeleteArray, that uses delete[] ptr; rather than plain delete ptr;
    tragomaskhalos, Aug 6, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,787
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  3. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  5. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    416
    Keith Thompson
    Dec 27, 2007
Loading...

Share This Page