Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10176 > unrolled thread

dynamic vector as function parameter

Started by"A" <a@a.a>
First post2011-09-15 18:38 +0200
Last post2011-09-18 17:37 +0000
Articles 8 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  dynamic vector as function parameter "A" <a@a.a> - 2011-09-15 18:38 +0200
    Re: dynamic vector as function parameter Victor Bazarov <v.bazarov@comcast.invalid> - 2011-09-15 13:57 -0400
      Re: dynamic vector as function parameter "A" <a@a.a> - 2011-09-15 21:45 +0200
        Re: dynamic vector as function parameter Paavo Helde <myfirstname@osa.pri.ee> - 2011-09-15 16:28 -0500
          Re: dynamic vector as function parameter "A" <a@a.a> - 2011-09-16 13:49 +0200
        Re: dynamic vector as function parameter Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-17 23:48 +0000
          Re: dynamic vector as function parameter "A" <a@a.a> - 2011-09-18 16:37 +0200
            Re: dynamic vector as function parameter Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-18 17:37 +0000

#10176 — dynamic vector as function parameter

From"A" <a@a.a>
Date2011-09-15 18:38 +0200
Subjectdynamic vector as function parameter
Message-ID<j4t9oo$vj0$1@gregory.bnet.hr>
// 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[i]);
    }
}

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. 

[toc] | [next] | [standalone]


#10178

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-09-15 13:57 -0400
Message-ID<j4te9t$ji6$1@dont-email.me>
In reply to#10176
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[i]);
>      }
> }
>
> 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

[toc] | [prev] | [next] | [standalone]


#10181

From"A" <a@a.a>
Date2011-09-15 21:45 +0200
Message-ID<j4tkme$io4$1@gregory.bnet.hr>
In reply to#10178
> 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. 

[toc] | [prev] | [next] | [standalone]


#10183

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2011-09-15 16:28 -0500
Message-ID<Xns9F624BFDFCCCmyfirstnameosapriee@216.196.109.131>
In reply to#10181
"A" <a@a.a> wrote in news:j4tkme$io4$1@gregory.bnet.hr:

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

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

cheers
Paavo

[toc] | [prev] | [next] | [standalone]


#10194

From"A" <a@a.a>
Date2011-09-16 13:49 +0200
Message-ID<j4vd5c$olc$1@gregory.bnet.hr>
In reply to#10183
> 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. 

[toc] | [prev] | [next] | [standalone]


#10237

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-09-17 23:48 +0000
Message-ID<slrnj7acdv.gtj.grahn+nntp@frailea.sa.invalid>
In reply to#10181
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   .

[toc] | [prev] | [next] | [standalone]


#10248

From"A" <a@a.a>
Date2011-09-18 16:37 +0200
Message-ID<j54voa$qku$1@gregory.bnet.hr>
In reply to#10237
> -- 
>  // Jorgen Grahn <grahn@  Oo  o.   .     .
> \X/     snipabacken.se>   O  o   .

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

[toc] | [prev] | [next] | [standalone]


#10254

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-09-18 17:37 +0000
Message-ID<slrnj7cb2a.1qs.grahn+nntp@frailea.sa.invalid>
In reply to#10248
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   .

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web