dynamic vector as function parameter

Discussion in 'C++' started by A, Sep 15, 2011.

  1. A

    A Guest

    // This works
    ABC( std::vector<__int64>(1,0x1234i64) );

    function ABC(const std::vector<__int64> &vect)
    {
    // read from vect
    for (unsigned i = 0; i < vect.size(); i++)
    {
    // Call function DEF
    DEF(vect);
    }
    }

    The question - if I do it like this - will there be any drawbacks? I simply
    need to use vector once without defining a variable - that's it.
    A, Sep 15, 2011
    #1
    1. Advertising

  2. On 9/15/2011 12:38 PM, A wrote:
    > // This works
    > ABC( std::vector<__int64>(1,0x1234i64) );
    >
    > function ABC(const std::vector<__int64> &vect)


    You meant

    void ABC(const ...

    probably.

    > {
    > // read from vect
    > for (unsigned i = 0; i< vect.size(); i++)
    > {
    > // Call function DEF
    > DEF(vect);
    > }
    > }
    >
    > The question - if I do it like this - will there be any drawbacks? I simply
    > need to use vector once without defining a variable - that's it.


    There are no drawbacks besides performance, I guess. Which ones are
    *you* thinking of? You're probably aware of the overhead you're going
    to incur by creating the vector of only one element. Can't you just
    call function DEF directly?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 15, 2011
    #2
    1. Advertising

  3. A

    A Guest

    > There are no drawbacks besides performance, I guess. Which ones are *you*
    > thinking of? You're probably aware of the overhead you're going to incur
    > by creating the vector of only one element. Can't you just call function
    > DEF directly?


    DEF function is just a dumb example it is more complex than this. Not to be
    overanalized :)

    My main concern here are memory leaks and to check with experts here if this
    is the way to pass it when I want to avoid creating a variable.

    The function should accept vector as input but on a few occasions it needs
    just a single parameter and instead of overloading it or using a vector as
    variable I simply want a quick way to pass a parameter into it.

    Performance is of no concern here. Thanks for the answer.
    A, Sep 15, 2011
    #3
  4. A

    A Guest

    > Yes, that's OK. This is creating a temporary object which will be
    > destroyed
    > automatically in the end of the full expression (semicolon here). In
    > general, it is hard to produce a memory leak in C++ without a 'new' or
    > 'malloc'.


    ok, that's what i needed basically. thanks.
    A, Sep 16, 2011
    #4
  5. A

    Jorgen Grahn Guest

    On Thu, 2011-09-15, A wrote:
    ....
    > The function should accept vector as input but on a few occasions it needs
    > just a single parameter and instead of overloading it or using a vector as
    > variable I simply want a quick way to pass a parameter into it.


    It sounds to me it would be smarter to overload it anyway, for
    readability in the client code:

    void ABC(const std::vector<__int64> &vect);
    void ABC(__int64 val);

    You can of course write the second one in terms of the first one,
    using the technique you showed.

    Or, write them both in terms of a third function, to get rid of the
    somewhat unnatural dependency on std::vector:

    void ABC(const __int64* begin, const __int64* end);

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Sep 18, 2011
    #5
  6. A

    A Guest

    > --
    > // Jorgen Grahn <grahn@ Oo o. . .
    > \X/ snipabacken.se> O o .


    i this one of those amiga-rulez signatures? :)
    A, Sep 18, 2011
    #6
  7. A

    Jorgen Grahn Guest

    On Sun, 2011-09-18, A wrote:
    >> // Jorgen Grahn <grahn@ Oo o. . .
    >> \X/ snipabacken.se> O o .

    >
    > i this one of those amiga-rulez signatures? :)


    It is the ASCII version of the Amiga logo, yes.
    (Although your news reader seems to have a bug which mangles it
    when quoting.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Sep 18, 2011
    #7
    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,772
  2. Replies:
    8
    Views:
    1,913
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  4. Rushikesh Joshi
    Replies:
    0
    Views:
    355
    Rushikesh Joshi
    Jul 10, 2004
  5. AzamSharp
    Replies:
    2
    Views:
    174
Loading...

Share This Page