Does this function have memory problem?

Discussion in 'C++' started by xz, Apr 4, 2008.

  1. xz

    xz Guest

    Does this function have memory problem?

    void pushSomthingIntoVector(vector<T> & v) { //T is a class
    v.assign(0, T(100)) //T(int a) is a constructor of class T
    v.push_back(T(50))
    }

    void anotherFunction() {
    vector<T> v;
    pushSomethingIntoVector(v);
    cout << v[0] << endl;
    cout << v[1] << endl;
    }

    T(100) and T(50) are created locally within the function
    pushSomthingIntoVector(vector<T> & v), does it exsit outside of the
    function
    ? say, in the two lines of "cout" ?

    Should I change the code as follows:

    void pushSomthingIntoVector(vector<T> & v) { //T is a class
    v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
    v.push_back(*(new T(50)))
    }
    xz, Apr 4, 2008
    #1
    1. Advertising

  2. xz

    Sharad Guest

    "xz" <> wrote in message
    > Does this function have memory problem?
    >
    > void pushSomthingIntoVector(vector<T> & v) { //T is a class
    > v.assign(0, T(100)) //T(int a) is a constructor of class T
    > v.push_back(T(50))
    > }
    >
    > void anotherFunction() {
    > vector<T> v;
    > pushSomethingIntoVector(v);
    > cout << v[0] << endl;
    > cout << v[1] << endl;
    > }
    >
    > T(100) and T(50) are created locally within the function
    > pushSomthingIntoVector(vector<T> & v), does it exsit outside of the
    > function
    > ? say, in the two lines of "cout" ?


    Yes, they do exist outside the function. You pass vector by reference, hence
    the changes to vector are seen outside the function too. Internally copies
    of the temporary objects (that you create in pushSomthingIntoVector) are
    stored inside the vector. They exist even when the function returns.

    > Should I change the code as follows:
    >
    > void pushSomthingIntoVector(vector<T> & v) { //T is a class
    > v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
    > v.push_back(*(new T(50)))
    > }


    No, let std::vector do the memory management for you.

    Sharad
    P.S. - Check the documentation of vector's assign member function.
    Sharad, Apr 5, 2008
    #2
    1. Advertising

  3. xz

    asterisc Guest

    On Apr 5, 1:33 am, xz <> wrote:
    > Does this function have memory problem?
    >
    > void pushSomthingIntoVector(vector<T> & v) { //T is a class
    > v.assign(0, T(100)) //T(int a) is a constructor of class T
    > v.push_back(T(50))
    >
    > }
    >
    > void anotherFunction() {
    > vector<T> v;
    > pushSomethingIntoVector(v);
    > cout << v[0] << endl;
    > cout << v[1] << endl;
    >
    > }
    >
    > T(100) and T(50) are created locally within the function
    > pushSomthingIntoVector(vector<T> & v), does it exsit outside of the
    > function
    > ? say, in the two lines of "cout" ?
    >
    > Should I change the code as follows:
    >
    > void pushSomthingIntoVector(vector<T> & v) { //T is a class
    > v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
    > v.push_back(*(new T(50)))
    >
    > }


    Let std::vector to do his job (manage memory allocations/
    deallocations).
    One advice though,instead of:

    void anotherFunction() {
    > vector<T> v;
    > pushSomethingIntoVector(v);
    > cout << v[0] << endl;
    > cout << v[1] << endl;
    >


    try to use
    for( vector<T>::const_iterator it = v.begin(); it != v.end(); ++it )
    cout << *it << endl;

    to be sure that your previous function push something there.

    Retrieving elements from a vector won't do any bounds check and won't
    throw any exception.
    asterisc, Apr 5, 2008
    #3
  4. Obnoxious User, Apr 5, 2008
    #4
  5. xz

    James Kanze Guest

    On 5 avr, 02:30, asterisc <> wrote:

    > Retrieving elements from a vector won't do any bounds check
    > and won't throw any exception.


    Using the [] operator with an out of bounds value is undefined
    behavior. In any modern implementation, I would expect an
    assertion failure, or something similar. (It's the case with
    g++ and VC++, at least.)

    And of course, if you have an unforseen out of bounds condition,
    you don't want an exception; you want the program to crash.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 5, 2008
    #5
  6. xz

    James Kanze Guest

    On Apr 7, 5:04 pm, Ron Natalie <> wrote:
    > James Kanze wrote:
    > > Using the [] operator with an out of bounds value is undefined
    > > behavior. In any modern implementation, I would expect an
    > > assertion failure, or something similar. (It's the case with
    > > g++ and VC++, at least.)


    > Provided you are in a mode with such assertions turned on.


    > VC++ won't do it in release mode.


    Now Ron, you know better:). The compiler does what you tell it to
    do. And VC++ (like every other compiler) offers tons of
    options, not just one or two modes. The code I deliver has the
    assertions turned on unless there is a real performance
    problems.

    It is true that typically, code compiled with them on and code
    compiled with them off cannot be linked together. (Worse: at
    least with g++, it can be linked without problems; it just core
    dumps when you run it.) Which means that one tight loop, and
    you have to recompile the entire application and all libraries
    with it turned off everywhere.

    This also means that if I'm delivering a library, I have to
    deliver it in two versions, since I can't know in advance
    whether the client code will have a performance problem
    elsewhere which will require turning the assertions off.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 8, 2008
    #6
  7. xz

    Jerry Coffin Guest

    In article <06a70357-8f00-4673-b305-
    >,
    says...
    > Does this function have memory problem?
    >
    > void pushSomthingIntoVector(vector<T> & v) { //T is a class
    > v.assign(0, T(100)) //T(int a) is a constructor of class T
    > v.push_back(T(50))
    > }


    This doesn't seem to make any sense. The vector starts out empty, so you
    could just as well use:

    void pushSomethingIntoVector(vector<T> &v) {
    v.push_back(T(100));
    v.push_back(T(50));
    }

    > void anotherFunction() {
    > vector<T> v;
    > pushSomethingIntoVector(v);
    > cout << v[0] << endl;
    > cout << v[1] << endl;
    > }


    Here you're taking for granted that the modification succeeds. I'd do
    something like:

    copy(v.begin(), v.end(), ostream_iterator<T>(std::cout, "\n"));

    This will only copy out the real contents of the vector, not what you
    _think_ you put there.

    [ ... ]

    > Should I change the code as follows:
    >
    > void pushSomthingIntoVector(vector<T> & v) { //T is a class
    > v.assign(0, *(new T(100)) ) //T(int a) is a constructor of class T
    > v.push_back(*(new T(50)))
    > }


    No, you should not. You're using new to allocate objects, but you're NOT
    keeping a pointer to them afterwards. That means you have no way of
    deleting those objects, so the memory is inevitably leaked.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Apr 11, 2008
    #7
  8. xz

    Jerry Coffin Guest

    In article <c50ffbd8-2a5d-4179-a3ae-
    >, says...

    [ ... ]

    > try to use
    > for( vector<T>::const_iterator it = v.begin(); it != v.end(); ++it )
    > cout << *it << endl;


    While I agree with using begin() and end() to find the current size of
    the vector, I do NOT agree that a for-loop is the way to go here. There
    are things that get more complex when you try to use standard
    algorithms, but this is NOT one of them. I'd advise using std::copy
    instead.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Apr 11, 2008
    #8
    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. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Does a resource have to have all phrases in it?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Jan 30, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    374
    Neil Ni
    Feb 3, 2006
  2. Replies:
    5
    Views:
    311
    James Kanze
    May 24, 2007
  3. Weng Tianxiang
    Replies:
    7
    Views:
    1,274
    Paul Uiterlinden
    Sep 11, 2009
  4. Nanostuff
    Replies:
    9
    Views:
    136
    Gary Wright
    Mar 1, 2007
  5. Joshua Muheim
    Replies:
    5
    Views:
    125
    Phlip
    Aug 11, 2007
Loading...

Share This Page