dynamic vector as function parameter

A

A

// 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.
 
V

Victor Bazarov

// 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
 
A

A

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

A

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.
 
J

Jorgen Grahn

.
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
 
J

Jorgen Grahn

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top